New submission from Nick Coghlan <ncogh...@gmail.com>:

While trying to create an example for a pickle bug discussion, I deliberately 
dropped `__main__` out of sys.modules, and the REPL session lost all of its 
runtime state.

Simplified reproducer:


```
>>> import sys
>>> mod = sys.modules[__name__]
>>> sys.modules[__name__] = object()
>>> dir()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'dir' is not defined

```

(Initially encountered on Python 2.7, reproduced on Python 3.7)

If I'd just dropped the reference to `__main__` entirely, that would make sense 
(since modules clear their namespaces when they go away), but I didn't: I saved 
a reference in a local variable first.

So it appears the CPython REPL isn't keeping a strong reference to either 
`__main__` or `__main__.__dict__` between statements, so the cyclic GC kicked 
in and decided the module could be destroyed.

----------
messages: 340516
nosy: ncoghlan
priority: normal
severity: normal
stage: test needed
status: open
title: Dropping __main__ from sys.modules clears the REPL namespace
type: behavior
versions: Python 2.7, Python 3.7, Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36665>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to