global param to exec (on win32)

david evans david.evans at ittransition.co.uk
Fri Jul 26 15:12:34 EDT 2002


I'm using win32 and I can't work out how "globals()" and the global param to
"exec" relate to each other

I'll be really grateful if anybody takes time to read to the end of this
rather long description of my problem

Thanks in advance

David Evans

-----

I've read the spec for exec at  www.python.org  and I think  I can use the
global/local params in the
same way as I would use "env" when exec'ing C program but I can't make it
work like I expect.

>From VB, I'm successfully calling functions from within a class that is
registered as an COM object (as per Ch5 PPW32).

This class contains a variable representing the "mx.ODBC" connection that I
need to share with other code (because of a restriction in the underlying
ODBC implementation I cant create multiple connections).  This variable is
"self.sdk" within the class.

The client VB code successfully calls many functions from within the class
but I also want to be able to execute (user definable) functions from an
external .py file such that these functions can also use the mx.ODBC
connection and there seems to be a catch 22:

      with the external .py file containing:

      def outside_func():
              xxx = sdk

* if I run:
        from external_file import *
        exec "outside_func()" , {'sdk':self.sdk}

        the call fails because it can't find outside_func()

* if I run:
        from external_file import *
        exec "outside_func()"

        the call works but the called function complains that it can't find
global 'sdk'

* if I run:
        # construct "my_dictionary" variable containing outside_func and sdk
        exec "outside_func()", my_dictionary

        the call works but the called function complaiss that it can't find
global 'sdk'
        and when I print globals() from within the called function, its the
same as globals()
        from within the caller, but I expect it to be the same as
"my_dictionary" within the
        caller.

Whatever I specify for the "local" param  in my exec call,  the target sees
"locals()" as its
param list.

Have I misunderstood the way that global/local params to exec work?
------

(thanks again if you got this far!)






More information about the Python-list mailing list