Skip to content

Create the Python shell view's control, and unwind it safely - #639

Merged
mdickinson merged 7 commits into
enthought:mainfrom
larsoner:fix
Aug 7, 2026
Merged

mdickinson merged 7 commits into
enthought:mainfrom
larsoner:fix

Conversation

@larsoner

@larsoner larsoner commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

@prabhuramachandran mentioned a bug to me:

If I enable the "View->Other->Python", I get this:

  ETS/envisage/envisage/plugins/python_shell/view/python_shell_view.py", line 146, in destroy_control
    self.window.application.unregister_service(self._service_id)
                                               ^^^^^^^^^^^^^^^^
AttributeError: 'PythonShellView' object has no attribute '_service_id'

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

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.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 when PythonShell(parent) does not immediately create a control.
  • Make destroy_control resilient when create_control didn’t complete (service unregister / stdout restoration become conditional).
  • Add GUI-gated tests for creation and safe/repeatable teardown; adjust filterwarnings to 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.

Comment on lines +69 to +76
view = PythonShellView(window=create_window())
view.create_control(None)
view.destroy_control()
stdout = sys.stdout

view.destroy_control()

self.assertIs(sys.stdout, stdout)
Comment on lines +43 to +54
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()

@mdickinson

Copy link
Copy Markdown
Member

Thanks, @larsoner! Looks fine in principle. A few small things:

  • it's fine for the new test-subpackage __init__.py to be empty - no need for a copyright header in what would otherwise be an empty file
  • EDM tests are failing because PythonShell depends on pygments. The non-EDM tests are passing only accidentally, because the version of pytest used for those tests pulls in pygments.
  • Claude pointed out that the original stdout might genuinely be None, in which case the restoration in destroy_control would now be bypassed. Not sure what the cleanest way to fix that is. Claude suggested an extra boolean flag.
  • Copilot has a reasonable point about cleanup - it would be good to be sure that the new tests can't leave global state changed (on either pass or failure).

Let me see if I can whip up a quick fix for the pygments dependency in a separate PR.

Also worth noting: I'm about to merge #638, which might cause this PR to need a reformat pass.

mdickinson added a commit that referenced this pull request Aug 7, 2026
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>
@mdickinson

Copy link
Copy Markdown
Member

Let me see if I can whip up a quick fix for the pygments dependency in a separate PR.

#641 (merged) ensures that pygments is present in the test environment for the GUI-using tests, and that a test marked with @requires_gui is skipped if pygments isn't available.

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.
@mdickinson

Copy link
Copy Markdown
Member

I'm about to merge #638, which might cause this PR to need a reformat pass.

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.
@larsoner

larsoner commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Done @mdickinson !

@mdickinson

Copy link
Copy Markdown
Member

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.
@mdickinson

Copy link
Copy Markdown
Member

Addressed my own __init__.py nitpick in 9faadbb. I do still want to take a second look at the original_stdout issue; doing that shortly.

mdickinson and others added 2 commits August 7, 2026 15:07
'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>
@mdickinson
mdickinson merged commit a0295ec into enthought:main Aug 7, 2026
26 checks passed
@mdickinson mdickinson mentioned this pull request Aug 7, 2026
4 tasks
@mdickinson

Copy link
Copy Markdown
Member

@larsoner This fix has now been shipped in Envisage 8.0.1.

@larsoner
larsoner deleted the fix branch August 8, 2026 14:30
@larsoner

larsoner commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Awesome, thanks!

@prabhuramachandran

Copy link
Copy Markdown
Member

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.

@prabhuramachandran

Copy link
Copy Markdown
Member

@mdickinson -- pardon the noise, I just saw the release!

larsoner added a commit to larsoner/mayavi that referenced this pull request Aug 14, 2026
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>
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.

4 participants