[pypy-commit] pypy stdlib-2.7.3: CPython Issue #12467: warnings: fix crash if globals()['__file__'] is None.

amauryfa noreply at buildbot.pypy.org
Wed Jun 13 08:51:11 CEST 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: stdlib-2.7.3
Changeset: r55638:7f72138882f6
Date: 2012-06-13 08:50 +0200
http://bitbucket.org/pypy/pypy/changeset/7f72138882f6/

Log:	CPython Issue #12467: warnings: fix crash if globals()['__file__']
	is None.

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
@@ -101,9 +101,8 @@
     # setup filename
     try:
         w_filename = space.getitem(w_globals, space.wrap("__file__"))
+        filename = space.str_w(w_filename)
     except OperationError, e:
-        if not e.match(space, space.w_KeyError):
-            raise
         if space.str_w(w_module) == '__main__':
             w_argv = space.sys.getdictvalue(space, 'argv')
             if w_argv and space.len_w(w_argv) > 0:
@@ -116,12 +115,11 @@
         else:
             w_filename = w_module
     else:
-        # if filename.lower().endswith((".pyc", ".pyo"))
-        if space.is_true(space.call_method(
-            w_filename, "endswith",
-            space.newtuple([space.wrap(".pyc"), space.wrap(".pyo")]))):
+        lc_filename = filename.lower()
+        if (lc_filename.endswith(".pyc") or 
+            lc_filename.endswith(".pyo")):
             # strip last character
-            w_filename = space.wrap(space.str_w(w_filename)[:-1])
+            w_filename = space.wrap(filename[:-1])
 
     return (w_filename, lineno, w_module, w_registry)
 
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
@@ -60,3 +60,10 @@
 
         assert result.count('\n') == 2
         assert '  warnings.warn(message, ' in result
+
+    def test_filename_none(self):
+        import _warnings
+        globals()['__file__'] = 'test.pyc'
+        _warnings.warn('test', UserWarning)
+        globals()['__file__'] = None
+        _warnings.warn('test', UserWarning)


More information about the pypy-commit mailing list