Code Objects / Variable Names

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • F. Schaefer

    Code Objects / Variable Names

    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.
  • Francis Avila

    #2
    Re: Code Objects / Variable Names

    F. Schaefer wrote in message ...[color=blue]
    >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.[/color]

    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).
    [color=blue]
    >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.[/color]

    [color=blue][color=green][color=darkred]
    >>> cobj = compile('(door == "ajar" and alternator == "off") or time >[/color][/color][/color]
    25', '<stdin>', 'exec')[color=blue][color=green][color=darkred]
    >>> dir(cobj)[/color][/color][/color]
    ['__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'][color=blue][color=green][color=darkred]
    >>> cobj.co_names[/color][/color][/color]
    ('door', 'alternator', 'time')

    See "Code objects" under "Internal Types" in section 3.2, "Standard Type
    Hierarchy," of the Python Language Reference Manual.
    [color=blue][color=green][color=darkred]
    >>> import dis #just for fun.
    >>> dis.disco(cobj)[/color][/color][/color]
    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 (==)[color=blue][color=green]
    >> 22 JUMP_IF_TRUE 10 (to 35)[/color][/color]
    25 POP_TOP
    26 LOAD_NAME 2 (time)
    29 LOAD_CONST 2 (25)
    32 COMPARE_OP 4 (>)[color=blue][color=green]
    >> 35 POP_TOP[/color][/color]
    36 LOAD_CONST 3 (None)
    39 RETURN_VALUE

    --
    Francis Avila

    Comment

    • Andrew Clover

      #3
      Re: Code Objects / Variable Names

      drfransch@netsc ape.net (F. Schaefer) wrote:
      [color=blue]
      > How can you know what variables are involved in a code object.[/color]


      [color=blue]
      > (door == "ajar" and alternator == "off") || time > 25[/color]
      [color=blue]
      > I need to extract, that the variables 'door', 'alternator' and 'time'
      > are involved.[/color]
      [color=blue][color=green][color=darkred]
      >>> c= compile('door== "ajar" and alternator=="of f" or time>25', '', 'eval')
      >>> c.co_names[/color][/color][/color]
      ('door', 'alternator', 'time')

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

      --
      Andrew Clover
      mailto:and@doxd esk.com

      Comment

      • Lonnie Princehouse

        #4
        Re: Code Objects / Variable Names

        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.



        drfransch@netsc ape.net (F. Schaefer) wrote in message news:<f8dfa927. 0401070051.39cd ed0c@posting.go ogle.com>...[color=blue]
        > 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.[/color]

        Comment

        • Christos TZOTZIOY Georgiou

          #5
          Re: Code Objects / Variable Names

          On 7 Jan 2004 00:51:45 -0800, rumours say that drfransch@netsc ape.net
          (F. Schaefer) might have written:
          [color=blue]
          >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.[/color]

          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

          Comment

          Working...