Logging global assignments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jacek Generowicz

    #1

    Logging global assignments

    I am dealing with an application which reads in configurations by
    calling (a wrapper around) execfile. Any configuration file may itself
    execfile other configuration files in the same manner.

    I would like to create a log containing all global assignments made in
    these files. Comparing the globals dictionaries before and after is
    not quite enough, as

    a) this misses multiple assignments to the same name, within a single
    file,

    b) doesn't allow me to pinpoint the line at which the assignment is
    made.

    Inspecting the implementation of execfile suggests to me that the
    assignments are performed by a hard-wired call to PyDict_SetItem, in
    the opcode implementations , so it looks like ideas based on giving
    execfile a globals dictionary with an instrumented __setitem__ are
    probably doomed to failure.

    Is there any way of injecting some of my code into the process of
    global name binding, or some other means of "capturing" global
    assignments ?
  • Larry Bates

    #2
    Re: Logging global assignments

    Jacek Generowicz wrote:[color=blue]
    > I am dealing with an application which reads in configurations by
    > calling (a wrapper around) execfile. Any configuration file may itself
    > execfile other configuration files in the same manner.
    >
    > I would like to create a log containing all global assignments made in
    > these files. Comparing the globals dictionaries before and after is
    > not quite enough, as
    >
    > a) this misses multiple assignments to the same name, within a single
    > file,
    >
    > b) doesn't allow me to pinpoint the line at which the assignment is
    > made.
    >
    > Inspecting the implementation of execfile suggests to me that the
    > assignments are performed by a hard-wired call to PyDict_SetItem, in
    > the opcode implementations , so it looks like ideas based on giving
    > execfile a globals dictionary with an instrumented __setitem__ are
    > probably doomed to failure.
    >
    > Is there any way of injecting some of my code into the process of
    > global name binding, or some other means of "capturing" global
    > assignments ?[/color]

    Suggestion:
    Use ConfigParser to set your globals instead and you can track
    your assignments over each file, section and option in the file.

    Larry Bates

    Comment

    • Jacek Generowicz

      #3
      Re: Logging global assignments

      Larry Bates <lbates@syscono nline.com> writes:
      [color=blue]
      > Suggestion:
      > Use ConfigParser to set your globals instead and you can track
      > your assignments over each file, section and option in the file.[/color]

      I would dearly love to go down this sort of route but, unfortunately,
      they are not _my_ configuration files, and it is not _my_
      configuration mechanism ... it's just the one I have to work with. I
      do not have the freedom to change it; I merely have to try to make
      some sense out of it :-(

      Comment

      • Peter Otten

        #4
        Re: Logging global assignments

        Jacek Generowicz wrote:
        [color=blue]
        > Inspecting the implementation of execfile suggests to me that the
        > assignments are performed by a hard-wired call to PyDict_SetItem, in
        > the opcode implementations , so it looks like ideas based on giving
        > execfile a globals dictionary with an instrumented __setitem__ are
        > probably doomed to failure.[/color]

        For Python 2.4 here is evidence against that assumption:

        $ cat config.py
        a = 42
        b = "so what"

        [color=blue][color=green]
        >> class Dict(dict):[/color][/color]
        .... def __setitem__(sel f, k, v):
        .... print k, "->", v
        .... dict.__setitem_ _(self, k, v)
        ....[color=blue][color=green][color=darkred]
        >>> execfile("confi g.py", Dict())[/color][/color][/color]
        a -> 42
        b -> so what

        We are all doomed to succeed, then...

        Peter



        Comment

        • brianmce@gmail.com

          #5
          Re: Logging global assignments

          In 2.4 at least, it looks like execfile can take an arbirary mapping
          object for globals, so something like this may work:

          class LoggedDict(dict ):
          def __setitem__(sel f, item, val):
          dict.__setitem_ _(self, item, val)
          # Do whatever logging is needed here
          print "Assignment made: %s = %r" % (item, val)

          # When loading your config file use:
          logged_globals = LoggedDict()
          execfile(my_con fig_file, logged_globals)

          Note that this covers all name binding (ie. function and class
          definitions etc), not just assignments.

          This might not work with earlier versions of python, since I think
          these may restrict the globals argument to a real dictionary.

          Comment

          Working...