Add a new PYTHONSHOWREFCOUNT environment variable which when set

prints the number of references when using the debug build.
This commit is contained in:
Charalampos Stratakis 2017-11-02 16:33:52 +01:00
parent a8ced1c5b0
commit 1279ca3068
3 changed files with 127 additions and 12 deletions

View File

@ -59,6 +59,28 @@ index f00f7f6026a..55bc12893d6 100644
+ dump allocations counts into stderr on shutdown.
+
+ .. versionadded:: 2.7.15
diff --git a/Doc/whatsnew/2.7.rst b/Doc/whatsnew/2.7.rst
index 28a8d4b..f0d2428 100644
--- a/Doc/whatsnew/2.7.rst
+++ b/Doc/whatsnew/2.7.rst
@@ -2540,6 +2540,17 @@ exemption allowing new ``-3`` warnings to be added in any Python 2.7
maintenance release.
+Two new environment variables for debug mode
+--------------------------------------------
+
+When Python is compiled with ``COUNT_ALLOC`` defined, allocation counts are no
+longer dumped by default anymore: the :envvar:`PYTHONSHOWALLOCCOUNT` environment
+variable must now also be set. Moreover, allocation counts are now dumped into
+stderr, rather than stdout. (Contributed by Victor Stinner; :issue:`31692`.)
+
+.. versionadded:: 2.7.15
+
+
PEP 434: IDLE Enhancement Exception for All Branches
----------------------------------------------------
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 25df3ed0c41..d14a6620b5d 100644
--- a/Lib/test/support/__init__.py
@ -145,16 +167,6 @@ index 415d5ebbd72..418481dadd8 100644
def test_callback_in_cycle_resurrection(self):
import gc
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-10-09-11-03-13.bpo-31692.5-bpdk.rst b/Misc/NEWS.d/next/Core and Builtins/2017-10-09-11-03-13.bpo-31692.5-bpdk.rst
new file mode 100644
index 00000000000..f32548c67bd
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-10-09-11-03-13.bpo-31692.5-bpdk.rst
@@ -0,0 +1,4 @@
+Add a new PYTHONSHOWALLOCCOUNT environment variable. When Python is compiled
+with COUNT_ALLOCS, PYTHONSHOWALLOCCOUNT now has to be set to dump allocation
+counts into stderr on shutdown. Moreover, allocations statistics are now dumped
+into stderr rather than stdout.
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 677f6e48111..44fe13d2f7d 100644
--- a/Python/pythonrun.c

View File

@ -0,0 +1,92 @@
diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst
index 55bc128..15d5830 100644
--- a/Doc/using/cmdline.rst
+++ b/Doc/using/cmdline.rst
@@ -664,6 +664,13 @@ if Python was configured with the ``--with-pydebug`` build option.
If set, Python will print memory allocation statistics every time a new
object arena is created, and on shutdown.
+.. envvar:: PYTHONSHOWREFCOUNT
+
+ If set, Python will print the total reference count when the program
+ finishes or after each statement in the interactive interpreter.
+
+ .. versionadded:: 2.7.15
+
.. envvar:: PYTHONSHOWALLOCCOUNT
If set and Python was compiled with ``COUNT_ALLOCS`` defined, Python will
diff --git a/Doc/whatsnew/2.7.rst b/Doc/whatsnew/2.7.rst
index f0d2428..b29593a 100644
--- a/Doc/whatsnew/2.7.rst
+++ b/Doc/whatsnew/2.7.rst
@@ -2548,6 +2548,10 @@ longer dumped by default anymore: the :envvar:`PYTHONSHOWALLOCCOUNT` environment
variable must now also be set. Moreover, allocation counts are now dumped into
stderr, rather than stdout. (Contributed by Victor Stinner; :issue:`31692`.)
+In debug mode, the ``[xxx refs]`` statistic is not written by default, the
+:envvar:`PYTHONSHOWREFCOUNT` environment variable now must also be set.
+(Contributed by Victor Stinner; :issue:`31733`.)
+
.. versionadded:: 2.7.15
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index d17f7f3..eb31e34 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -37,14 +37,6 @@
#include "windows.h"
#endif
-#ifndef Py_REF_DEBUG
-#define PRINT_TOTAL_REFS()
-#else /* Py_REF_DEBUG */
-#define PRINT_TOTAL_REFS() fprintf(stderr, \
- "[%" PY_FORMAT_SIZE_T "d refs]\n", \
- _Py_GetRefTotal())
-#endif
-
#ifdef __cplusplus
extern "C" {
#endif
@@ -104,6 +96,21 @@ PyModule_GetWarningsModule(void)
return PyImport_ImportModule("warnings");
}
+static void
+_PyDebug_PrintTotalRefs(void)
+{
+#ifdef Py_REF_DEBUG
+ Py_ssize_t total;
+
+ if (!Py_GETENV("PYTHONSHOWREFCOUNT")) {
+ return;
+ }
+
+ total = _Py_GetRefTotal();
+ fprintf(stderr, "[%" PY_FORMAT_SIZE_T "d refs]\n", total);
+#endif
+}
+
static int initialized = 0;
/* API to access the initialized flag -- useful for esoteric use */
@@ -486,7 +493,7 @@ Py_Finalize(void)
}
#endif
- PRINT_TOTAL_REFS();
+ _PyDebug_PrintTotalRefs();
#ifdef Py_TRACE_REFS
/* Display all objects still alive -- this can invoke arbitrary
@@ -777,7 +784,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flag
}
for (;;) {
ret = PyRun_InteractiveOneFlags(fp, filename, flags);
- PRINT_TOTAL_REFS();
+ _PyDebug_PrintTotalRefs();
if (ret == E_EOF)
return 0;
/*

View File

@ -104,7 +104,7 @@ Summary: An interpreted, interactive, object-oriented programming language
Name: %{python}
# Remember to also rebase python-docs when changing this:
Version: 2.7.14
Release: 1%{?dist}
Release: 2%{?dist}
License: Python
Group: Development/Languages
Requires: %{python}-libs%{?_isa} = %{version}-%{release}
@ -726,6 +726,12 @@ Patch280: 00280-Fix-test_regrtest-test_crashed-on-s390x.patch
# https://bugs.python.org/issue31692
Patch283: 00283-fix-tests_with_COUNT_ALLOCS.patch
# 00284 #
# Add a new PYTHONSHOWREFCOUNT environment variable. In debug mode, Python now
# will print the total reference count if PYTHONSHOWREFCOUNT is set.
# Backported from upstream: https://bugs.python.org/issue31733
Patch284: 00284-add-PYTHONSHOWREFCOUNT-env-var.patch
# (New patches go here ^^^)
#
# When adding new patches to "python2" and "python3" in Fedora, EL, etc.,
@ -1031,8 +1037,9 @@ mv Modules/cryptmodule.c Modules/_cryptmodule.c
%if 0%{with_rewheel}
%patch198 -p1
%endif
%patch283 -p1
%patch280 -p1
%patch283 -p1
%patch284 -p1
%if 0%{?_module_build}
@ -1908,6 +1915,10 @@ rm -fr %{buildroot}
# ======================================================
%changelog
* Thu Nov 02 2017 Charalampos Stratakis <cstratak@redhat.com> - 2.7.14-2
- Add a new PYTHONSHOWREFCOUNT environment variable for printing the reference
count in debug builds.
* Mon Oct 09 2017 Iryna Shcherbina <ishcherb@redhat.com> - 2.7.14-1
- Update to version 2.7.14