| 1 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 |
from pypy.module.cpyext.api import ( |
| 3 |
PyObjectFields, CONST_STRING, CANNOT_FAIL, Py_ssize_t, |
| 4 |
cpython_api, bootstrap_function, cpython_struct, build_type_checkers, |
| 5 |
slot_function) |
| 6 |
from pypy.module.cpyext.pyobject import ( |
| 7 |
PyObject, make_ref, from_ref, decref, make_typedescr) |
| 8 |
from rpython.rlib.unroll import unrolling_iterable |
| 9 |
from pypy.interpreter.function import Function, Method |
| 10 |
from pypy.interpreter.pycode import PyCode |
| 11 |
from pypy.interpreter import pycode |
| 12 |
from pypy.tool.stdlib_opcode import bytecode_spec |
| 13 |
opcodedesc = bytecode_spec.opcodedesc |
| 14 |
|
| 15 |
CODE_FLAGS = dict( |
| 16 |
CO_OPTIMIZED = 0x0001, |
| 17 |
CO_NEWLOCALS = 0x0002, |
| 18 |
CO_VARARGS = 0x0004, |
| 19 |
CO_VARKEYWORDS = 0x0008, |
| 20 |
CO_NESTED = 0x0010, |
| 21 |
CO_GENERATOR = 0x0020, |
| 22 |
CO_COROUTINE=0x0080, |
| 23 |
CO_ITERABLE_COROUTINE=0x0100, |
| 24 |
) |
| 25 |
ALL_CODE_FLAGS = unrolling_iterable(CODE_FLAGS.items()) |
| 26 |
|
| 27 |
PyFunctionObjectStruct = lltype.ForwardReference() |
| 28 |
PyFunctionObject = lltype.Ptr(PyFunctionObjectStruct) |
| 29 |
PyFunctionObjectFields = PyObjectFields + \ |
| 30 |
(("func_name", PyObject),) |
| 31 |
cpython_struct("PyFunctionObject", PyFunctionObjectFields, PyFunctionObjectStruct) |
| 32 |
|
| 33 |
PyCodeObjectStruct = lltype.ForwardReference() |
| 34 |
PyCodeObject = lltype.Ptr(PyCodeObjectStruct) |
| 35 |
PyCodeObjectFields = PyObjectFields + \ |
| 36 |
(("co_name", PyObject), |
| 37 |
("co_filename", PyObject), |
| 38 |
("co_flags", rffi.INT), |
| 39 |
("co_argcount", rffi.INT), |
| 40 |
) |
| 41 |
cpython_struct("PyCodeObject", PyCodeObjectFields, PyCodeObjectStruct) |
| 42 |
|
| 43 |
@bootstrap_function |
| 44 |
def init_functionobject(space): |
| 45 |
make_typedescr(Function.typedef, |
| 46 |
basestruct=PyFunctionObject.TO, |
| 47 |
attach=function_attach, |
| 48 |
dealloc=function_dealloc) |
| 49 |
make_typedescr(PyCode.typedef, |
| 50 |
basestruct=PyCodeObject.TO, |
| 51 |
attach=code_attach, |
| 52 |
dealloc=code_dealloc) |
| 53 |
|
| 54 |
PyFunction_Check, PyFunction_CheckExact = build_type_checkers("Function", Function) |
| 55 |
PyMethod_Check, PyMethod_CheckExact = build_type_checkers("Method", Method) |
| 56 |
PyCode_Check, PyCode_CheckExact = build_type_checkers("Code", PyCode) |
| 57 |
|
| 58 |
def function_attach(space, py_obj, w_obj, w_userdata=None): |
| 59 |
py_func = rffi.cast(PyFunctionObject, py_obj) |
| 60 |
assert isinstance(w_obj, Function) |
| 61 |
py_func.c_func_name = make_ref(space, space.newtext(w_obj.name)) |
| 62 |
|
| 63 |
@slot_function([PyObject], lltype.Void) |
| 64 |
def function_dealloc(space, py_obj): |
| 65 |
py_func = rffi.cast(PyFunctionObject, py_obj) |
| 66 |
decref(space, py_func.c_func_name) |
| 67 |
from pypy.module.cpyext.object import _dealloc |
| 68 |
_dealloc(space, py_obj) |
| 69 |
|
| 70 |
def code_attach(space, py_obj, w_obj, w_userdata=None): |
| 71 |
py_code = rffi.cast(PyCodeObject, py_obj) |
| 72 |
assert isinstance(w_obj, PyCode) |
| 73 |
py_code.c_co_name = make_ref(space, space.newtext(w_obj.co_name)) |
| 74 |
py_code.c_co_filename = make_ref(space, w_obj.w_filename) |
| 75 |
co_flags = 0 |
| 76 |
for name, value in ALL_CODE_FLAGS: |
| 77 |
if w_obj.co_flags & getattr(pycode, name): |
| 78 |
co_flags |= value |
| 79 |
rffi.setintfield(py_code, 'c_co_flags', co_flags) |
| 80 |
rffi.setintfield(py_code, 'c_co_argcount', w_obj.co_argcount) |
| 81 |
|
| 82 |
@slot_function([PyObject], lltype.Void) |
| 83 |
def code_dealloc(space, py_obj): |
| 84 |
py_code = rffi.cast(PyCodeObject, py_obj) |
| 85 |
decref(space, py_code.c_co_name) |
| 86 |
decref(space, py_code.c_co_filename) |
| 87 |
from pypy.module.cpyext.object import _dealloc |
| 88 |
_dealloc(space, py_obj) |
| 89 |
|
| 90 |
@cpython_api([PyObject], PyObject, result_borrowed=True) |
| 91 |
def PyFunction_GetCode(space, w_func): |
| 92 |
"""Return the code object associated with the function object op.""" |
| 93 |
func = space.interp_w(Function, w_func) |
| 94 |
return func.code |
| 95 |
|
| 96 |
@cpython_api([PyObject], PyObject) |
| 97 |
def PyFunction_GetModule(space, w_func): |
| 98 |
return space.getattr(w_func, space.newtext('__module__')) |
| 99 |
|
| 100 |
|
| 101 |
@cpython_api([PyObject], PyObject) |
| 102 |
def PyFunction_GetGlobals(space, w_func): |
| 103 |
return space.getattr(w_func, space.newtext('__globals__')) |
| 104 |
|
| 105 |
@cpython_api([PyObject, PyObject], PyObject) |
| 106 |
def PyMethod_New(space, w_func, w_self): |
| 107 |
"""Return a new method object, with func being any callable object |
| 108 |
and self the instance the method should be bound. func is the |
| 109 |
function that will be called when the method is called. self must |
| 110 |
not be NULL.""" |
| 111 |
return Method(space, w_func, w_self) |
| 112 |
|
| 113 |
@cpython_api([PyObject], PyObject, result_borrowed=True) |
| 114 |
def PyMethod_Function(space, w_method): |
| 115 |
"""Return the function object associated with the method meth.""" |
| 116 |
assert isinstance(w_method, Method) |
| 117 |
return w_method.w_function |
| 118 |
|
| 119 |
@cpython_api([PyObject], PyObject, result_borrowed=True) |
| 120 |
def PyMethod_Self(space, w_method): |
| 121 |
"""Return the instance associated with the method meth if it is bound, |
| 122 |
otherwise return NULL.""" |
| 123 |
assert isinstance(w_method, Method) |
| 124 |
return w_method.w_instance |
| 125 |
|
| 126 |
def unwrap_list_of_texts(space, w_list): |
| 127 |
return [space.text_w(w_item) for w_item in space.fixedview(w_list)] |
| 128 |
|
| 129 |
@cpython_api([rffi.INT_real, rffi.INT_real, rffi.INT_real, rffi.INT_real, |
| 130 |
rffi.INT_real, |
| 131 |
PyObject, PyObject, PyObject, PyObject, PyObject, PyObject, |
| 132 |
PyObject, PyObject, PyObject, rffi.INT_real, PyObject], PyObject) |
| 133 |
def PyCode_New(space, argcount, kwonlyargcount, nlocals, stacksize, flags, |
| 134 |
w_code, w_consts, w_names, w_varnames, w_freevars, w_cellvars, |
| 135 |
w_filename, w_funcname, w_qualname, firstlineno, w_linetable): |
| 136 |
"""Return a new code object. If you need a dummy code object to |
| 137 |
create a frame, use PyCode_NewEmpty() instead. Calling |
| 138 |
PyCode_New() directly can bind you to a precise Python |
| 139 |
version since the definition of the bytecode changes often.""" |
| 140 |
return PyCode(space, |
| 141 |
argcount=rffi.cast(lltype.Signed, argcount), |
| 142 |
posonlyargcount=0, |
| 143 |
kwonlyargcount = rffi.cast(lltype.Signed, kwonlyargcount), |
| 144 |
nlocals=rffi.cast(lltype.Signed, nlocals), |
| 145 |
stacksize=rffi.cast(lltype.Signed, stacksize), |
| 146 |
flags=rffi.cast(lltype.Signed, flags), |
| 147 |
code=space.bytes_w(w_code), |
| 148 |
consts=space.fixedview(w_consts), |
| 149 |
names=unwrap_list_of_texts(space, w_names), |
| 150 |
varnames=unwrap_list_of_texts(space, w_varnames), |
| 151 |
filename=space.fsencode_w(w_filename), |
| 152 |
name=space.text_w(w_funcname), |
| 153 |
qualname=space.text_w(w_qualname), |
| 154 |
firstlineno=rffi.cast(lltype.Signed, firstlineno), |
| 155 |
linetable=space.bytes_w(w_linetable), |
| 156 |
freevars=unwrap_list_of_texts(space, w_freevars), |
| 157 |
cellvars=unwrap_list_of_texts(space, w_cellvars)) |
| 158 |
|
| 159 |
@cpython_api([rffi.INT_real, rffi.INT_real, rffi.INT_real, rffi.INT_real, |
| 160 |
rffi.INT_real, rffi.INT_real, |
| 161 |
PyObject, PyObject, PyObject, PyObject, PyObject, PyObject, |
| 162 |
PyObject, PyObject, PyObject, rffi.INT_real, PyObject, PyObject], PyCodeObject) |
| 163 |
def PyCode_NewWithPosOnlyArgs(space, argcount, posonlyargcount, kwonlyargcount, |
| 164 |
nlocals, stacksize, flags, |
| 165 |
w_code, w_consts, w_names, w_varnames, w_freevars, w_cellvars, |
| 166 |
w_filename, w_funcname, w_qualname, firstlineno, w_linetable, w_exceptiontable): |
| 167 |
"""Return a new code object. If you need a dummy code object to |
| 168 |
create a frame, use PyCode_NewEmpty() instead. Calling |
| 169 |
PyCode_New() directly can bind you to a precise Python |
| 170 |
version since the definition of the bytecode changes often.""" |
| 171 |
return PyCode(space, |
| 172 |
argcount=rffi.cast(lltype.Signed, argcount), |
| 173 |
posonlyargcount=rffi.cast(lltype.Signed, posonlyargcount), |
| 174 |
kwonlyargcount = rffi.cast(lltype.Signed, kwonlyargcount), |
| 175 |
nlocals=rffi.cast(lltype.Signed, nlocals), |
| 176 |
stacksize=rffi.cast(lltype.Signed, stacksize), |
| 177 |
flags=rffi.cast(lltype.Signed, flags), |
| 178 |
code=space.bytes_w(w_code), |
| 179 |
consts=space.fixedview(w_consts), |
| 180 |
names=unwrap_list_of_texts(space, w_names), |
| 181 |
varnames=unwrap_list_of_texts(space, w_varnames), |
| 182 |
filename=space.fsencode_w(w_filename), |
| 183 |
name=space.text_w(w_funcname), |
| 184 |
qualname=space.text_w(w_qualname), |
| 185 |
firstlineno=rffi.cast(lltype.Signed, firstlineno), |
| 186 |
linetable=space.bytes_w(w_linetable), |
| 187 |
freevars=unwrap_list_of_texts(space, w_freevars), |
| 188 |
cellvars=unwrap_list_of_texts(space, w_cellvars)) |
| 189 |
|
| 190 |
raises_assertionerror_bytecode = bytes(bytearray([ |
| 191 |
opcodedesc.LOAD_ASSERTION_ERROR.index, 0, |
| 192 |
opcodedesc.RAISE_VARARGS.index, 1, |
| 193 |
])) |
| 194 |
|
| 195 |
@cpython_api([CONST_STRING, CONST_STRING, rffi.INT_real], PyCodeObject) |
| 196 |
def PyCode_NewEmpty(space, filename, funcname, firstlineno): |
| 197 |
"""Creates a new empty code object with the specified source location.""" |
| 198 |
from pypy.interpreter.location import encode_positions |
| 199 |
firstlineno = rffi.cast(lltype.Signed, firstlineno) |
| 200 |
pos = (firstlineno, -1, -1, -1) |
| 201 |
size_bc = len(raises_assertionerror_bytecode) // 2 |
| 202 |
linetable = encode_positions([pos] * size_bc, firstlineno) |
| 203 |
|
| 204 |
name = rffi.charp2str(funcname) |
| 205 |
return PyCode(space, |
| 206 |
argcount=0, |
| 207 |
posonlyargcount=0, |
| 208 |
kwonlyargcount=0, |
| 209 |
nlocals=0, |
| 210 |
stacksize=1, |
| 211 |
flags=0, |
| 212 |
code=raises_assertionerror_bytecode, |
| 213 |
consts=[], |
| 214 |
names=[], |
| 215 |
varnames=[], |
| 216 |
filename=rffi.charp2str(filename), |
| 217 |
name=name, |
| 218 |
qualname=name, |
| 219 |
firstlineno=rffi.cast(lltype.Signed, firstlineno), |
| 220 |
linetable=linetable, |
| 221 |
freevars=[], |
| 222 |
cellvars=[]) |
| 223 |
|
| 224 |
@cpython_api([PyCodeObject], Py_ssize_t, error=CANNOT_FAIL) |
| 225 |
def PyCode_GetNumFree(space, w_co): |
| 226 |
"""Return the number of free variables in co.""" |
| 227 |
co = space.interp_w(PyCode, w_co) |
| 228 |
return len(co.co_freevars) |
| 229 |
|
| 230 |
@cpython_api([PyCodeObject, rffi.INT_real], rffi.INT_real, error=-1) |
| 231 |
def PyCode_Addr2Line(space, w_code, offset): |
| 232 |
from pypy.interpreter.pytraceback import offset2lineno |
| 233 |
offset = rffi.cast(lltype.Signed, offset) |
| 234 |
co = space.interp_w(PyCode, w_code) |
| 235 |
if offset < 0: |
| 236 |
return -1 |
| 237 |
if offset > len(co.co_code): |
| 238 |
return -1 |
| 239 |
return offset2lineno(co, offset) |
| 240 |
|
| 241 |
@cpython_api([PyCodeObject], PyObject) |
| 242 |
def PyCode_GetCellvars(space, w_co): |
| 243 |
co = space.interp_w(PyCode, w_co) |
| 244 |
return space.newtuple([space.newtext(v) for v in co.co_cellvars]) |
| 245 |
|
| 246 |
@cpython_api([PyCodeObject], PyObject) |
| 247 |
def PyCode_GetCode(space, w_co): |
| 248 |
co = space.interp_w(PyCode, w_co) |
| 249 |
return space.newbytes(co.co_code) |
| 250 |
|
| 251 |
@cpython_api([PyCodeObject], PyObject) |
| 252 |
def PyCode_GetFreevars(space, w_co): |
| 253 |
co = space.interp_w(PyCode, w_co) |
| 254 |
return space.newtuple([space.newtext(v) for v in co.co_freevars]) |
| 255 |
|
| 256 |
@cpython_api([PyCodeObject], PyObject) |
| 257 |
def PyCode_GetVarnames(space, w_co): |
| 258 |
co = space.interp_w(PyCode, w_co) |
| 259 |
return space.newtuple([space.newtext(v) for v in co.co_varnames]) |