[py-svn] commit/py: hpk42: normalize getfslineno to always return (string, int) tuple

Bitbucket commits-noreply at bitbucket.org
Wed Dec 14 11:53:29 CET 2011


1 new commit in py:


https://bitbucket.org/hpk42/py/changeset/f14a0bda45eb/
changeset:   f14a0bda45eb
user:        hpk42
date:        2011-12-14 11:53:01
summary:     normalize getfslineno to always return (string, int) tuple
affected #:  5 files

diff -r 44cff93787d68267a2d2cb3f3f6c45a45ee352d5 -r f14a0bda45eb4e0891fb89b65ed72fe8a7b62471 CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -14,6 +14,8 @@
   about the eval magic of a decorator library
 - iniconfig: add support for ; as comment starter
 - properly handle lists in xmlgen on python3
+- normalize py.code.getfslineno(obj) to always return a (string, int) tuple
+  defaulting to ("", -1) respectively if no source code can be found for obj.
 
 Changes between 1.4.4 and 1.4.5
 ==================================================


diff -r 44cff93787d68267a2d2cb3f3f6c45a45ee352d5 -r f14a0bda45eb4e0891fb89b65ed72fe8a7b62471 py/__init__.py
--- a/py/__init__.py
+++ b/py/__init__.py
@@ -8,7 +8,7 @@
 
 (c) Holger Krekel and others, 2004-2010
 """
-__version__ = '1.4.6.dev5'
+__version__ = '1.4.6.dev6'
 
 from py import _apipkg
 


diff -r 44cff93787d68267a2d2cb3f3f6c45a45ee352d5 -r f14a0bda45eb4e0891fb89b65ed72fe8a7b62471 py/_code/source.py
--- a/py/_code/source.py
+++ b/py/_code/source.py
@@ -261,6 +261,9 @@
 
 
 def getfslineno(obj):
+    """ Return source location (path, lineno) for the given object.
+    If the source cannot be determined return ("", -1)
+    """
     try:
         code = py.code.Code(obj)
     except TypeError:
@@ -268,19 +271,19 @@
             fn = (py.std.inspect.getsourcefile(obj) or
                   py.std.inspect.getfile(obj))
         except TypeError:
-            return None, None
+            return "", -1
 
         fspath = fn and py.path.local(fn) or None
+        lineno = -1
         if fspath:
             try:
                 _, lineno = findsource(obj)
             except IOError:
-                lineno = None
-        else:
-            lineno = None
+                pass
     else:
         fspath = code.path
         lineno = code.firstlineno
+    assert isinstance(lineno, int)
     return fspath, lineno
 
 #
@@ -293,7 +296,7 @@
     except py.builtin._sysex:
         raise
     except:
-        return None, None
+        return "", -1
     source = Source()
     source.lines = [line.rstrip() for line in sourcelines]
     return source, lineno


diff -r 44cff93787d68267a2d2cb3f3f6c45a45ee352d5 -r f14a0bda45eb4e0891fb89b65ed72fe8a7b62471 setup.py
--- a/setup.py
+++ b/setup.py
@@ -12,7 +12,7 @@
         name='py',
         description='library with cross-python path, ini-parsing, io, code, log facilities',
         long_description = open('README.txt').read(),
-        version='1.4.6.dev5',
+        version='1.4.6.dev6',
         url='http://pylib.org',
         license='MIT license',
         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],


diff -r 44cff93787d68267a2d2cb3f3f6c45a45ee352d5 -r f14a0bda45eb4e0891fb89b65ed72fe8a7b62471 testing/code/test_source.py
--- a/testing/code/test_source.py
+++ b/testing/code/test_source.py
@@ -434,8 +434,11 @@
     assert fspath.basename == "test_source.py"
     assert lineno == A_lineno
 
-    assert getfslineno(3) == (None, None)
-
+    assert getfslineno(3) == ("", -1)
+    class B:
+        pass
+    B.__name__ = "B2"
+    assert getfslineno(B)[1] == -1
 
 def test_code_of_object_instance_with_call():
     class A:

Repository URL: https://bitbucket.org/hpk42/py/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the pytest-commit mailing list