Code Objects / Variable Names

Discussion in 'Python' started by F. Schaefer, Jan 7, 2004.

  1. F. Schaefer

    F. Schaefer Guest

    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. Schaefer, Jan 7, 2004
    #1
    1. Advertising

  2. 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.



    >>> cobj = compile('(door == "ajar" and alternator == "off") or time >

    25', '<stdin>', 'exec')
    >>> dir(cobj)

    ['__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']
    >>> cobj.co_names

    ('door', 'alternator', 'time')

    See "Code objects" under "Internal Types" in section 3.2, "Standard Type
    Hierarchy," of the Python Language Reference Manual.

    >>> import dis #just for fun.
    >>> dis.disco(cobj)

    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 (==)
    >> 22 JUMP_IF_TRUE 10 (to 35)

    25 POP_TOP
    26 LOAD_NAME 2 (time)
    29 LOAD_CONST 2 (25)
    32 COMPARE_OP 4 (>)
    >> 35 POP_TOP

    36 LOAD_CONST 3 (None)
    39 RETURN_VALUE

    --
    Francis Avila
    Francis Avila, Jan 7, 2004
    #2
    1. Advertising

  3. (F. Schaefer) wrote:

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


    http://www.python.org/doc/current/ref/types.html#l2h-138

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


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


    >>> c= compile('door=="ajar" and alternator=="off" or time>25', '', 'eval')
    >>> c.co_names

    ('door', 'alternator', 'time')

    HTH. Of course if the expression itself contains the likes of 'eval',
    'globals()' etc. this cannot be exhaustive.

    --
    Andrew Clover
    mailto:
    http://www.doxdesk.com/
    Andrew Clover, Jan 7, 2004
    #3
  4. 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.



    (F. Schaefer) wrote in message news:<>...
    > 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.
    Lonnie Princehouse, Jan 7, 2004
    #4
  5. On 7 Jan 2004 00:51:45 -0800, rumours say that
    (F. Schaefer) might have written:

    >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.
    --
    TZOTZIOY, I speak England very best,
    Ils sont fous ces Redmontains! --Harddix
    Christos TZOTZIOY Georgiou, Jan 8, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paddy McCarthy
    Replies:
    3
    Views:
    696
    Anthony J Bybell
    Sep 24, 2004
  2. wanwan
    Replies:
    3
    Views:
    415
    Alex Martelli
    Oct 14, 2005
  3. 7stud
    Replies:
    11
    Views:
    668
    Dennis Lee Bieber
    Mar 20, 2007
  4. News123
    Replies:
    2
    Views:
    456
    John Machin
    Nov 26, 2008
  5. Peter Buckley
    Replies:
    5
    Views:
    194
    matt neuburg
    Feb 27, 2009
Loading...

Share This Page