This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Console interpreter holds a hard link on last displayed object
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, gpcracker
Priority: normal Keywords:

Created on 2015-09-29 19:33 by gpcracker, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg251879 - (view) Author: Vladislav Ignatenko (gpcracker) Date: 2015-09-29 19:34
Hi everyone,
I've faced with python interpreter behavior I didn't expected when I was using code.InteractiveConsole in my remote python console.
I have also detected this behavior on local python interpreter.

Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> a = 'test string A'
>>> b = 'test string B'
>>> print sys.getrefcount(a) - 1, sys.getrefcount(b) - 1
1 1
>>> a
'test string A'
>>> print sys.getrefcount(a) - 1, sys.getrefcount(b) - 1
2 1
>>> b
'test string B'
>>> print sys.getrefcount(a) - 1, sys.getrefcount(b) - 1
1 2
>>> print a, b
test string A test string B
>>> print sys.getrefcount(a) - 1, sys.getrefcount(b) - 1
1 2
>>> (a, b)
('test string A', 'test string B')
>>> print sys.getrefcount(a) - 1, sys.getrefcount(b) - 1
2 2
>>> sys
<module 'sys' (built-in)>
>>> print sys.getrefcount(a) - 1, sys.getrefcount(b) - 1
1 1

When I used python interactive console to display variable value on it's own (no print usage), it holds a hard link on last object, blocking it's destruction sometimes, if there were no other links to this object. When user perform an 'eval' calculation, result is displayed, and interpreter holds a hard link to it, however user can not access it.

1. Is this behavior normal?
2. Is there any way to clean this link directly?
msg251881 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2015-09-29 19:55
In interactive mode the extra reference is held by the "_" built-in variable. 

    >>> a
    'test string A'
    >>> del __builtin__._; sys.getrefcount(a) - 1
    1

Look at the following disassembled code, compiled in the 'single' interactive mode:

    >>> dis.dis(compile('a', '', 'single'))
      1           0 LOAD_NAME                0 (a)
                  3 PRINT_EXPR          
                  4 LOAD_CONST               0 (None)
                  7 RETURN_VALUE  

[PRINT_EXPR][1] calls [sys.displayhook][2]. The default display hook prints a non-None value and sets it as __builtin__._. You can replace this with your own display hook. The original function is always available as sys.__displayhook__.

[1]: https://hg.python.org/cpython/file/v2.7.10/Python/ceval.c#l1727
[2]: https://docs.python.org/2/library/sys.html#sys.displayhook
History
Date User Action Args
2022-04-11 14:58:21adminsetgithub: 69460
2015-09-29 19:55:47eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg251881

resolution: not a bug
stage: resolved
2015-09-29 19:34:21gpcrackersetmessages: + msg251879
2015-09-29 19:33:40gpcrackercreate