]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merge 3.5
authorVictor Stinner <victor.stinner@gmail.com>
Thu, 24 Mar 2016 23:33:12 +0000 (00:33 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Thu, 24 Mar 2016 23:33:12 +0000 (00:33 +0100)
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.

1  2 
Lib/test/test_warnings/__init__.py
Lib/warnings.py
Misc/NEWS

Simple merge
diff --cc Lib/warnings.py
index d4f591ee71d85744442da571551663378b88161d,cf9f5b282acdd66884994b377ec397bd91b96dd3..1ece5149f4be57dd14e3588aebdc95fc63be86b6
@@@ -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
-         import tracemalloc
-         tb = tracemalloc.get_object_traceback(msg.source)
++
 +    if msg.source is not None:
-                 line = linecache.getline(frame.filename, frame.lineno)
++        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))
++
++                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 70ff3de8eb7d5676f42bb20eeff7d5dcc5ccbc9a,3baeeecae9dd2484de58eb5fd72229b3475389b8..f4837356191238fa5a1f781ab19ab76ed3aae86b
+++ 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.