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: [2.7] Add PYTHONSHOWREFCOUNT environment variable to Python 2.7
Type: Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, cstratak, ezio.melotti, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2017-10-09 13:44 by vstinner, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3932 merged vstinner, 2017-10-09 13:47
PR 4019 merged vstinner, 2017-10-17 13:55
Messages (5)
msg303952 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-09 13:44
When Python 2.7 is built in debug mode, it displays the total number of references  when the program finishes or after each statement in the interactive interpreter.

Example:

haypo@selma$ ./python
Python 2.7.14+ (heads/2.7:cc4b6f1c62, Oct  9 2017, 15:30:11) 
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+1
2
[45025 refs]
>>> 2+2
4
[45025 refs]
>>> 
[45025 refs]
[21655 refs]

While I'm Python core developer working on reference leaks for years, I *never* used this value. I only use "python -m test -R 3:3 test_xxx" to track reference leaks.

I really liked Python 3.4 which made this option an opt-in, python3 -X showrefcount: bpo-17323 (commit 1f8898a5916b942c86ee8716c37d2db388e7bf2f).

I propose to change Python 2.7 behaviour to not print this value by default, but add a new PYTHONSHOWREFCOUNT environment variable to display it. The value is displayed if PYTHONSHOWREFCOUNT is set. Other similar existing variables:

* PYTHONTHREADDEBUG
* PYTHONDUMPREFS
* PYTHONMALLOCSTATS

https://docs.python.org/2.7/using/cmdline.html#debug-mode-variables

I prefer to add a new environment style rather than adding a new -X command line option. IMHO an environment variable is less intrusive. For example, older Python 2.7 versions completely ignore unknown environment variables, whereas "python2.7 -X showrefcount ..." currently logs the "-X is reserved for implementation-specific arguments" message into stderr.

Attached PR adds PYTHONSHOWREFCOUNT.
msg303962 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-09 14:27
Since this issue is on the thin border between "feature" and "bug fix", I would like to get the feedback of Benjamin on this change.

@Benjamin: Are you ok to make such change late in the Python 2.7 cycle, in Python 2.7.15?
msg304491 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-17 08:35
New changeset 3c082a7fdb472f02bcac7a7f8fe1e3a34a11b70b by Victor Stinner in branch '2.7':
bpo-31733: Add PYTHONSHOWREFCOUNT env var (GH-3932)
https://github.com/python/cpython/commit/3c082a7fdb472f02bcac7a7f8fe1e3a34a11b70b
msg304496 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-17 10:03
Python 2.7.15 now requires to set PYTHONSHOWREFCOUNT env var to dump "[xxx refs]".
msg304526 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-10-17 20:13
New changeset 355393e7438deeab4aeec3fcffea65e8cada083b by Victor Stinner in branch '2.7':
[2.7] bpo-31733, bpo-31692: Document 2 new env vars in What's New in Python 2.7 (GH-4019)
https://github.com/python/cpython/commit/355393e7438deeab4aeec3fcffea65e8cada083b
History
Date User Action Args
2022-04-11 14:58:53adminsetgithub: 75914
2017-10-17 20:13:15vstinnersetmessages: + msg304526
2017-10-17 13:55:57vstinnersetpull_requests: + pull_request3993
2017-10-17 10:03:43vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg304496

stage: resolved
2017-10-17 08:35:25vstinnersetmessages: + msg304491
2017-10-10 16:21:18cstrataksetnosy: + cstratak
2017-10-09 14:27:38vstinnersetmessages: + msg303962
2017-10-09 13:47:56vstinnersetnosy: + benjamin.peterson, ezio.melotti, serhiy.storchaka

stage: patch review -> (no value)
2017-10-09 13:47:29vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request3907
2017-10-09 13:44:19vstinnercreate