Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r92996:bc4acc4caa28
Date: 2017-11-12 15:30 +0100
http://bitbucket.org/pypy/pypy/changeset/bc4acc4caa28/

Log:    Issue #2699: test and fixes.

        Note that this includes a fix to the stdlib warnings.py, otherwise
        non-ascii warning messages are usually swallowed. That's a bug in
        CPython, I think.

diff --git a/lib-python/2.7/warnings.py b/lib-python/2.7/warnings.py
--- a/lib-python/2.7/warnings.py
+++ b/lib-python/2.7/warnings.py
@@ -43,11 +43,12 @@
         unicodetype = unicode
     except NameError:
         unicodetype = ()
+    template = "%s: %s: %s\n"
     try:
         message = str(message)
     except UnicodeEncodeError:
-        pass
-    s =  "%s: %s: %s\n" % (lineno, category.__name__, message)
+        template = unicode(template)
+    s = template % (lineno, category.__name__, message)
     line = linecache.getline(filename, lineno) if line is None else line
     if line:
         line = line.strip()
diff --git a/pypy/module/_warnings/interp_warnings.py 
b/pypy/module/_warnings/interp_warnings.py
--- a/pypy/module/_warnings/interp_warnings.py
+++ b/pypy/module/_warnings/interp_warnings.py
@@ -201,9 +201,20 @@
     w_stderr = space.sys.get("stderr")
 
     # Print "filename:lineno: category: text\n"
-    message = "%s:%d: %s: %s\n" % (space.text_w(w_filename), lineno,
-                                   space.text_w(w_name), space.text_w(w_text))
-    space.call_method(w_stderr, "write", space.newtext(message))
+    try:
+        message = "%s:%d: %s: %s\n" % (space.text_w(w_filename), lineno,
+                                       space.text_w(w_name),
+                                       space.text_w(w_text))
+    except OperationError as e:
+        if e.async(space):
+            raise
+        message = u"%s:%d: %s: %s\n" % (space.unicode_w(w_filename), lineno,
+                                        space.unicode_w(w_name),
+                                        space.unicode_w(w_text))
+        w_message = space.newunicode(message)
+    else:
+        w_message = space.newtext(message)
+    space.call_method(w_stderr, "write", w_message)
 
     # Print "  source_line\n"
     if not w_sourceline:
@@ -248,7 +259,7 @@
     if space.isinstance_w(w_message, space.w_Warning):
         w_text = space.str(w_message)
         w_category = space.type(w_message)
-    elif (not space.isinstance_w(w_message, space.w_unicode) or
+    elif (not space.isinstance_w(w_message, space.w_unicode) and
           not space.isinstance_w(w_message, space.w_bytes)):
         w_text = space.str(w_message)
         w_message = space.call_function(w_category, w_message)
diff --git a/pypy/module/_warnings/test/test_warnings.py 
b/pypy/module/_warnings/test/test_warnings.py
--- a/pypy/module/_warnings/test/test_warnings.py
+++ b/pypy/module/_warnings/test/test_warnings.py
@@ -65,3 +65,23 @@
         _warnings.warn('test', UserWarning)
         globals()['__file__'] = None
         _warnings.warn('test', UserWarning)
+
+    def test_warn_unicode(self):
+        import _warnings, sys
+        old = sys.stderr
+        try:
+            class Grab:
+                def write(self, u):
+                    self.data.append(u)
+            sys.stderr = Grab()
+            sys.stderr.data = data = []
+            _warnings.warn_explicit("9238exbexn8", Warning,
+                                    "<string>", 1421, module_globals=globals())
+            assert isinstance(''.join(data), str)
+            _warnings.warn_explicit(u"\u1234\u5678", UserWarning,
+                                    "<str2>", 831, module_globals=globals())
+            assert isinstance(''.join(data), unicode)
+            assert ''.join(data).endswith(
+                             u'<str2>:831: UserWarning: \u1234\u5678\n')
+        finally:
+            sys.stderr = old
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to