[Python-Dev] Fix import errors to have data (original) (raw)

Christian Tismer tismer at stackless.com
Mon Aug 2 11:01:26 CEST 2004


Greg Ewing wrote:

Circular imports, to be made consistent, would need something like a final "commit" after all imports succeeded.

Silly idea. Typically, circular imports aren't planned, they just happen, and (usually) just happen to work. I thought Christian meant this was something that would be done automatically, not something the user would have to do explicitly. Maybe it could be as simple as saving a snapshot of sys.modules whenever importing of a module is begun, and if execution of its body doesn't complete, restoring the snapshot?

This is exactly what I thought of. It seems to be correct, also with nested imports, since this is a stack-like structure of undoable things.

Example

main: import A #1 #6

A: try: import B #2 #5 except (ImportError, maybe others): # provide surrogate for B

 # other action that fails

B: import A #3 import C #4 # do some stuff that fails

#1: snapshot of sys.modules, A added [main, A] #2: snapshot of sys.modules, B added [main, A, B] #3: snapshot of sys.modules, A exists [main, A, B] #3: snapshot of sys.modules, C added [main, A, B, C]

after failing imports:

#5: restore [main, A] #5: restore [main]

So one side effect is that completely valid imports in the context of a failing import are undone. I guess this is intended.

A problem are side effects, of course. It would be nice to have a cleanup construct for completely or partially imported modules, to undo any side effects. How do we remove extension modules which have been loaded as a side effect?

ciao - chris

Christian Tismer :^) <mailto:tismer at stackless.com> Mission Impossible 5oftware : Have a break! Take a ride on Python's Johannes-Niemeyer-Weg 9a : Starship http://starship.python.net/ 14109 Berlin : PGP key -> http://wwwkeys.pgp.net/ work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776 PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04 whom do you want to sponsor today? http://www.stackless.com/



More information about the Python-Dev mailing list