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).
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.