add a helper to get a function's code
--HG-- branch : trunk
This commit is contained in:
parent
f16d54f9a8
commit
d1b45ef3d4
|
@ -125,6 +125,7 @@ py.apipkg.initpkg(__name__, dict(
|
||||||
'_istext' : '._builtin:_istext',
|
'_istext' : '._builtin:_istext',
|
||||||
'_getimself' : '._builtin:_getimself',
|
'_getimself' : '._builtin:_getimself',
|
||||||
'_getfuncdict' : '._builtin:_getfuncdict',
|
'_getfuncdict' : '._builtin:_getfuncdict',
|
||||||
|
'_getcode' : '._builtin:_getcode',
|
||||||
'builtins' : '._builtin:builtins',
|
'builtins' : '._builtin:builtins',
|
||||||
'execfile' : '._builtin:execfile',
|
'execfile' : '._builtin:execfile',
|
||||||
'callable' : '._builtin:callable',
|
'callable' : '._builtin:callable',
|
||||||
|
|
|
@ -111,6 +111,9 @@ if sys.version_info >= (3, 0):
|
||||||
def _getfuncdict(function):
|
def _getfuncdict(function):
|
||||||
return getattr(function, "__dict__", None)
|
return getattr(function, "__dict__", None)
|
||||||
|
|
||||||
|
def _getcode(function):
|
||||||
|
return getattr(function, "__code__", None)
|
||||||
|
|
||||||
def execfile(fn, globs=None, locs=None):
|
def execfile(fn, globs=None, locs=None):
|
||||||
if globs is None:
|
if globs is None:
|
||||||
back = sys._getframe(1)
|
back = sys._getframe(1)
|
||||||
|
@ -147,6 +150,9 @@ else:
|
||||||
def _getfuncdict(function):
|
def _getfuncdict(function):
|
||||||
return getattr(function, "__dict__", None)
|
return getattr(function, "__dict__", None)
|
||||||
|
|
||||||
|
def _getcode(function):
|
||||||
|
return getattr(function, "func_code", None)
|
||||||
|
|
||||||
def print_(*args, **kwargs):
|
def print_(*args, **kwargs):
|
||||||
""" minimal backport of py3k print statement. """
|
""" minimal backport of py3k print statement. """
|
||||||
sep = ' '
|
sep = ' '
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import sys
|
import sys
|
||||||
|
import types
|
||||||
import py
|
import py
|
||||||
from py.builtin import set, frozenset, reversed, sorted
|
from py.builtin import set, frozenset, reversed, sorted
|
||||||
|
|
||||||
|
@ -146,3 +147,8 @@ def test_tryimport():
|
||||||
assert x == py
|
assert x == py
|
||||||
x = py.builtin._tryimport('asldkajsdl', 'py.path')
|
x = py.builtin._tryimport('asldkajsdl', 'py.path')
|
||||||
assert x == py.path
|
assert x == py.path
|
||||||
|
|
||||||
|
def test_getcode():
|
||||||
|
code = py.builtin._getcode(test_getcode)
|
||||||
|
assert isinstance(code, types.CodeType)
|
||||||
|
assert py.builtin._getcode(4) is None
|
||||||
|
|
Loading…
Reference in New Issue