Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of ~/.nestrc in show_help_with_pager() #931

Closed
wants to merge 1 commit into from

Conversation

physicalist
Copy link

This fixes a bug in the code parsing the pager command in ~/.nestrc which
could lead to a TypeError exception when calling subprocess.call with
pager=None if ~/.nestrc is an empty file (0 bytes).
I discovered this when @babsey and I discussed his changes in #928 and #913.
In addition, the code now opens ~/.nestrc safely using with and uses
only one loop and regular expression for parsing and returns the last
/page << /command entry instead of the first it finds.

This fixes the code parsing for the pager command in ~/.nestrc which
could lead to a TypeError exception when calling subprocess.call with
pager=None if ~/.nestrc is an empty file (0 bytes).
In addition, the code now opens ~/.nestrc safely using `with` and uses
only one loop and one regular expression for parsing and uses the last
'/page << /command' entry instead of the first it finds.
@heplesser heplesser requested review from heplesser and jougs April 19, 2018 11:28
@heplesser heplesser added T: Bug Wrong statements in the code or documentation ZC: Infrastructure DO NOT USE THIS LABEL ZP: PR Created DO NOT USE THIS LABEL S: High Should be handled next I: No breaking change Previously written code will work as before, no one should note anything changing (aside the fix) labels Apr 19, 2018
@heplesser heplesser added this to the NEST 2.16 milestone Apr 19, 2018
pager = pa
except:
print('Failed to open ~/.nestrc, using default pager')
pass

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you parsing the nestrc file manually? It is run by SLI upon startup, so you can just do something like

nest.sli_func("/page GetOptions")["command"]

to get the command in a much safer way. To have a default pager available in case nestrc does not define one, you can use

try:
    pager = nest.sli_func("/page GetOptions")["command"]
except:
    pager = 'more'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jougs The parsing code was introduced over a year ago in bdc87b7 and merged with #688. I reviewed then, and as now entirely overlooked the fact that we know the parser once NEST is loaded. So @physicalist, the solution proposed by @jougs is the way to go.

Copy link
Contributor

@heplesser heplesser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@physicalist Thanks for the PR, it looks good except for a few details I commented on inline. Could you also send me a filled, signed and scanned contributor agreement by personal email?

line):
pager = pa
except:
print('Failed to open ~/.nestrc, using default pager')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather write "parse" than "open", since in principle an exception could occur during reading as well.

pager = pa
except:
print('Failed to open ~/.nestrc, using default pager')
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass is not needed here, please remove it.

r'^\s?(?!%)\s?\/page\s?<<\s?\/command\s?\((\w*)',
line):
pager = pa
except:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 advises against bare except clauses. except IOError seems appropriate here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I meant to look up the most general but still appropriate exception class but forgot to do that :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the above solution the exception class would simply be NESTError.

@heplesser
Copy link
Contributor

Superseeded by #948.

@heplesser heplesser closed this May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I: No breaking change Previously written code will work as before, no one should note anything changing (aside the fix) S: High Should be handled next T: Bug Wrong statements in the code or documentation ZC: Infrastructure DO NOT USE THIS LABEL ZP: PR Created DO NOT USE THIS LABEL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants