Skip to content

Commit b8192d5

Browse files
authored
Merge 8268f49 into 05aeb3d
2 parents 05aeb3d + 8268f49 commit b8192d5

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

source/pythonConsole.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from logHandler import log # noqa: E402
3030
import braille # noqa: E402
3131
import gui.contextHelp # noqa: E402
32+
import textInfos
3233

3334

3435
class HelpCommand(object):
@@ -223,7 +224,6 @@ def initNamespace(self):
223224
import config
224225
import controlTypes
225226
import globalPlugins
226-
import textInfos
227227
import vision
228228

229229
self.namespace.clear()
@@ -255,25 +255,35 @@ def updateNamespaceSnapshotVars(self):
255255
Typically, used before the NVDA python console is opened, after which, calls
256256
to the 'api' module will refer to this new focus.
257257
"""
258-
try:
259-
caretPos = api.getCaretPosition()
260-
except RuntimeError:
261-
log.debug("Unable to set caretPos snapshot variable for python console.")
262-
caretPos = None
263258

264-
self._namespaceSnapshotVars = {
265-
"focus": api.getFocusObject(),
259+
def _getCaretPos() -> textInfos.TextInfo | None:
260+
try:
261+
return api.getCaretPosition()
262+
except RuntimeError:
263+
log.debug("Unable to set caretPos snapshot variable for python console.")
264+
return None
265+
266+
self._namespaceSnapshotVarsGetters = {
267+
"focus": (lambda: api.getFocusObject()),
266268
# Copy the focus ancestor list, as it gets mutated once it is replaced in api.setFocusObject.
267-
"focusAnc": list(api.getFocusAncestors()),
268-
"fdl": api.getFocusDifferenceLevel(),
269-
"fg": api.getForegroundObject(),
270-
"nav": api.getNavigatorObject(),
271-
"caretObj": api.getCaretObject(),
272-
"caretPos": caretPos,
273-
"review": api.getReviewPosition(),
274-
"mouse": api.getMouseObject(),
275-
"brlRegions": braille.handler.buffer.regions,
269+
"focusAnc": (lambda: list(api.getFocusAncestors())),
270+
"fdl": (lambda: api.getFocusDifferenceLevel()),
271+
"fg": (lambda: api.getForegroundObject()),
272+
"nav": (lambda: api.getNavigatorObject()),
273+
"caretObj": (lambda: api.getCaretObject()),
274+
"caretPos": _getCaretPos,
275+
"review": (lambda: api.getReviewPosition()),
276+
"mouse": (lambda: api.getMouseObject()),
277+
"brlRegions": (lambda: braille.handler.buffer.regions),
276278
}
279+
self._namespaceSnapshotVars = {}
280+
for name, getter in self._namespaceSnapshotVarsGetters.items():
281+
try:
282+
value = getter()
283+
except Exception:
284+
log.error(f"Unable to set {name} snapshot variable for python console.", exc_info=True)
285+
value = None
286+
self._namespaceSnapshotVars[name] = value
277287
self.namespace.update(self._namespaceSnapshotVars)
278288

279289
def removeNamespaceSnapshotVars(self):

0 commit comments

Comments
 (0)