Repository navigation
Create the Python shell view's control, and unwind it safely - #639
Conversation
Pyface 8.0 split widget construction in two: PythonShell(parent) leaves
'control' at None until create() is called. 'create_control' never made
that second call, so it went on to read 'self.namespace' off a shell with
no interpreter and died with
AttributeError: 'NoneType' object has no attribute 'interpreter'
which is to say the view has not opened at all under any Pyface since 8.0.
Call create() when the control is not already there, so this keeps working
against a Pyface that still builds it in the constructor.
What the user actually sees is a different error, from the clean-up:
AttributeError: 'PythonShellView' object has no attribute '_service_id'
'create_control' assigns '_service_id' on its last line, and Pyface's
WorkbenchWindowLayout.add_view calls 'destroy_control' from the except
branch that handles a 'create_control' which raised -- so undoing what
never happened replaced the error explaining the failure with an error
from the clean-up after it. Give '_service_id' a trait default and skip
what was never done; same for the sys.stdout redirect, which a part-way
'create_control' does leave behind.
These are the plugin's first tests, and the first anywhere in envisage to
import pyface.workbench, whose import-time PendingDeprecationWarning the
pytest configuration has to allow now that warnings are errors.
Reported downstream as enthought/mayavi#1409.
There was a problem hiding this comment.
Pull request overview
Fixes the Envisage Python shell view so it can reliably create its toolkit control under newer Pyface versions (where widget construction is split) and so teardown is safe even when create_control fails part-way through. Adds regression tests for both successful creation and “cleanup-after-failure” scenarios, and updates pytest warning filters to accommodate Pyface import-time warnings.
Changes:
- Ensure
PythonShell.create()is called whenPythonShell(parent)does not immediately create acontrol. - Make
destroy_controlresilient whencreate_controldidn’t complete (service unregister / stdout restoration become conditional). - Add GUI-gated tests for creation and safe/repeatable teardown; adjust
filterwarningsto ignore a Pyface PendingDeprecationWarning.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/envisage/plugins/python_shell/view/python_shell_view.py |
Creates the shell control for Pyface 8+ and makes teardown robust to partial initialization. |
src/envisage/plugins/python_shell/tests/test_python_shell_view.py |
Adds regression tests covering control creation and safe teardown, including repeatability. |
src/envisage/plugins/python_shell/tests/__init__.py |
Adds package marker (and standard header) for the new test package. |
pyproject.toml |
Ignores an import-time Pyface PendingDeprecationWarning under warnings-as-errors testing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| view = PythonShellView(window=create_window()) | ||
| view.create_control(None) | ||
| view.destroy_control() | ||
| stdout = sys.stdout | ||
|
|
||
| view.destroy_control() | ||
|
|
||
| self.assertIs(sys.stdout, stdout) |
| control = view.create_control(None) | ||
| try: | ||
| self.assertIsNotNone(control) | ||
| self.assertIsNotNone( | ||
| view.window.application.get_service(IPythonShell) | ||
| ) | ||
| # reaches the interpreter behind the 'namespace' property | ||
| view.bind("answer", 42) | ||
| self.assertEqual(view.namespace["answer"], 42) | ||
| finally: | ||
| view.destroy_control() | ||
|
|
|
Thanks, @larsoner! Looks fine in principle. A few small things:
Let me see if I can whip up a quick fix for the Also worth noting: I'm about to merge #638, which might cause this PR to need a reformat pass. |
Closes #640. Pyface treats `PythonShell` and `PythonEditor` as optional imports gated on Pygments, and `envisage.plugins.python_shell.view.python_shell_view` imports `PythonShell` from `pyface.api` at module level. So the python_shell plugin needs Pygments, and nothing in Envisage said so: the `gui` dependency group installs only `pyside6`, and `EDM_PACKAGES` installs `apptools configobj pyface pytest traits traitsui`. The PyPI CI jobs have Pygments only because pytest 9 depends on it. EDM ships pytest 7.4.1, which doesn't, so the EDM environments have none — which is how #639 hit `ImportError: cannot import name 'PythonShell' from 'pyface.api'` at collection time in the EDM pyside6 jobs. Add Pygments to the `gui` dependency group and to `EDM_PACKAGES`, and have `requires_gui` skip with a message that says so when it's missing, rather than leaving the import to fail. Only a couple of GUI tests need Pygments, but telling them apart isn't worth a second decorator. `edm search` reports `pygments 2.17.2-1` in `enthought/free` for Python 3.11 on `osx_arm64`, `rh8-x86_64` and `win-x86_64`, so all the EDM jobs can get it. *This PR was created with the assistance of an AI coding agent.* --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
#641 (merged) ensures that pygments is present in the test environment for the GUI-using tests, and that a test marked with There's a catch, though - for the one non-gui-using test job, the new test module may still fail at import time, in which case we'd need a local import. |
The redirect create_control installs over sys.stdout is precisely what a create_control that raises part way leaves behind, so hanging the tear-down off the test reaching its own last line is the one thing these tests must not do -- a failure inside create_control would leak the redirect into every test after it. Move it to addCleanup, which runs either way. Registered LIFO, so destroy_control gets its own go at restoring stdout first and the setattr is only there for the case where it could not. Checked rather than assumed: a test that raises immediately after create_control leaves sys.stdout redirected for the next test without this, and does not with it. It takes `pytest -s` to see either way -- pytest's per-test capture swaps sys.stdout itself and puts its own back afterwards, which hides the leak from the suite as it normally runs.
Looks like it did. Sorry about that. :-( |
main replaced black, isort and flake8 with ruff (enthought#637) and reformatted with it (enthought#642) after this branch was cut, so the two files it touches were still formatted to black's 79 columns. Run ruff over them. main also documented the changelog conventions (enthought#644), so add the entries this PR owes: the fix under Fixes, and the plugin's first tests under Tests, beside the Pygments dependency (enthought#641) they turned up.
|
Done @mdickinson ! |
|
Thanks, @larsoner! Getting a second local review from Opus, then assuming that turns up nothing blocking I'll merge. |
The four test packages already in the tree are all zero bytes, and ruff's CPY001 only asks for the header in a file that has something else in it.
|
Addressed my own |
'sys.stdout' is None under pythonw.exe, so 'original_stdout' could not carry both the saved value and the fact that 'create_control' got as far as saving it. The redirect then outlived the view that installed it, and the next view restored that dead writer in place of the real stdout. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
@larsoner This fix has now been shipped in Envisage 8.0.1. |
|
Awesome, thanks! |
|
Many thanks @larsoner and @mdickinson! Do you have an idea of when this would be in a new envisage release? Is there a planned release in the near future? Thanks. |
|
@mdickinson -- pardon the noise, I just saw the release! |
The "setuptools<82" pin was there because envisage.api imported pkg_resources, which setuptools 82 removed; envisage 8.0.0 dropped that import, so requiring it retires the pin, the two warning filters that went with it, and the half of the test_mayavi2 guard that asked whether pkg_resources was importable -- which would otherwise have skipped those 18 tests in silence the moment the pin went. 8.0.1 rather than 8.0.0 because it also carries the Python shell view fix (enthought/envisage#639). Nothing else mayavi imports wants pkg_resources: of the ETS stack only traits' and traitsui's etsdemo entry points still do. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@prabhuramachandran mentioned a bug to me:
I had Claude Opus 5 look into it and below was the conclusion it wrote out. Happy to iterate on the PR and own/vouch for these changes, even if I'm a little out of my depth a bit here -- the associated workaround in enthought/mayavi#1414 does seem to work!
Pyface 8.0 split widget construction in two: PythonShell(parent) leaves 'control' at None until create() is called. 'create_control' never made that second call, so it went on to read 'self.namespace' off a shell with no interpreter and died with
which is to say the view has not opened at all under any Pyface since 8.0. Call create() when the control is not already there, so this keeps working against a Pyface that still builds it in the constructor.
What the user actually sees is a different error, from the clean-up:
'create_control' assigns '_service_id' on its last line, and Pyface's WorkbenchWindowLayout.add_view calls 'destroy_control' from the except branch that handles a 'create_control' which raised -- so undoing what never happened replaced the error explaining the failure with an error from the clean-up after it. Give '_service_id' a trait default and skip what was never done; same for the sys.stdout redirect, which a part-way 'create_control' does leave behind.
These are the plugin's first tests, and the first anywhere in envisage to import pyface.workbench, whose import-time PendingDeprecationWarning the pytest configuration has to allow now that warnings are errors.
Reported downstream as enthought/mayavi#1409.