Loading new code...

Olivier Dagenais olivierS.dagenaisP at canadaA.comM
Fri Aug 11 22:58:26 EDT 2000


Gordon,

First, thanks for answering, I didn't think I was going to get an answer...

> What you're trying to do can't be done, because there is no "global"
> namespace. If you have 2 modules that both want to see module X, they both
> must "import X". Only one of those imports will involve any actual work,
> the second one will just gain access to the already imported module

Fair enough, I had to at least ask...  It would have been silly to do as you
say if it wasn't necessary...  And the last thing I want to do is to code
something silly...

> You want to use ihooks or imputil (Greg Stein's module on lyra.org).
You'll
> probably find the latter easier to use. The imp module really only gives
> you access to some low level routines in the import process. There's a lot
> more than goes on than just getting the code object. (And what you
actually
> want to do at the bottom level is unmarshal a compiled code object, not
> exec it.)

Sorry, I should have been more precise:  there's actual Python instructions
in the database, not "compiled code objects"...  Does that make it easier?
I get that idea from the documentation, saying a function or class
definition must be "declared" before it is used (sort of like not using
forward declarations in C), so, if I exec some code that has a class
definition before I attempt to construct it, am I good to go?  For example:

# open database
cursor.execute ( "SELECT Instructions FROM Code" )
for class_definition in cursor.fetchall ( ):
    exec ( class_definition[0] )


...so, knowing that I have just loaded [from the database] a class called,
say, "Blarg", can I do:

exec ( "self.aNewBlarg = Blarg ( 'arg1', 'arg2' )" )


> But reloading a module is another can of worms. For one thing, reloading
> does no good unless everyone is accessing the module through the module
> object (IOW, one "from X import ..." and you're screwed).

Understood.  So, assuming there are no "from X import ..." (or no "import
...", either), can I simply re-exec the class definition(s) that have been
changed?  (reloading isn't THAT important, however loading arbitrary classes
whose names are unknown is a priority)

Thanks!

--
----------------------------------------------------------------------
Olivier A. Dagenais - Carleton University - Computer Science III







More information about the Python-list mailing list