(msg)
| 33 | pass |
| 34 | |
| 35 | def _formatwarnmsg_impl(msg): |
| 36 | category = msg.category.__name__ |
| 37 | s = f"{msg.filename}:{msg.lineno}: {category}: {msg.message}\n" |
| 38 | |
| 39 | if msg.line is None: |
| 40 | try: |
| 41 | import linecache |
| 42 | line = linecache.getline(msg.filename, msg.lineno) |
| 43 | except Exception: |
| 44 | # When a warning is logged during Python shutdown, linecache |
| 45 | # and the import machinery don't work anymore |
| 46 | line = None |
| 47 | linecache = None |
| 48 | else: |
| 49 | line = msg.line |
| 50 | if line: |
| 51 | line = line.strip() |
| 52 | s += " %s\n" % line |
| 53 | |
| 54 | if msg.source is not None: |
| 55 | try: |
| 56 | import tracemalloc |
| 57 | # Logging a warning should not raise a new exception: |
| 58 | # catch Exception, not only ImportError and RecursionError. |
| 59 | except Exception: |
| 60 | # don't suggest to enable tracemalloc if it's not available |
| 61 | suggest_tracemalloc = False |
| 62 | tb = None |
| 63 | else: |
| 64 | try: |
| 65 | suggest_tracemalloc = not tracemalloc.is_tracing() |
| 66 | tb = tracemalloc.get_object_traceback(msg.source) |
| 67 | except Exception: |
| 68 | # When a warning is logged during Python shutdown, tracemalloc |
| 69 | # and the import machinery don't work anymore |
| 70 | suggest_tracemalloc = False |
| 71 | tb = None |
| 72 | |
| 73 | if tb is not None: |
| 74 | s += 'Object allocated at (most recent call last):\n' |
| 75 | for frame in tb: |
| 76 | s += (' File "%s", lineno %s\n' |
| 77 | % (frame.filename, frame.lineno)) |
| 78 | |
| 79 | try: |
| 80 | if linecache is not None: |
| 81 | line = linecache.getline(frame.filename, frame.lineno) |
| 82 | else: |
| 83 | line = None |
| 84 | except Exception: |
| 85 | line = None |
| 86 | if line: |
| 87 | line = line.strip() |
| 88 | s += ' %s\n' % line |
| 89 | elif suggest_tracemalloc: |
| 90 | s += (f'{category}: Enable tracemalloc to get the object ' |
| 91 | f'allocation traceback\n') |
| 92 | return s |
no test coverage detected