Code Objects / Variable Names

F

F. Schaefer

PROBLEM:

How can you know what variables are involved in a code object.

It is not possible to execute the code object simply into a
dictionary and then watch the defined variables. This is
because these code objects only contain conditions, that
read out variables.

EXAMPLE:

For the following (compiled) condition

(door == "ajar" and alternator == "off") || time > 25

I need to extract, that the variables 'door', 'alternator' and 'time'
are involved.


Best Regards,

Frank Schäfer.
 
F

Francis Avila

F. Schaefer wrote in message ...
PROBLEM:

How can you know what variables are involved in a code object.

It is not possible to execute the code object simply into a
dictionary and then watch the defined variables. This is
because these code objects only contain conditions, that
read out variables.

This seems a strange requirement. Perhaps you could elaborate on what
you're doing and we could suggest alternative approaches? One *rarely*
needs to pass around raw code objects in Python, and both producing and
executing them is considerably slower than just about any other construct
you could use (not that it matters much).
EXAMPLE:

For the following (compiled) condition

(door == "ajar" and alternator == "off") || time > 25

I need to extract, that the variables 'door', 'alternator' and 'time'
are involved.
['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', 'co_argcount', 'co_cellvars',
'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags',
'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals',
'co_stacksize', 'co_varnames']('door', 'alternator', 'time')

See "Code objects" under "Internal Types" in section 3.2, "Standard Type
Hierarchy," of the Python Language Reference Manual.
1 0 LOAD_NAME 0 (door)
3 LOAD_CONST 0 ('ajar')
6 COMPARE_OP 2 (==)
9 JUMP_IF_FALSE 10 (to 22)
12 POP_TOP
13 LOAD_NAME 1 (alternator)
16 LOAD_CONST 1 ('off')
19 COMPARE_OP 2 (==) 25 POP_TOP
26 LOAD_NAME 2 (time)
29 LOAD_CONST 2 (25)
32 COMPARE_OP 4 (>) 36 LOAD_CONST 3 (None)
39 RETURN_VALUE
 
L

Lonnie Princehouse

You can use decompyle to get equivalent source from a code object, and
then use the compiler module to parse the source into syntax trees.

Traverse the trees to figure out which variables are being used.

If you've already got the source somewhere, you won't need decompyle.
 
C

Christos TZOTZIOY Georgiou

For the following (compiled) condition

(door == "ajar" and alternator == "off") || time > 25

I need to extract, that the variables 'door', 'alternator' and 'time'
are involved.

I believe that the solution that Francis and Andrew proposed is the most
suitable for your case. There is another one, however, that *might* be
useful sometime: use the eval(your_code, globals, locals) form, and
catch the NameError exceptions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top