本文整理匯總了Python中new.code方法的典型用法代碼示例。如果您正苦於以下問題:Python new.code方法的具體用法?Python new.code怎麽用?Python new.code使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類new
的用法示例。
在下文中一共展示了new.code方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: python
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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.
示例2: python
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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()
示例3: calc_lnotab
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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
示例4: nextBlock
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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)
示例5: pruneNext
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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 = []
示例6: extract_code_globals
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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
示例7: _make_skel_func
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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
示例8: debug
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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." % (IF(_DEBUG, "on", "off"),))
示例9: alias
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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
示例10: pyeval
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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
示例11: pyexec
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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))
示例12: debug
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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",))
示例13: pyexec
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
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))
示例14: read
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
def read(self, f):
if isinstance(f, basestring):
f = open(f, "rb")
self.magic = f.read(4)
self.modtime = f.read(4)
self.code = marshal.load(f)
示例15: write
# 需要導入模塊: import new [as 別名]
# 或者: from new import code [as 別名]
def write(self, f):
if isinstance(f, basestring):
f = open(f, "wb")
f.write(self.magic)
f.write(self.modtime)
marshal.dump(self.code, f)