from python-list@cwi.nl

Steven D. Majewski (sdm7g@elvis.med.Virginia.EDU)
Wed, 6 Apr 1994 20:06:26 GMT

Subject: code & function objects

On Apr 6, 13:06, "Steven D. Majewski" wrote:
>
> Naievely copying and the codeobject case and modifying it for
> funcobject in marshal.c
>
> else if (is_funcobject(v)) {
> funcobject *fn = (funcobject *) v;
> w_byte(TYPE_CODE, p);
> w_object(fn->func_code, p );
> w_object(fn->func_globals, p );
> w_object(fn->func_name, p );
> }
>
> Will also (usually) give you a nice infinite recursion on
> marshal.dumps( function ), as it tries to dump function's
> .func_globals dict, which in many cases contains other
> function objects.
>

Interestingly - how imported .pyc get around this is that the
entire file is a piece of executable code which when executed
creates the other objects. ( Another fact that should have
been obvious from Python's behaviour, but I hadn't ever thought
about it before. )

There is some magic stuff in the beginning, but if you skip over
that:

>>> import marshal
>>> pycode = open( 'f.pyc', 'r' ).read()
>>> code_obj = marshal.loads( pycode[8:] ) # skip Magic id
>>> code_obj
<code object ? at 20074f78, file "./f.py", line 0>
>>> dir()
['__name__', 'code_obj', 'marshal', 'pycode']
>>> exec code_obj
>>> dir() # exec-ing code object def's entries into namespace
['__name__', 'addtodict', 'addtolist', 'code_obj', 'fac', 'fib',
'marshal', 'pycode']
>>>

[ This causes me to reconsider my previous suggestion of using
(a more complete) marshal.c to add newfuncobject( code, name,
global ) and other new*object functions to Python. Maybe I've
got it ass-backwards! ]

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics