23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 | |
def new(self, rec=False, **kwargs): |
""" return new code object with modified attributes. |
if rec-cursive is true then dive into code |
objects contained in co_consts. |
""" |
names = [x for x in dir(self.raw) if x[:3] == 'co_'] |
for name in kwargs: |
if name not in names: |
raise TypeError("unknown code attribute: %r" %(name, )) |
if rec: |
newconstlist = [] |
co = self.raw |
cotype = type(co) |
for c in co.co_consts: |
if isinstance(c, cotype): |
c = self.__class__(c).new(rec=True, **kwargs) |
newconstlist.append(c) |
return self.new(rec=False, co_consts=tuple(newconstlist), **kwargs) |
for name in names: |
if name not in kwargs: |
kwargs[name] = getattr(self.raw, name) |
return py.std.new.code( |
kwargs['co_argcount'], |
kwargs['co_nlocals'], |
kwargs['co_stacksize'], |
kwargs['co_flags'], |
kwargs['co_code'], |
kwargs['co_consts'], |
kwargs['co_names'], |
kwargs['co_varnames'], |
kwargs['co_filename'], |
kwargs['co_name'], |
kwargs['co_firstlineno'], |
kwargs['co_lnotab'], |
kwargs['co_freevars'], |
kwargs['co_cellvars'], |
) | |