pythonConsole module

class pythonConsole.HelpCommand

Bases: object

Emulation of the ‘help’ command found in the Python interactive shell.

_reprMessage = 'Type help(object) to get help about object.'
class pythonConsole.ExitConsoleCommand(exitFunc)

Bases: object

An object that can be used as an exit command that can close the console or print a friendly message for its repr.

_reprMessage = 'Type exit() to exit the console'
pythonConsole.consoleUI = None

The singleton Python console UI instance.

class pythonConsole.Completer(namespace=None)

Bases: Completer

Create a new completer for the command line.

Completer([namespace]) -> completer instance.

If unspecified, the default namespace where completions are performed is __main__ (technically, __main__.__dict__). Namespaces should be given as dictionaries.

Completer instances should be used as the completion mechanism of readline via the set_completer() call:

readline.set_completer(Completer(my_namespace).complete)

attr_matches(text: str) list[str]

attr_matches implementation as used in Python 3.9. In Python 3.10, attr_matches was changed in a way that filtered out property descriptors, but more importantly, no longer catches exceptions when calling getattr. This causes serious issues for baseObject.Getter descriptors when a getter raises NotImplementedError, for example (#15872).

_callable_postfix(val, word)
class pythonConsole.CommandCompiler

Bases: CommandCompiler

A L{codeop.CommandCompiler} exposing the status of the last compilation.

error

Whether the last compilation was on error. @type: bool

class pythonConsole.PythonConsole(*args, **kwargs)

Bases: InteractiveConsole, AutoPropertyObject

An interactive Python console for NVDA which directs output to supplied functions. This is necessary for a Python console with input/output other than stdin/stdout/stderr. Input is always received via the L{push} method. This console handles redirection of stdout and stderr and prevents clobbering of the gettext “_” builtin. The console’s namespace is populated with useful modules and can be updated with a snapshot of NVDA’s state using L{updateNamespaceSnapshotVars}.

Constructor.

The optional locals argument will be passed to the InteractiveInterpreter base class.

The optional filename argument should specify the (file)name of the input stream; it will show up in tracebacks.

namespace

The namespace available to the console. This can be updated externally. @type: dict

_namespaceSnapshotVars

The variables last added to the namespace containing a snapshot of NVDA’s state. @type: Optional[dict]

property prompt
_set_prompt(prompt)
_get_prompt()
write(data)

Write a string.

The base implementation writes to sys.stderr; a subclass may replace this with a different implementation.

push(line)

Push a line to the interpreter.

The line should not have a trailing newline; it may have internal newlines. The line is appended to a buffer and the interpreter’s runsource() method is called with the concatenated contents of the buffer as source. If this indicates that the command was executed or invalid, the buffer is reset; otherwise, the command is incomplete, and the buffer is left as it was after the line was appended. The return value is 1 if more input is required, 0 if the line was dealt with in some way (this is the same as runsource()).

showsyntaxerror(filename=None)

Display the syntax error that just occurred.

This doesn’t display a stack trace because there isn’t one.

If a filename is given, it is stuffed in the exception instead of what was there before (because Python’s parser always uses “<string>” when reading from a string).

The output is written by self.write(), below.

showtraceback()

Display the exception that just occurred.

We remove the first stack item because it is our own code.

The output is written by self.write(), below.

initNamespace()

(Re-)Initialize the console namespace with useful globals.

updateNamespaceSnapshotVars()

Update the console namespace with a snapshot of NVDA’s current state. This creates/updates variables for the current focus, navigator object, etc. Typically, used before the NVDA python console is opened, after which, calls to the ‘api’ module will refer to this new focus.

removeNamespaceSnapshotVars()

Remove the variables from the console namespace containing the last snapshot of NVDA’s state. This removes the variables added by L{updateNamespaceSnapshotVars}.

_abc_impl = <_abc._abc_data object>
_propertyCache: Set[GetterMethodT]
class pythonConsole.ConsoleUI(parent)

Bases: ContextHelpMixin, Frame

The NVDA Python console GUI.

helpId = 'PythonConsole'
onActivate(evt)
onClose(evt)
output(data)
clear()

Clear the output.

echo(data)
setPrompt(prompt)
execute()
historyMove(movement)
RE_COMPLETE_UNIT = re.compile('[\\w.]*$')
complete()
_getCompletions(original)
_findBestCompletion(original, completions)
_insertCompletion(original, completed)
onInputChar(evt)
onInputPaste(evt)
onInputTextChange(evt: CommandEvent)
onOutputKeyDown(evt)
onOutputChar(evt)
pythonConsole.initialize()

Initialize the NVDA Python console GUI. This creates a singleton instance of the console GUI. This is accessible as L{consoleUI}. This may be manipulated externally.

pythonConsole.activate()

Activate the console GUI. This shows the GUI and brings it to the foreground if possible. @precondition: L{initialize} has been called.