Python new.code() Examples
The following are 30
code examples of new.code().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
new
, or try the search function
.
Example #1
Source File: CLI.py From pycopia with Apache License 2.0 | 7 votes |
def python(self, argv): import code ns = self._get_ns() console = code.InteractiveConsole(ns) console.raw_input = self._ui.user_input try: saveps1, saveps2 = sys.ps1, sys.ps2 except AttributeError: saveps1, saveps2 = ">>> ", "... " sys.ps1, sys.ps2 = "%%GPython%%N:%s> " % (self._obj.__class__.__name__,), "more> " if readline: oc = readline.get_completer() readline.set_completer(Completer(ns).complete) console.interact("You are now in Python. ^D exits.") if readline: readline.set_completer(oc) sys.ps1, sys.ps2 = saveps1, saveps2 self._reset_scopes() # This is needed to reset PagedIO so background events don't cause the pager to activate.
Example #2
Source File: cloudpickle.py From spark-cluster-deployment with Apache License 2.0 | 6 votes |
def _make_skel_func(code, num_closures, base_globals = None): """ Creates a skeleton function object that contains just the provided code and the correct number of cells in func_closure. All other func attributes (e.g. func_globals) are empty. """ #build closure (cells): if not ctypes: raise Exception('ctypes failed to import; cannot build function') cellnew = ctypes.pythonapi.PyCell_New cellnew.restype = ctypes.py_object cellnew.argtypes = (ctypes.py_object,) dummy_closure = tuple(map(lambda i: cellnew(None), range(num_closures))) if base_globals is None: base_globals = {} base_globals['__builtins__'] = __builtins__ return types.FunctionType(code, base_globals, None, None, dummy_closure) # this piece of opaque code is needed below to modify 'cell' contents
Example #3
Source File: bytecode_graph.py From flare-bytecode_graph with Apache License 2.0 | 6 votes |
def calc_lnotab(self): ''' Creates a new co_lineno after modifying bytecode ''' rvalue = "" prev_lineno = self.code.co_firstlineno prev_offset = self.head.addr for current in self.nodes(): if current.co_lnotab == prev_lineno: continue new_offset = current.co_lnotab - prev_lineno new_offset = 0xff if new_offset > 0xff else new_offset rvalue += struct.pack("BB", current.addr - prev_offset, (current.co_lnotab - prev_lineno) & 0xff) prev_lineno = current.co_lnotab prev_offset = current.addr return rvalue
Example #4
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 6 votes |
def nextBlock(self, block=None): # XXX think we need to specify when there is implicit transfer # from one block to the next. might be better to represent this # with explicit JUMP_ABSOLUTE instructions that are optimized # out when they are unnecessary. # # I think this strategy works: each block has a child # designated as "next" which is returned as the last of the # children. because the nodes in a graph are emitted in # reverse post order, the "next" block will always be emitted # immediately after its parent. # Worry: maintaining this invariant could be tricky if block is None: block = self.newBlock() # Note: If the current block ends with an unconditional # control transfer, then it is incorrect to add an implicit # transfer to the block graph. The current code requires # these edges to get the blocks emitted in the right order, # however. :-( If a client needs to remove these edges, call # pruneEdges(). self.current.addNext(block) self.startBlock(block)
Example #5
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 6 votes |
def pruneNext(self): """Remove bogus edge for unconditional transfers Each block has a next edge that accounts for implicit control transfers, e.g. from a JUMP_IF_FALSE to the block that will be executed if the test is true. These edges must remain for the current assembler code to work. If they are removed, the dfs_postorder gets things in weird orders. However, they shouldn't be there for other purposes, e.g. conversion to SSA form. This method will remove the next edge when it follows an unconditional control transfer. """ try: op, arg = self.insts[-1] except (IndexError, ValueError): return if op in self._uncond_transfer: self.next = []
Example #6
Source File: CLI.py From pycopia with Apache License 2.0 | 6 votes |
def python(self, argv): import code ns = self._get_ns() console = code.InteractiveConsole(ns) console.raw_input = self._ui.user_input try: saveps1, saveps2 = sys.ps1, sys.ps2 except AttributeError: saveps1, saveps2 = ">>> ", "... " sys.ps1, sys.ps2 = "%%GPython%%N:%s> " % (self._obj.__class__.__name__,), "more> " if readline: oc = readline.get_completer() readline.set_completer(Completer(ns).complete) console.interact("You are now in Python. ^D exits.") if readline: readline.set_completer(oc) sys.ps1, sys.ps2 = saveps1, saveps2 self._reset_scopes()
Example #7
Source File: cloudpickle.py From spark-cluster-deployment with Apache License 2.0 | 6 votes |
def extract_code_globals(co): """ Find all globals names read or written to by codeblock co """ code = co.co_code names = co.co_names out_names = set() n = len(code) i = 0 extended_arg = 0 while i < n: op = code[i] i = i+1 if op >= HAVE_ARGUMENT: oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg extended_arg = 0 i = i+2 if op == EXTENDED_ARG: extended_arg = oparg*65536L if op in GLOBAL_OPS: out_names.add(names[oparg]) #print 'extracted', out_names, ' from ', names return out_names
Example #8
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 5 votes |
def getCode(self): return ''.join(self.code)
Example #9
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 5 votes |
def nextLine(self, lineno): if self.firstline == 0: self.firstline = lineno self.lastline = lineno else: # compute deltas addr = self.codeOffset - self.lastoff line = lineno - self.lastline # Python assumes that lineno always increases with # increasing bytecode address (lnotab is unsigned char). # Depending on when SET_LINENO instructions are emitted # this is not always true. Consider the code: # a = (1, # b) # In the bytecode stream, the assignment to "a" occurs # after the loading of "b". This works with the C Python # compiler because it only generates a SET_LINENO instruction # for the assignment. if line >= 0: push = self.lnotab.append while addr > 255: push(255); push(0) addr -= 255 while line > 255: push(addr); push(255) line -= 255 addr = 0 if addr > 0 or line > 0: push(addr); push(line) self.lastline = lineno self.lastoff = self.codeOffset
Example #10
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 5 votes |
def addCode(self, *args): for arg in args: self.code.append(chr(arg)) self.codeOffset = self.codeOffset + len(args)
Example #11
Source File: cloudpickle.py From spark-cluster-deployment with Apache License 2.0 | 5 votes |
def save_codeobject(self, obj, pack=struct.pack): """ Save a code object """ #print 'try to save codeobj: ', obj args = ( obj.co_argcount, obj.co_nlocals, obj.co_stacksize, obj.co_flags, obj.co_code, obj.co_consts, obj.co_names, obj.co_varnames, obj.co_filename, obj.co_name, obj.co_firstlineno, obj.co_lnotab, obj.co_freevars, obj.co_cellvars ) self.save_reduce(types.CodeType, args, obj=obj)
Example #12
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 5 votes |
def getConsts(self): """Return a tuple for the const slot of the code object Must convert references to code (MAKE_FUNCTION) to code objects recursively. """ l = [] for elt in self.consts: if isinstance(elt, PyFlowGraph): elt = elt.getCode() l.append(elt) return tuple(l)
Example #13
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 5 votes |
def newCodeObject(self): assert self.stage == DONE if (self.flags & CO_NEWLOCALS) == 0: nlocals = 0 else: nlocals = len(self.varnames) argcount = self.argcount if self.flags & CO_VARKEYWORDS: argcount = argcount - 1 return new.code(argcount, nlocals, self.stacksize, self.flags, self.lnotab.getCode(), self.getConsts(), tuple(self.names), tuple(self.varnames), self.filename, self.name, self.lnotab.firstline, self.lnotab.getTable(), tuple(self.freevars), tuple(self.cellvars))
Example #14
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 5 votes |
def computeStackDepth(self): """Compute the max stack depth. Approach is to compute the stack effect of each basic block. Then find the path through the code with the largest total effect. """ depth = {} exit = None for b in self.getBlocks(): depth[b] = findDepth(b.getInstructions()) seen = {} def max_depth(b, d): if seen.has_key(b): return d seen[b] = 1 d = d + depth[b] children = b.get_children() if children: return max([max_depth(c, d) for c in children]) else: if not b.label == "exit": return max_depth(self.exit, d) else: return d self.stacksize = max_depth(self.entry, 0)
Example #15
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 5 votes |
def getCode(self): """Get a Python code object""" assert self.stage == RAW self.computeStackDepth() self.flattenGraph() assert self.stage == FLAT self.convertArgs() assert self.stage == CONV self.makeByteCode() assert self.stage == DONE return self.newCodeObject()
Example #16
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 5 votes |
def __init__(self, name, filename, args=(), optimized=0, klass=None): self.super_init() self.name = name self.filename = filename self.docstring = None self.args = args # XXX self.argcount = getArgCount(args) self.klass = klass if optimized: self.flags = CO_OPTIMIZED | CO_NEWLOCALS else: self.flags = 0 self.consts = [] self.names = [] # Free variables found by the symbol table scan, including # variables used only in nested scopes, are included here. self.freevars = [] self.cellvars = [] # The closure list is used to track the order of cell # variables and free variables in the resulting code object. # The offsets used by LOAD_CLOSURE/LOAD_DEREF refer to both # kinds of variables. self.closure = [] self.varnames = list(args) or [] for i in range(len(self.varnames)): var = self.varnames[i] if isinstance(var, TupleArg): self.varnames[i] = var.getName() self.stage = RAW
Example #17
Source File: pyassem.py From medicare-demo with Apache License 2.0 | 5 votes |
def fixupOrder(self, blocks, default_next): """Fixup bad order introduced by DFS.""" # XXX This is a total mess. There must be a better way to get # the code blocks in the right order. self.fixupOrderHonorNext(blocks, default_next) self.fixupOrderForward(blocks, default_next)
Example #18
Source File: cloudpickle.py From spark-cluster-deployment with Apache License 2.0 | 5 votes |
def extract_func_data(self, func): """ Turn the function into a tuple of data necessary to recreate it: code, globals, defaults, closure, dict """ code = func.func_code # extract all global ref's func_global_refs = CloudPickler.extract_code_globals(code) if code.co_consts: # see if nested function have any global refs for const in code.co_consts: if type(const) is types.CodeType and const.co_names: func_global_refs = func_global_refs.union( CloudPickler.extract_code_globals(const)) # process all variables referenced by global environment f_globals = {} for var in func_global_refs: #Some names, such as class functions are not global - we don't need them if func.func_globals.has_key(var): f_globals[var] = func.func_globals[var] # defaults requires no processing defaults = func.func_defaults def get_contents(cell): try: return cell.cell_contents except ValueError, e: #cell is empty error on not yet assigned raise pickle.PicklingError('Function to be pickled has free variables that are referenced before assignment in enclosing scope') # process closure
Example #19
Source File: modulefinder.py From medicare-demo with Apache License 2.0 | 5 votes |
def replace_paths_in_code(self, co): new_filename = original_filename = os.path.normpath(co.co_filename) for f, r in self.replace_paths: if original_filename.startswith(f): new_filename = r + original_filename[len(f):] break if self.debug and original_filename not in self.processed_paths: if new_filename != original_filename: self.msgout(2, "co_filename %r changed to %r" \ % (original_filename,new_filename,)) else: self.msgout(2, "co_filename %r remains unchanged" \ % (original_filename,)) self.processed_paths.append(original_filename) consts = list(co.co_consts) for i in range(len(consts)): if isinstance(consts[i], type(co)): consts[i] = self.replace_paths_in_code(consts[i]) return new.code(co.co_argcount, co.co_nlocals, co.co_stacksize, co.co_flags, co.co_code, tuple(consts), co.co_names, co.co_varnames, new_filename, co.co_name, co.co_firstlineno, co.co_lnotab, co.co_freevars, co.co_cellvars)
Example #20
Source File: bytecode_graph.py From flare-bytecode_graph with Apache License 2.0 | 5 votes |
def parse_bytecode(self): ''' Parses the bytecode stream and creates an instruction graph ''' self.bytecodes = {} prev = None offset = 0 targets = [] while offset < len(self.code.co_code): next = Bytecode(self.base + offset, self.code.co_code[offset:offset+3], prev) self.bytecodes[self.base + offset] = next offset += self.bytecodes[offset].len() if prev is not None: prev.next = next prev = next if next.get_target_addr() is not None: targets.append(next.get_target_addr()) for x in targets: if x not in self.bytecodes: print "Nonlinear issue at offset: %08x" % x self.head = self.bytecodes[self.base] self.apply_labels() return
Example #21
Source File: bytecode_graph.py From flare-bytecode_graph with Apache License 2.0 | 5 votes |
def get_code(self, start=None): ''' Produce a new code object based on the graph ''' self.refactor() # generate a new co_lineno new_co_lineno = self.calc_lnotab() # generate new bytecode stream new_co_code = "" for x in self.nodes(start): new_co_code += x.bin() # create a new code object with modified bytecode and updated line numbers # a new code object is necessary because co_code is readonly rvalue = new.code(self.code.co_argcount, self.code.co_nlocals, self.code.co_stacksize, self.code.co_flags, new_co_code, self.code.co_consts, self.code.co_names, self.code.co_varnames, self.code.co_filename, self.code.co_name, self.code.co_firstlineno, new_co_lineno) return rvalue
Example #22
Source File: bytecode_graph.py From flare-bytecode_graph with Apache License 2.0 | 5 votes |
def apply_lineno(self): ''' Parses the code object co_lnotab list and applies line numbers to bytecode. This is used to create a new co_lnotab list after modifying bytecode. ''' byte_increments = [ord(c) for c in self.code.co_lnotab[0::2]] line_increments = [ord(c) for c in self.code.co_lnotab[1::2]] lineno = self.code.co_firstlineno addr = self.base linenos = [] for byte_incr, line_incr in zip(byte_increments, line_increments): addr += byte_incr lineno += line_incr linenos.append((addr, lineno)) if linenos == []: return current_addr, current_lineno = linenos.pop(0) current_addr, next_lineno = linenos.pop(0) for x in self.nodes(): if x.addr >= current_addr: current_lineno = next_lineno if len(linenos) != 0: current_addr, next_lineno = linenos.pop(0) x.co_lnotab = current_lineno
Example #23
Source File: hack_pyc.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def hack_line_numbers(self): self.code = hack_line_numbers(self.code)
Example #24
Source File: CLI.py From pycopia with Apache License 2.0 | 5 votes |
def alias(self, argv): """alias [newalias] With no argument prints the current set of aliases. With an argument of the form alias ..., sets a new alias. """ if len(argv) == 1: for name, val in self._aliases.items(): self._print("alias %s='%s'" % (name, " ".join(val))) return 0 elif len(argv) == 2 and '=' not in argv[1]: name = argv[1] try: self._print("%s=%s" % (name, " ".join(self._aliases[name]))) except KeyError: self._print("undefined alias.") return 0 # else try: # this icky code to handle different permutations of where the '=' is. argv.pop(0) # discard 'alias' name = argv.pop(0) if "=" in name: name, rh = name.split("=", 1) argv.insert(0,rh) elif argv[0].startswith("="): if len(argv[0]) > 1: # if argv[1] is '=something' argv[0] = argv[0][1:] else: del argv[0] # remove the '=' self._aliases[name] = argv except: ex, val = sys.exc_info()[:2] self._print("alias: Could not set alias. Usage: alias name=value") self._print("%s (%s)" % (ex, val)) return 1
Example #25
Source File: CLI.py From pycopia with Apache License 2.0 | 5 votes |
def pyeval(self, argv): snippet = " ".join(argv[1:]).strip()+"\n" ns = self._get_ns() try: code = compile(snippet, '<CLI>', 'eval') rv = eval(code, globals(), ns) except: t, v, tb = sys.exc_info() self._print('*** %s (%s)' % (t, v)) else: self._print(rv) return rv
Example #26
Source File: CLI.py From pycopia with Apache License 2.0 | 5 votes |
def pyexec(self, argv): snippet = " ".join(argv[1:]).strip()+"\n" ns = self._get_ns() try: code = compile(snippet, '<CLI>', 'exec') exec code in globals(), ns except: t, v, tb = sys.exc_info() self._print('*** %s (%s)' % (t, v))
Example #27
Source File: CLI.py From pycopia with Apache License 2.0 | 5 votes |
def debug(self, argv): """debug ["on"|"off"] Enables debugging for CLI code. Enters the debugger if an exception occurs.""" global _DEBUG if len(argv) > 1: cmd = argv[1] if cmd == "on": _DEBUG = True else: _DEBUG = False else: self._ui.printf( "Debugging is currently %%I%s%%N." % ("on" if _DEBUG else "off",))
Example #28
Source File: CLI.py From pycopia with Apache License 2.0 | 5 votes |
def alias(self, argv): """alias [newalias] With no argument prints the current set of aliases. With an argument of the form alias ..., sets a new alias. """ if len(argv) == 1: for name, val in self._aliases.items(): self._print("alias %s='%s'" % (name, " ".join(val))) return 0 elif len(argv) == 2 and '=' not in argv[1]: name = argv[1] try: self._print("%s=%s" % (name, " ".join(self._aliases[name]))) except KeyError: self._print("undefined alias.") return 0 # else try: # this icky code to handle different permutations of where the '=' is. argv.pop(0) # discard 'alias' name = argv.pop(0) if "=" in name: name, rh = name.split("=", 1) argv.insert(0,rh) elif argv[0].startswith("="): if len(argv[0]) > 1: # if argv[1] is '=something' argv[0] = argv[0][1:] else: del argv[0] # remove the '=' self._aliases[name] = argv except: ex, val = sys.exc_info()[:2] self._print("alias: Could not set alias. Usage: alias name=value") self._print("%s (%s)" % (ex, val)) return 1
Example #29
Source File: CLI.py From pycopia with Apache License 2.0 | 5 votes |
def pyeval(self, argv): snippet = " ".join(argv[1:]).strip()+"\n" ns = self._get_ns() try: code = compile(snippet, '<CLI>', 'eval') rv = eval(code, globals(), ns) except: t, v, tb = sys.exc_info() self._print('*** %s (%s)' % (t, v)) else: self._print(rv) return rv
Example #30
Source File: CLI.py From pycopia with Apache License 2.0 | 5 votes |
def pyexec(self, argv): snippet = " ".join(argv[1:]).strip()+"\n" ns = self._get_ns() try: code = compile(snippet, '<CLI>', 'exec') exec(code, globals(), ns) except: t, v, tb = sys.exc_info() self._print('*** %s (%s)' % (t, v))