[Python-Dev] Garbage announcement printed on interpreter shutdown (original) (raw)

Georg Brandl g.brandl at gmx.net
Fri Sep 10 22:32:25 CEST 2010


Hey #python-dev,

I'd like to ask your opinion on this change; I think it should be reverted or at least made silent by default. Basically, it prints a warning like

  gc: 2 uncollectable objects at shutdown:
      Use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them.

at interpreter shutdown if gc.garbage is nonempty.

IMO this runs contrary to the decision we made when DeprecationWarnings were made silent by default: it spews messages not only at developers, but also at users, who don't need it and probably are going to be quite confused by it, assuming it came from their console application (imagine Mercurial printing this).

Opinions?

Georg

Am 09.08.2010 00:18, schrieb antoine.pitrou:

Author: antoine.pitrou Date: Mon Aug 9 00πŸ”ž46 2010 New Revision: 83861

Log: Issue #477863: Print a warning at shutdown if gc.garbage is not empty.

Modified: python/branches/py3k/Doc/library/gc.rst python/branches/py3k/Doc/whatsnew/3.2.rst python/branches/py3k/Include/pythonrun.h python/branches/py3k/Lib/test/testgc.py python/branches/py3k/Misc/NEWS python/branches/py3k/Modules/gcmodule.c python/branches/py3k/Python/pythonrun.c Modified: python/branches/py3k/Doc/library/gc.rst ============================================================================== --- python/branches/py3k/Doc/library/gc.rst (original) +++ python/branches/py3k/Doc/library/gc.rst Mon Aug 9 00πŸ”ž46 2010 @@ -177,6 +177,15 @@ If :const:DEBUGSAVEALL is set, then all unreachable objects will be added to this list rather than freed. + .. versionchanged:: 3.2 + If this list is non-empty at interpreter shutdown, a warning message + gets printed: + + :: + + gc: 2 uncollectable objects at shutdown: + Use gc.setdebug(gc.DEBUGUNCOLLECTABLE) to list them. + The following constants are provided for use with :func:setdebug: @@ -197,6 +206,9 @@ reachable but cannot be freed by the collector). These objects will be added to the garbage list. + .. versionchanged:: 3.2 + Also print the contents of the :data:garbage list at interpreter + shutdown (rather than just its length), if it isn't empty. .. data:: DEBUGSAVEALL Modified: python/branches/py3k/Doc/whatsnew/3.2.rst ============================================================================== --- python/branches/py3k/Doc/whatsnew/3.2.rst (original) +++ python/branches/py3k/Doc/whatsnew/3.2.rst Mon Aug 9 00πŸ”ž46 2010 @@ -119,6 +119,11 @@ * The :class:ftplib.FTP class now supports the context manager protocol (Contributed by Tarek ZiadΓ© and Giampaolo RodolΓ ; :issue:4972.) +* A warning message will now get printed at interpreter shutdown if + the :data:gc.garbage list isn't empty. This is meant to make the + programmer aware that his code contains object finalization issues. + (Added by Antoine Pitrou; :issue:477863.) + * The :func:shutil.copytree function has two new options: * ignoredanglingsymlinks: when symlinks=False (meaning that the Modified: python/branches/py3k/Include/pythonrun.h ============================================================================== --- python/branches/py3k/Include/pythonrun.h (original) +++ python/branches/py3k/Include/pythonrun.h Mon Aug 9 00πŸ”ž46 2010 @@ -148,6 +148,7 @@ PyAPIFUNC(void) PyByteArrayFini(void); PyAPIFUNC(void) PyFloatFini(void); PyAPIFUNC(void) PyOSFiniInterrupts(void); +PyAPIFUNC(void) PyGCFini(void); /* Stuff with no proper home (yet) */ PyAPIFUNC(char *) PyOSReadline(FILE *, FILE *, char *); Modified: python/branches/py3k/Lib/test/testgc.py ============================================================================== --- python/branches/py3k/Lib/test/testgc.py (original) +++ python/branches/py3k/Lib/test/testgc.py Mon Aug 9 00πŸ”ž46 2010 @@ -1,5 +1,5 @@ import unittest -from test.support import verbose, rununittest +from test.support import verbose, rununittest, strippythonstderr import sys import gc import weakref @@ -466,6 +466,42 @@ # would be damaged, with an empty dict. self.assertEqual(x, None) + def testgarbageatshutdown(self): + import subprocess + code = """if 1: + import gc + class X: + def init(self, name): + self.name = name + def repr(self): + return "<X %%r>" %% self.name + def del(self): + pass + + x = X('first') + x.x = x + x.y = X('second') + del x + if %d: + gc.setdebug(gc.DEBUGUNCOLLECTABLE) + """ + def runcommand(code): + p = subprocess.Popen([sys.executable, "-c", code], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + stdout, stderr = p.communicate() + self.assertEqual(p.returncode, 0) + self.assertEqual(stdout.strip(), b"") + return strippythonstderr(stderr) + + stderr = runcommand(code % 0) + self.assertIn(b"gc: 2 uncollectable objects at shutdown", stderr) + self.assertNotIn(b"[<X 'first'>, <X 'second'>]", stderr) + # With DEBUGUNCOLLECTABLE, the garbage list gets printed + stderr = runcommand(code % 1) + self.assertIn(b"gc: 2 uncollectable objects at shutdown", stderr) + self.assertIn(b"[<X 'first'>, <X 'second'>]", stderr) + class GCTogglingTests(unittest.TestCase): def setUp(self): gc.enable() Modified: python/branches/py3k/Misc/NEWS ============================================================================== --- python/branches/py3k/Misc/NEWS (original) +++ python/branches/py3k/Misc/NEWS Mon Aug 9 00πŸ”ž46 2010 @@ -30,6 +30,8 @@ Extensions ---------- +- Issue #477863: Print a warning at shutdown if gc.garbage is not empty. + - Issue #6869: Fix a refcount problem in the ctypes extension. - Issue #5504: ctypes should now work with systems where mmap can't Modified: python/branches/py3k/Modules/gcmodule.c ============================================================================== --- python/branches/py3k/Modules/gcmodule.c (original) +++ python/branches/py3k/Modules/gcmodule.c Mon Aug 9 00πŸ”ž46 2010 @@ -1295,17 +1295,16 @@ static struct PyModuleDef gcmodule = { PyModuleDefHEADINIT, - "gc", - gc_doc,_ - -1, - GcMethods, - NULL, - NULL, - NULL, - NULL + "gc", /* mname */ + gc_doc, /* mdoc /_ _+ -1, / msize /_ _+ GcMethods, / mmethods /_ _+ NULL, / mreload /_ _+ NULL, / mtraverse /_ _+ NULL, / mclear /_ _+ NULL / mfree */_ }; - PyMODINITFUNC PyInitgc(void) { @@ -1364,6 +1363,37 @@ return n; } +void +PyGCFini(void) +{ + if (garbage != NULL && PyListGETSIZE(garbage) > 0) { + PySysWriteStderr( + "gc: " + "%" PYFORMATSIZET "d uncollectable objects at shutdown:\n", + PyListGETSIZE(garbage) + ); + if (debug & DEBUGUNCOLLECTABLE) { + PyObject *repr = NULL, *bytes = NULL; + repr = PyObjectRepr(garbage); + if (!repr || !(bytes = PyUnicodeEncodeFSDefault(repr))) + PyErrWriteUnraisable(garbage); + else { + PySysWriteStderr( + " %s\n", + PyBytesASSTRING(bytes) + ); + } + PyXDECREF(repr); + PyXDECREF(bytes); + } + else { + PySysWriteStderr( + " Use gc.setdebug(gc.DEBUGUNCOLLECTABLE) to list them.\n" + ); + } + } +} + /* for debugging */ void PyGCDump(PyGCHead *g) Modified: python/branches/py3k/Python/pythonrun.c ============================================================================== --- python/branches/py3k/Python/pythonrun.c (original) +++ python/branches/py3k/Python/pythonrun.c Mon Aug 9 00πŸ”ž46 2010 @@ -404,6 +404,9 @@ while (PyGCCollect() > 0) /* nothing */; #endif + /* We run this while most interpreter state is still alive, so that + debug information can be printed out */ + PyGCFini(); /* Destroy all modules */ PyImportCleanup();


Python-checkins mailing list Python-checkins at python.org http://mail.python.org/mailman/listinfo/python-checkins

-- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.



More information about the Python-Dev mailing list