add a helper to get a function's code

--HG--
branch : trunk
This commit is contained in:
Benjamin Peterson 2010-04-23 20:39:40 -05:00
parent f16d54f9a8
commit d1b45ef3d4
3 changed files with 13 additions and 0 deletions

View File

@ -125,6 +125,7 @@ py.apipkg.initpkg(__name__, dict(
'_istext' : '._builtin:_istext',
'_getimself' : '._builtin:_getimself',
'_getfuncdict' : '._builtin:_getfuncdict',
'_getcode' : '._builtin:_getcode',
'builtins' : '._builtin:builtins',
'execfile' : '._builtin:execfile',
'callable' : '._builtin:callable',

View File

@ -111,6 +111,9 @@ if sys.version_info >= (3, 0):
def _getfuncdict(function):
return getattr(function, "__dict__", None)
def _getcode(function):
return getattr(function, "__code__", None)
def execfile(fn, globs=None, locs=None):
if globs is None:
back = sys._getframe(1)
@ -147,6 +150,9 @@ else:
def _getfuncdict(function):
return getattr(function, "__dict__", None)
def _getcode(function):
return getattr(function, "func_code", None)
def print_(*args, **kwargs):
""" minimal backport of py3k print statement. """
sep = ' '

View File

@ -1,4 +1,5 @@
import sys
import types
import py
from py.builtin import set, frozenset, reversed, sorted
@ -146,3 +147,8 @@ def test_tryimport():
assert x == py
x = py.builtin._tryimport('asldkajsdl', '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