本文整理汇总了Python中pypy.interpreter.pycode.PyCode类的典型用法代码示例。如果您正苦于以下问题:Python PyCode类的具体用法?Python PyCode怎么用?Python PyCode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PyCode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_method
def setup_method(self, method):
def c(x, y, *args):
pass
code = PyCode._from_code(self.space, c.func_code)
class ConcreteFastscopeFrame(Frame):
def __init__(self, space, code, numlocals):
self.code = code
Frame.__init__(self, space)
self.numlocals = numlocals
self.fastlocals_w = [None] * self.numlocals
def getcode(self):
return self.code
def setfastscope(self, scope_w):
self.fastlocals_w = scope_w
def getfastscope(self):
return self.fastlocals_w
def getfastscopelength(self):
return self.numlocals
self.f = ConcreteFastscopeFrame(self.space, code, numlocals=5)
示例2: test_call_function
def test_call_function(self):
space = self.space
d = {}
for i in range(10):
args = "(" + ''.join(["a%d," % a for a in range(i)]) + ")"
exec """
def f%s:
return %s
""" % (args, args) in d
f = d['f']
res = f(*range(i))
code = PyCode._from_code(self.space, f.func_code)
fn = Function(self.space, code, self.space.newdict())
assert fn.code.fast_natural_arity == i|PyCode.FLATPYCALL
if i < 5:
def bomb(*args):
assert False, "shortcutting should have avoided this"
code.funcrun = bomb
code.funcrun_obj = bomb
args_w = map(space.wrap, range(i))
w_res = space.call_function(fn, *args_w)
check = space.is_true(space.eq(w_res, space.wrap(res)))
assert check
示例3: unmarshal_pycode
def unmarshal_pycode(space, u, tc):
argcount = u.get_int()
nlocals = u.get_int()
stacksize = u.get_int()
flags = u.get_int()
code = unmarshal_str(u)
u.start(TYPE_TUPLE)
consts_w = u.get_tuple_w()
# copy in order not to merge it with anything else
names = unmarshal_strlist(u, TYPE_TUPLE)
varnames = unmarshal_strlist(u, TYPE_TUPLE)
freevars = unmarshal_strlist(u, TYPE_TUPLE)
cellvars = unmarshal_strlist(u, TYPE_TUPLE)
filename = unmarshal_str(u)
name = unmarshal_str(u)
firstlineno = u.get_int()
lnotab = unmarshal_str(u)
code = PyCode._code_new_w(
space,
argcount,
nlocals,
stacksize,
flags,
code,
consts_w,
names,
varnames,
filename,
name,
firstlineno,
lnotab,
freevars,
cellvars,
)
return space.wrap(code)
示例4: test_flatcall_method
def test_flatcall_method(self):
space = self.space
def f(self, a):
return a
code = PyCode._from_code(self.space, f.func_code)
fn = Function(self.space, code, self.space.newdict())
assert fn.code.fast_natural_arity == 2|PyCode.FLATPYCALL
def bomb(*args):
assert False, "shortcutting should have avoided this"
code.funcrun = bomb
code.funcrun_obj = bomb
w_3 = space.newint(3)
w_res = space.appexec([fn, w_3], """(f, x):
class A(object):
m = f
y = A().m(x)
b = A().m
z = b(x)
return y is x and z is x
""")
assert space.is_true(w_res)
示例5: test_flatcall
def test_flatcall(self):
space = self.space
def f(a):
return a
code = PyCode._from_code(self.space, f.func_code)
fn = Function(self.space, code, self.space.newdict())
assert fn.code.fast_natural_arity == 1|PyCode.FLATPYCALL
def bomb(*args):
assert False, "shortcutting should have avoided this"
code.funcrun = bomb
code.funcrun_obj = bomb
w_3 = space.newint(3)
w_res = space.call_function(fn, w_3)
assert w_res is w_3
w_res = space.appexec([fn, w_3], """(f, x):
return f(x)
""")
assert w_res is w_3
示例6: test_flatcall_default_arg
def test_flatcall_default_arg(self):
space = self.space
def f(a, b):
return a+b
code = PyCode._from_code(self.space, f.func_code)
fn = Function(self.space, code, self.space.newdict(),
defs_w=[space.newint(1)])
assert fn.code.fast_natural_arity == 2|eval.Code.FLATPYCALL
def bomb(*args):
assert False, "shortcutting should have avoided this"
code.funcrun = bomb
code.funcrun_obj = bomb
w_3 = space.newint(3)
w_4 = space.newint(4)
# ignore this for now
#w_res = space.call_function(fn, w_3)
# assert space.eq_w(w_res, w_4)
w_res = space.appexec([fn, w_3], """(f, x):
return f(x)
""")
assert space.eq_w(w_res, w_4)
示例7: test_flatcall_default_arg_method
def test_flatcall_default_arg_method(self):
space = self.space
def f(self, a, b):
return a+b
code = PyCode._from_code(self.space, f.func_code)
fn = Function(self.space, code, self.space.newdict(),
defs_w=[space.newint(1)])
assert fn.code.fast_natural_arity == 3|eval.Code.FLATPYCALL
def bomb(*args):
assert False, "shortcutting should have avoided this"
code.funcrun = bomb
code.funcrun_obj = bomb
w_3 = space.newint(3)
w_res = space.appexec([fn, w_3], """(f, x):
class A(object):
m = f
y = A().m(x)
b = A().m
z = b(x)
return y+10*z
""")
assert space.eq_w(w_res, space.wrap(44))
示例8: test_method_get
def test_method_get(self):
space = self.space
# Create some function for this test only
def m(self): return self
func = Function(space, PyCode._from_code(self.space, m.func_code),
space.newdict())
# Some shorthands
obj1 = space.wrap(23)
obj2 = space.wrap(42)
args = Arguments(space, [])
# Check method returned from func.__get__()
w_meth1 = descr_function_get(space, func, obj1, space.type(obj1))
meth1 = space.unwrap(w_meth1)
assert isinstance(meth1, Method)
assert meth1.call_args(args) == obj1
# Check method returned from method.__get__()
# --- meth1 is already bound so meth1.__get__(*) is meth1.
w_meth2 = meth1.descr_method_get(obj2, space.type(obj2))
meth2 = space.unwrap(w_meth2)
assert isinstance(meth2, Method)
assert meth2.call_args(args) == obj1
# Check method returned from unbound_method.__get__()
w_meth3 = descr_function_get(space, func, None, space.type(obj2))
meth3 = space.unwrap(w_meth3)
w_meth4 = meth3.descr_method_get(obj2, space.w_None)
meth4 = space.unwrap(w_meth4)
assert isinstance(meth4, Method)
assert meth4.call_args(args) == obj2
# Check method returned from unbound_method.__get__()
# --- with an incompatible class
w_meth5 = meth3.descr_method_get(space.wrap('hello'), space.w_str)
assert space.is_w(w_meth5, w_meth3)
示例9: test_AppFrame
def test_AppFrame(space):
import sys
co = PyCode._from_code(space, somefunc.func_code)
pyframe = PyFrame(space, co, space.newdict(), None)
runner = AppFrame(space, pyframe)
exprinfo.run("f = lambda x: x+1", runner)
msg = exprinfo.interpret("assert isinstance(f(2), float)", runner)
assert msg.startswith("assert isinstance(3, float)\n"
" + where 3 = ")
示例10: test_AppFrame
def test_AppFrame(space):
import sys
co = PyCode._from_code(space, somefunc.func_code)
pyframe = space.FrameClass(space, co, space.newdict(), None)
runner = AppFrame(space, pyframe)
interpret("f = lambda x: x+1", runner, should_fail=False)
msg = interpret("assert isinstance(f(2), float)", runner)
assert msg.startswith("assert isinstance(3, float)\n"
" + where 3 = ")
示例11: check
def check(self, w_dict, evalexpr, expected):
# for now, we compile evalexpr with CPython's compiler but run
# it with our own interpreter to extract the data from w_dict
co_expr = compile(evalexpr, "<evalexpr>", "eval")
space = self.space
pyco_expr = PyCode._from_code(space, co_expr)
w_res = pyco_expr.exec_code(space, w_dict, w_dict)
res = space.str_w(space.repr(w_res))
assert res == repr(expected)
示例12: eval
def eval(self, expression, w_globals, w_locals):
"NOT_RPYTHON: For internal debugging."
import types
from pypy.interpreter.pycode import PyCode
if isinstance(expression, str):
expression = compile(expression, '?', 'eval')
if isinstance(expression, types.CodeType):
expression = PyCode._from_code(self, expression)
if not isinstance(expression, PyCode):
raise TypeError, 'space.eval(): expected a string, code or PyCode object'
return expression.exec_code(self, w_globals, w_locals)
示例13: check
def check(self, w_dict, evalexpr, expected):
# for now, we compile evalexpr with CPython's compiler but run
# it with our own interpreter to extract the data from w_dict
co_expr = compile(evalexpr, '<evalexpr>', 'eval')
space = self.space
pyco_expr = PyCode._from_code(space, co_expr)
w_res = pyco_expr.exec_host_bytecode(w_dict, w_dict)
res = space.str_w(space.repr(w_res))
if not isinstance(expected, float):
assert res == repr(expected)
else:
# Float representation can vary a bit between interpreter
# versions, compare the numbers instead.
assert eval(res) == expected
示例14: exec_
def exec_(self, statement, w_globals, w_locals, hidden_applevel=False):
"NOT_RPYTHON: For internal debugging."
import types
from pypy.interpreter.pycode import PyCode
if isinstance(statement, str):
statement = compile(statement, '?', 'exec')
if isinstance(statement, types.CodeType):
statement = PyCode._from_code(self, statement,
hidden_applevel=hidden_applevel)
if not isinstance(statement, PyCode):
raise TypeError, 'space.exec_(): expected a string, code or PyCode object'
w_key = self.wrap('__builtins__')
if not self.is_true(self.contains(w_globals, w_key)):
self.setitem(w_globals, w_key, self.wrap(self.builtin))
return statement.exec_code(self, w_globals, w_locals)
示例15: getframe
def getframe(self, func):
space = self.space
try:
func = func.im_func
except AttributeError:
pass
code = func.func_code
code = PyCode._from_code(self.space, code)
w_globals = Constant({}) # space.newdict()
frame = self.space.createframe(code, w_globals)
formalargcount = code.getformalargcount()
dummy = Constant(None)
# dummy.dummy = True
arg_list = [Variable() for i in range(formalargcount)] + [dummy] * (frame.nlocals - formalargcount)
frame.setfastscope(arg_list)
return frame