From: Victor Stinner Date: Thu, 24 Mar 2016 23:33:12 +0000 (+0100) Subject: Merge 3.5 X-Git-Tag: v3.6.0a1~346 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e091d32a7ac514a415161043c4a70e1765363c5a;p=thirdparty%2FPython%2Fcpython.git Merge 3.5 Issue #21925: warnings.formatwarning() now catches exceptions when calling linecache.getline() and tracemalloc.get_object_traceback() to be able to log ResourceWarning emitted late during the Python shutdown process. --- e091d32a7ac514a415161043c4a70e1765363c5a diff --cc Lib/warnings.py index d4f591ee71d8,cf9f5b282acd..1ece5149f4be --- a/Lib/warnings.py +++ b/Lib/warnings.py @@@ -22,71 -12,29 +22,92 @@@ def _showwarnmsg_impl(msg) if file is None: file = sys.stderr if file is None: - # sys.stderr is None when run with pythonw.exe - warnings get lost + # sys.stderr is None when run with pythonw.exe: + # warnings get lost return + text = _formatwarnmsg(msg) try: - file.write(formatwarning(message, category, filename, lineno, line)) + file.write(text) except OSError: - pass # the file (probably stderr) is invalid - this warning gets lost. + # the file (probably stderr) is invalid - this warning gets lost. + pass -def formatwarning(message, category, filename, lineno, line=None): - """Function to format a warning the standard way.""" - s = "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message) - if line is None: +def _formatwarnmsg_impl(msg): - import linecache + s = ("%s:%s: %s: %s\n" + % (msg.filename, msg.lineno, msg.category.__name__, + msg.message)) ++ + if msg.line is None: - line = linecache.getline(msg.filename, msg.lineno) + try: + import linecache - line = linecache.getline(filename, lineno) ++ line = linecache.getline(msg.filename, msg.lineno) + except Exception: + # When a warning is logged during Python shutdown, linecache + # and the improt machinery don't work anymore + line = None ++ linecache = None + else: + line = msg.line if line: line = line.strip() s += " %s\n" % line ++ + if msg.source is not None: - import tracemalloc - tb = tracemalloc.get_object_traceback(msg.source) ++ try: ++ import tracemalloc ++ tb = tracemalloc.get_object_traceback(msg.source) ++ except Exception: ++ # When a warning is logged during Python shutdown, tracemalloc ++ # and the import machinery don't work anymore ++ tb = None ++ + if tb is not None: + s += 'Object allocated at (most recent call first):\n' + for frame in tb: + s += (' File "%s", lineno %s\n' + % (frame.filename, frame.lineno)) - line = linecache.getline(frame.filename, frame.lineno) ++ ++ try: ++ if linecache is not None: ++ line = linecache.getline(frame.filename, frame.lineno) ++ else: ++ line = None ++ except Exception: ++ line = None + if line: + line = line.strip() + s += ' %s\n' % line return s +# Keep a reference to check if the function was replaced +_showwarning = showwarning + +def _showwarnmsg(msg): + """Hook to write a warning to a file; replace if you like.""" + showwarning = globals().get('showwarning', _showwarning) + if showwarning is not _showwarning: + # warnings.showwarning() was replaced + if not callable(showwarning): + raise TypeError("warnings.showwarning() must be set to a " + "function or method") + + showwarning(msg.message, msg.category, msg.filename, msg.lineno, + msg.file, msg.line) + return + _showwarnmsg_impl(msg) + +# Keep a reference to check if the function was replaced +_formatwarning = formatwarning + +def _formatwarnmsg(msg): + """Function to format a warning the standard way.""" + formatwarning = globals().get('formatwarning', _formatwarning) + if formatwarning is not _formatwarning: + # warnings.formatwarning() was replaced + return formatwarning(msg.message, msg.category, + msg.filename, msg.lineno, line=msg.line) + return _formatwarnmsg_impl(msg) + def filterwarnings(action, message="", category=Warning, module="", lineno=0, append=False): """Insert an entry into the list of warnings filters (at the front). diff --cc Misc/NEWS index 70ff3de8eb7d,3baeeecae9dd..f48373561912 --- a/Misc/NEWS +++ b/Misc/NEWS @@@ -232,17 -93,10 +232,22 @@@ Core and Builtin Library ------- -- Issue #21925: :func:`warnings.formatwarning` now catches exceptions on - ``linecache.getline(...)`` to be able to log :exc:`ResourceWarning` emitted - late during the Python shutdown process. ++- Issue #21925: :func:`warnings.formatwarning` now catches exceptions when ++ calling :func;`linecache.getline` and ++ :func:`tracemalloc.get_object_traceback` to be able to log ++ :exc:`ResourceWarning` emitted late during the Python shutdown process. ++ +- Issue #23848: On Windows, faulthandler.enable() now also installs an + exception handler to dump the traceback of all Python threads on any Windows + exception, not only on UNIX signals (SIGSEGV, SIGFPE, SIGABRT). + +- Issue #26530: Add C functions :c:func:`_PyTraceMalloc_Track` and + :c:func:`_PyTraceMalloc_Untrack` to track memory blocks using the + :mod:`tracemalloc` module. Add :c:func:`_PyTraceMalloc_GetTraceback` to get + the traceback of an object. + +- Issue #26588: The _tracemalloc now supports tracing memory allocations of + multiple address spaces (domains). - Issue #24266: Ctrl+C during Readline history search now cancels the search mode when compiled with Readline 7.