Mercurial > public > mercurial-scm > hg
comparison contrib/check-py3-compat.py @ 41555:ba7eaff26474
check-py3-compat: manually format and print warnings
The warnings mechanism may print to stderr on Python 3. Independent buffering
of stdout and stderr can lead to warnings output not being printed properly.
This commit traps warnings when executing individual files and prints
warnings to stdout so output is deterministic.
Differential Revision: https://phab.mercurial-scm.org/D5845
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 04 Feb 2019 14:38:23 -0800 |
parents | 01417ca7f2e2 |
children | 2372284d9457 |
comparison
equal
deleted
inserted
replaced
41554:01417ca7f2e2 | 41555:ba7eaff26474 |
---|---|
12 import ast | 12 import ast |
13 import importlib | 13 import importlib |
14 import os | 14 import os |
15 import sys | 15 import sys |
16 import traceback | 16 import traceback |
17 import warnings | |
17 | 18 |
18 def check_compat_py2(f): | 19 def check_compat_py2(f): |
19 """Check Python 3 compatibility for a file with Python 2""" | 20 """Check Python 3 compatibility for a file with Python 2""" |
20 with open(f, 'rb') as fh: | 21 with open(f, 'rb') as fh: |
21 content = fh.read() | 22 content = fh.read() |
89 fn = check_compat_py2 | 90 fn = check_compat_py2 |
90 else: | 91 else: |
91 fn = check_compat_py3 | 92 fn = check_compat_py3 |
92 | 93 |
93 for f in sys.argv[1:]: | 94 for f in sys.argv[1:]: |
94 fn(f) | 95 with warnings.catch_warnings(record=True) as warns: |
96 fn(f) | |
97 | |
98 for w in warns: | |
99 print(warnings.formatwarning(w.message, w.category, | |
100 w.filename, w.lineno).rstrip()) | |
95 | 101 |
96 sys.exit(0) | 102 sys.exit(0) |