- implement a general getfslineno helper in code/source.py with tests
- not exposed yey --HG-- branch : trunk
This commit is contained in:
parent
9950fdc3eb
commit
1e5ece07e8
|
@ -217,6 +217,26 @@ def compile_(source, filename=None, mode='exec', flags=
|
|||
return co
|
||||
|
||||
|
||||
def getfslineno(obj):
|
||||
try:
|
||||
code = py.code.Code(obj)
|
||||
except TypeError:
|
||||
# fallback to
|
||||
fn = (py.std.inspect.getsourcefile(obj) or
|
||||
py.std.inspect.getfile(obj))
|
||||
fspath = fn and py.path.local(fn) or None
|
||||
if fspath:
|
||||
try:
|
||||
_, lineno = findsource(obj)
|
||||
except IOError:
|
||||
lineno = None
|
||||
else:
|
||||
lineno = None
|
||||
else:
|
||||
fspath = code.path
|
||||
lineno = code.firstlineno
|
||||
return fspath, lineno
|
||||
|
||||
#
|
||||
# helper functions
|
||||
#
|
||||
|
|
|
@ -355,3 +355,23 @@ def test_findsource___source__():
|
|||
assert 'if 1:' in str(src)
|
||||
assert src[lineno] == " def x():"
|
||||
|
||||
|
||||
def test_getfslineno():
|
||||
from py.__.code.source import getfslineno
|
||||
|
||||
def f(x):
|
||||
pass
|
||||
|
||||
fspath, lineno = getfslineno(f)
|
||||
|
||||
assert fspath == py.path.local(__file__)
|
||||
assert lineno == f.func_code.co_firstlineno-1 # see findsource
|
||||
|
||||
class A(object):
|
||||
pass
|
||||
|
||||
fspath, lineno = getfslineno(A)
|
||||
|
||||
_, A_lineno = py.std.inspect.findsource(A)
|
||||
assert fspath == py.path.local(__file__)
|
||||
assert lineno == A_lineno
|
||||
|
|
Loading…
Reference in New Issue