Re: marshal and code objects

tnb2d@henson.cs.virginia.edu
Tue, 5 Apr 94 11:44:41 EDT

Steve Majewski is currently having this same problem as he
tries to re-assign the code objects he "optimizes ;-)" with his
byte-code optimizer. I have had this problem in the past when trying
to change the instance method that is bound to an instance method
object (the 'im_self' attribute). The problem is that these built-in
objects have *read-only* attributes, so they are not modifiable, and
there is no facility currently available from within Python (although
you can do it from C!) to create new ones.
What we need is the ability to create new code objects,
function objects, instance method objects, etc... with user-specified
parameters. For an instance method object, for instance, those are
im_class, im_func and im_self. I went ahead and wrote a dynamic
module that does this for instance method objects, and I can easily
write modules to do this for code objects and function objects as
well, but I believe this should be supported by the language itself (I
believe this because it's a useful operation that more people than
just me want to do!)
But before I make a bigger fool of myself, please tell me that
this is not already supported somehow and I'm just missign it!

Richard Stoakley writes:
|
| Is there anyway to assign a code object to a function or method in
| python?
|
| If not why are code objects supported as a valid type that can be
| packed up using the marshal module in python?
|
| if I have:
|
| >>> import marshal
|
| >>> def gru(*args):
| ... print args
|
| >>> marshalledFunc = marshal.dumps(gru.func_code)
|
| >>> reclaimedFunc = marshal.loads(marshalledFunc)
|
| I can ask if "gru.func_code == reclaimedFunc" and python says you bet,
| but I can't then assign reclaimedFunc to any object to get a new
| function with the same functionality as gru. (because gru.func_code is
| read only).
|
|
| thanks,
|
| stoakley
|
| -------------------------------------------------------------------------------
| Richard Stoakley
| rws2v@virginia.edu "It's never to late to have a pleasant childhood!"
| University of Virginia
|
|
|

-------> Tommy.

"Subvert the parental paradigm - Refuse Birth."