def str(self, out_inst=None): '''Convert to a string.''' if not out_inst: out_inst = PyStringLL(None, self.v) out_inst.declare_tmp(name="_str") self.v.ctx.add(c.Assignment('=', c.ID(out_inst.name), c.FuncCall(c.ID('PyObject_Str'), c.ExprList(c.ID(self.name))))) self.fail_if_null(out_inst.name) return out_inst
def transfer_to_runnerfunc(self, args, vararg, kwonlyargs, kwarg): ## PyObject **tmp = calloc(sizeof(PyObject*), <len(args)> + 2) #0: set to the function object #1: set to the generator object itself #2: used as a slot to communicate a yielded value #3+: args in canonical order argsname = self.v.scope.ctx.reserve_name('gen_argslist', self.v.tu) decl = c.Decl(argsname, c.PtrDecl(PyObjectLL.typedecl()), init=c.ID('NULL')) self.v.scope.ctx.add_variable(decl, False) self.v.ctx.add(c.Assignment('=', c.ID(argsname), c.FuncCall(c.ID('calloc'), c.ExprList(c.Constant('integer', self.N_EXTRA_PARAMS + len(self.stub_arg_insts)), c.FuncCall(c.ID('sizeof'), c.ExprList(PyObjectLL.typedecl())))))) self.fail_if_null(argsname) self.v.ctx.add(c.Assignment('=', c.ArrayRef(c.ID(argsname), c.Constant('integer', self.SELF_INDEX)), c.ID('self'))) self.v.ctx.add(c.Assignment('=', c.ArrayRef(c.ID(argsname), c.Constant('integer', self.GENERATOR_INDEX)), c.ID('NULL'))) self.v.ctx.add(c.Assignment('=', c.ArrayRef(c.ID(argsname), c.Constant('integer', self.RETURN_INDEX)), c.ID('NULL'))) for i, arg_inst in enumerate(self.stub_arg_insts, self.ARGS_INDEX): self.v.ctx.add(c.Assignment('=', c.ArrayRef(c.ID(argsname), c.Constant('integer', i)), c.ID(arg_inst.name))) self.v.ctx.add(c.FuncCall(c.ID('Py_XINCREF'), c.ExprList(c.ID(arg_inst.name)))) self.v.ctx.add(c.Assignment('=', c.ID('__return_value__'), c.FuncCall(c.ID('MpGenerator_New'), c.ExprList( c.FuncCall(c.ID('strdup'), c.ExprList(c.Constant('string', PyStringLL.name_to_c_string(self.hlnode.owner.name)))), c.ID(self.c_runner_func.decl.name), c.ID(argsname), c.Constant('integer', self.STACKSIZE) #FIXME: try to discover and set a good size for the stack )))) self.fail_if_null('__return_value__') self.v.ctx.add(c.Assignment('=', c.ArrayRef(c.ID(argsname), c.Constant('integer', self.GENERATOR_INDEX)), c.ID('__return_value__')))
def declare_function_object(self, docstring): # create the function definition structure c_name = c.Constant('string', PyStringLL.name_to_c_string(self.hlnode.owner.name)) c_docstring = c.Constant('string', PyStringLL.python_to_c_string(docstring)) if docstring else c.ID('NULL') # create the function pyobject itself self.c_obj = PyObjectLL(self.hlnode, self.v) self.c_obj.declare(is_global=True, quals=['static'], name=self.hlnode.owner.global_c_name + "_pycfunc") self.c_obj.xdecref() self.v.ctx.add(c.Assignment('=', c.ID(self.c_obj.name), c.FuncCall(c.ID('MpFunction_New'), c.ExprList( c_name, c.ID(self.c_pystub_func.decl.name), c_docstring)))) self.fail_if_null(self.c_obj.name) # Note: this incref is for the global reference and needs to get cleaned up too # -- the inst we return gets owned by the local scope self.c_obj.incref() return self.c_obj
def create_builder_funcdef(self): self.v.ctx.add(c.Comment('Declare Class creation pycfunction "{}"'.format(self.hlnode.owner.name))) # create the function pyobject itself builder_func = PyObjectLL(self.hlnode, self.v) builder_func.declare_tmp(name=self.hlnode.owner.name + "_builder_pycfunc") c_name = c.Constant('string', PyStringLL.name_to_c_string(self.hlnode.owner.name)) self.v.ctx.add(c.Assignment('=', c.ID(builder_func.name), c.FuncCall(c.ID('MpFunction_New'), c.ExprList( c_name, c.ID(self.c_builder_func.decl.name), c.ID('NULL'))))) self.fail_if_null(builder_func.name) return builder_func
def set_initial_string_attribute(self, name: str, s: str): if s is not None: ps = PyStringLL(None, self.v) ps.declare_tmp() ps.new(s) else: ps = PyObjectLL(None, self.v) ps.declare_tmp() ps.assign_none() self.set_attr_string(name, ps) ps.decref()
def intro(self, docstring, module_name): self.v.ctx.add_variable(c.Decl('__return_value__', PyObjectLL.typedecl('__return_value__')), False) # set the docstring ds = PyStringLL(None, self.v) ds.declare_tmp() if docstring: ds.new(docstring) else: ds.assign_none() self.c_namespace_dict.set_item_string('__doc__', ds) ds.decref() # set the module name ds = PyStringLL(None, self.v) ds.declare_tmp() if module_name: ds.new(module_name) self.c_namespace_dict.set_item_string('__module__', ds) ds.decref()