Esempio n. 1
0
 def assemble(self):
     """Build a PyCode object."""
     # Unless it's interactive, every code object must end in a return.
     if not self.current_block.have_return:
         self.use_next_block()
         if self.add_none_to_final_return:
             self.load_const(self.space.w_None)
         self.emit_op(ops.RETURN_VALUE)
         self.current_block.auto_inserted_return = True
     # Set the first lineno if it is not already explicitly set.
     if self.first_lineno == -1:
         if self.first_block.instructions:
             self.first_lineno = self.first_block.instructions[0].lineno
         else:
             self.first_lineno = 1
     blocks = self.first_block.post_order()
     self._resolve_block_targets(blocks)
     lnotab = self._build_lnotab(blocks)
     stack_depth = self._stacksize(blocks)
     consts_w = self.consts_w[:]
     names = _list_from_dict(self.names)
     var_names = _list_from_dict(self.var_names)
     cell_names = _list_from_dict(self.cell_vars)
     free_names = _list_from_dict(self.free_vars, len(cell_names))
     flags = self._get_code_flags()
     # (Only) inherit compilerflags in PyCF_MASK
     flags |= (self.compile_info.flags & consts.PyCF_MASK)
     bytecode = ''.join([block.get_code() for block in blocks])
     return PyCode(self.space, self.argcount, self.kwonlyargcount,
                   len(self.var_names), stack_depth, flags, bytecode,
                   list(consts_w), names, var_names,
                   self.compile_info.filename, self.name, self.first_lineno,
                   lnotab, free_names, cell_names,
                   self.compile_info.hidden_applevel)
Esempio n. 2
0
 def newCodeObject(self):
     if (self.flags & CO_NEWLOCALS) == 0:
         nlocals = 0
     else:
         nlocals = len(self.varnames)
     argcount = self.argcount
     if self.flags & CO_VARKEYWORDS:
         argcount = argcount - 1
     if self.lnotab is None:    # obscure case
         firstline = 0
         lnotab = ""
     else:
         firstline = self.lnotab.firstline
         lnotab = self.lnotab.getTable()
     return PyCode( self.space, argcount, nlocals,
                    self.stacksize, self.flags,
                    ''.join(self.co_code),
                    self.getConsts(),
                    self.names,
                    self.varnames,
                    self.filename, self.name,
                    firstline,
                    lnotab,
                    self.freevars,
                    self.cellvars
                    )
Esempio n. 3
0
def PyCode_New(space, argcount, kwonlyargcount, nlocals, stacksize, flags,
               w_code, w_consts, w_names, w_varnames, w_freevars, w_cellvars,
               w_filename, w_funcname, firstlineno, w_lnotab):
    """Return a new code object.  If you need a dummy code object to
    create a frame, use PyCode_NewEmpty() instead.  Calling
    PyCode_New() directly can bind you to a precise Python
    version since the definition of the bytecode changes often."""
    return space.wrap(
        PyCode(
            space,
            argcount=rffi.cast(lltype.Signed, argcount),
            kwonlyargcount=0,  # XXX fix signature
            nlocals=rffi.cast(lltype.Signed, nlocals),
            stacksize=rffi.cast(lltype.Signed, stacksize),
            flags=rffi.cast(lltype.Signed, flags),
            code=space.str_w(w_code),
            consts=space.fixedview(w_consts),
            names=unwrap_list_of_strings(space, w_names),
            varnames=unwrap_list_of_strings(space, w_varnames),
            filename=space.str0_w(w_filename),
            name=space.str_w(w_funcname),
            firstlineno=rffi.cast(lltype.Signed, firstlineno),
            lnotab=space.str_w(w_lnotab),
            freevars=unwrap_list_of_strings(space, w_freevars),
            cellvars=unwrap_list_of_strings(space, w_cellvars)))
Esempio n. 4
0
def PyCode_NewEmpty(space, filename, funcname, firstlineno):
    """Creates a new empty code object with the specified source location."""
    return space.wrap(PyCode(space,
                             argcount=0,
                             nlocals=0,
                             stacksize=0,
                             flags=0,
                             code="",
                             consts=[],
                             names=[],
                             varnames=[],
                             filename=rffi.charp2str(filename),
                             name=rffi.charp2str(funcname),
                             firstlineno=rffi.cast(lltype.Signed, firstlineno),
                             lnotab="",
                             freevars=[],
                             cellvars=[]))
Esempio n. 5
0
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)
    return PyCode(space, argcount, nlocals, stacksize, flags, code,
                  consts_w[:], names, varnames, filename, name, firstlineno,
                  lnotab, freevars, cellvars)
Esempio n. 6
0
 def make_code_with_const(w_obj):
     return PyCode(space, 0, 0, 1, 0, '', [w_obj], [], [], '', '', 0,
                   '', [], [], False)