D5845: check-py3-compat: manually format and print warnings

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Mon Feb 4 22:48:00 UTC 2019


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D5845

AFFECTED FILES
  contrib/check-py3-compat.py

CHANGE DETAILS

diff --git a/contrib/check-py3-compat.py b/contrib/check-py3-compat.py
--- a/contrib/check-py3-compat.py
+++ b/contrib/check-py3-compat.py
@@ -14,6 +14,7 @@
 import os
 import sys
 import traceback
+import warnings
 
 def check_compat_py2(f):
     """Check Python 3 compatibility for a file with Python 2"""
@@ -91,6 +92,11 @@
         fn = check_compat_py3
 
     for f in sys.argv[1:]:
-        fn(f)
+        with warnings.catch_warnings(record=True) as warns:
+            fn(f)
+
+        for w in warns:
+            print(warnings.formatwarning(w.message, w.category,
+                                         w.filename, w.lineno).rstrip())
 
     sys.exit(0)



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list