Skip to content

Commit

Permalink
fix when no configuration file
Browse files Browse the repository at this point in the history
some code cleanup
  • Loading branch information
gotcha committed Feb 28, 2020
1 parent c23f5c0 commit 59ef89a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion HISTORY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
0.13.1 (unreleased)
-------------------

- Nothing changed yet.
- Fix when no configuration file
[gotcha]


0.13.0 (2020-02-28)
Expand Down
23 changes: 21 additions & 2 deletions ipdb/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def set_trace(frame=None, context=None):
wrap_sys_excepthook()
if not context:
context = os.environ.get(
"IPDB_CONTEXT_SIZE", get_config().getint("ipdb", "context", fallback=3)
"IPDB_CONTEXT_SIZE", get_context_from_config()
)
if frame is None:
frame = sys._getframe().f_back
Expand All @@ -77,6 +77,21 @@ def set_trace(frame=None, context=None):
p.shell.restore_sys_module_state()


def get_context_from_config():
try:
parser = get_config()
print(parser.__dict__)
return parser.getint("ipdb", "context")
except (configparser.NoSectionError, configparser.NoOptionError):
return 3
except ValueError:
value = parser.get("ipdb", "context")
raise ValueError(
"In %s, context value [%s] cannot be converted into an integer."
% (parser.filepath, value)
)


class ConfigFile(object):
"""
Filehandle wrapper that adds a "[ipdb]" section to the start of a config
Expand Down Expand Up @@ -146,8 +161,12 @@ def get_config():
if filepaths:
# Python 3 has parser.read_file(iterator) while Python2 has
# parser.readfp(obj_with_readline)
read_func = getattr(parser, 'read_file', getattr(parser, 'readfp'))
try:
read_func = parser.read_file
except AttributeError:
read_func = parser.readfp
for filepath in filepaths:
parser.filepath = filepath
# Users are expected to put an [ipdb] section
# only if they use setup.cfg
if filepath.endswith('setup.cfg'):
Expand Down

0 comments on commit 59ef89a

Please sign in to comment.