[pypy-svn] pypy fast-forward: warnings.warn can still display the source line even when
amauryfa
commits-noreply at bitbucket.org
Tue Jan 4 17:03:44 CET 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40386:9c14c6964756
Date: 2011-01-04 16:46 +0100
http://bitbucket.org/pypy/pypy/changeset/9c14c6964756/
Log: warnings.warn can still display the source line even when
showarning() is missing
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
@@ -43,5 +43,20 @@
assert len(w) == 0
warnings.defaultaction = 'default'
+ def test_show_source_line(self):
+ import warnings
+ import sys, StringIO
+ from test.warning_tests import inner
+ # With showarning() missing, make sure that output is okay.
+ del warnings.showwarning
+ stderr = sys.stderr
+ try:
+ sys.stderr = StringIO.StringIO()
+ inner('test message')
+ result = sys.stderr.getvalue()
+ finally:
+ sys.stderr = stderr
+ assert result.count('\n') == 2
+ assert ' warnings.warn(message, ' in result
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
@@ -211,15 +211,31 @@
space.call_method(w_stderr, "write", space.wrap(message))
# Print " source_line\n"
- if w_sourceline:
- line = space.str_w(w_sourceline)
- message = "\n"
- for i in range(len(line)):
- c = line[i]
- if c not in ' \t\014':
- message = " %s\n" % (line[i:],)
- break
- space.call_method(w_stderr, "write", space.wrap(message))
+ if not w_sourceline:
+ try:
+ # sourceline = linecache.getline(filename, lineno).strip()
+ w_builtins = space.getbuiltinmodule('__builtin__')
+ w_linecachemodule = space.call_method(w_builtins, '__import__',
+ space.wrap("linecache"))
+ w_sourceline = space.call_method(w_linecachemodule, "getline",
+ w_filename, space.wrap(lineno))
+ w_sourceline = space.call_method(w_sourceline, "strip")
+ except OperationError:
+ w_sourceline = None
+
+ if not w_sourceline:
+ return
+ line = space.str_w(w_sourceline)
+ if not line:
+ return
+
+ message = "\n"
+ for i in range(len(line)):
+ c = line[i]
+ if c not in ' \t\014':
+ message = " %s\n" % (line[i:],)
+ break
+ space.call_method(w_stderr, "write", space.wrap(message))
def do_warn(space, w_message, w_category, stacklevel):
context_w = setup_context(space, stacklevel)
More information about the Pypy-commit
mailing list