Skip to content

Python Console: Preserve the result of the last executed command (#9782)#9783

Merged
michaelDCurran merged 2 commits into
nvaccess:masterfrom
accessolutions:i9782
Jun 25, 2019
Merged

Python Console: Preserve the result of the last executed command (#9782)#9783
michaelDCurran merged 2 commits into
nvaccess:masterfrom
accessolutions:i9782

Conversation

@JulienCochuyt

@JulienCochuyt JulienCochuyt commented Jun 21, 2019

Copy link
Copy Markdown
Contributor

Link to issue number:

Fixes #9782

Summary of the issue:

The NVDA Python Console implementation prevents clobbering of the gettext "_" built-in but at the same time prevents the user from taking advantage of the standard sys.displayhook saving the last returned value under that same name.

Description of how this pull request fixes the issue:

Save the last execution result in the global namespace rather than in __builtins__.
Gettext is thus not affected.

Testing performed:

Known issues with pull request:

Unlike on the standard Python Console where the "" special variable is stored in builtins, a variable named "" in the global namespace will be overridden every time a command is executed.
An exception is made for the gettext function. That is, if it is explicitly promoted to the global namespace by executing _ = _ it won't get overridden on subsequent commands execution until explicitly deleted by del _.
If this is to be considered a problem, a little more intrusive implementation could extend the namespace dictionary to specially handle "_" and preserve any manually defined global with that name.

Furthermore, None is the only special value in the standard sys.displayhook, as it never overrides the "_" built-in. This PR treats the gettext function in the same way and thus does not override the last result if the executed command returns either None or the gettext function.
If this is to be considered a problem, I guess a custom display hook could do the trick, but I doubt it would be worth the effort.

Change log entry:

Python Console: The global variable "_" now stores the result of the last executed command.

@LeonarddeR LeonarddeR left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I really like this proposal, however, I think it needs to be documented in the developer guide that in most cases, _ contains the last result on the python console instead of the gettext function and that it could always be accessed by explicitly delling _ or using builtin._. I've already verified that this will work in the Python 3 version of NVDA as well, as long as builtin is renamed to builtins

@michaelDCurran michaelDCurran merged commit 329657f into nvaccess:master Jun 25, 2019
@nvaccessAuto nvaccessAuto added this to the 2019.3 milestone Jun 25, 2019
michaelDCurran added a commit that referenced this pull request Jun 25, 2019
@JulienCochuyt JulienCochuyt deleted the i9782 branch June 26, 2019 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python Console: Preserve the result of the last executed command

4 participants