Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I'm working on pypreprocessor which is a preprocessor that takes c-style directives and I've been able to make it work like a traditional preprocessor (it's self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports.

The problem is: The preprocessor runs through the file, processes it, outputs to a temporary file, and exec() the temporary file. Libraries that are imported need to be handled a little different, because they aren't executed, but rather they are loaded and made accessible to the caller module.

What I need to be able to do is: Interrupt the import (since the preprocessor is being run in the middle of the import), load the postprocessed code as a tempModule, and replace the original import with the tempModule to trick the calling script with the import into believing that the tempModule is the original module.

I have searched everywhere and so far and have no solution.

This Stack Overflow question is the closest I've seen so far to providing an answer: Override namespace in PythonOverride namespace in Python

Here's what I have.

# Remove the bytecode file created by the first import
os.remove(moduleName + '.pyc')

# Remove the first import
del sys.modules[moduleName]

# Import the postprocessed module
tmpModule = __import__(tmpModuleName)

# Set first module's reference to point to the preprocessed module
sys.modules[moduleName] = tmpModule

moduleName is the name of the original module, and tmpModuleName is the name of the postprocessed code file.

The strange part is this solution still runs completely normal as if the first module completed loaded normally; unless you remove the last line, then you get a module not found error.

Hopefully someone on Stack Overflow know a lot more about imports than I do, because this one has me stumped.

Note: I will only award a solution, or, if this is not possible in Python; the best, most detailed explanation of why this is not impossible.

Update: For anybody who is interested, here is the working code.

if imp.lock_held() is True:
    del sys.modules[moduleName]
    sys.modules[tmpModuleName] = __import__(tmpModuleName)
    sys.modules[moduleName] = __import__(tmpModuleName)

The 'imp.lock_held' part detects whether the module is being loaded as a library. The following lines do the rest.

I'm working on pypreprocessor which is a preprocessor that takes c-style directives and I've been able to make it work like a traditional preprocessor (it's self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports.

The problem is: The preprocessor runs through the file, processes it, outputs to a temporary file, and exec() the temporary file. Libraries that are imported need to be handled a little different, because they aren't executed, but rather they are loaded and made accessible to the caller module.

What I need to be able to do is: Interrupt the import (since the preprocessor is being run in the middle of the import), load the postprocessed code as a tempModule, and replace the original import with the tempModule to trick the calling script with the import into believing that the tempModule is the original module.

I have searched everywhere and so far and have no solution.

This Stack Overflow question is the closest I've seen so far to providing an answer: Override namespace in Python

Here's what I have.

# Remove the bytecode file created by the first import
os.remove(moduleName + '.pyc')

# Remove the first import
del sys.modules[moduleName]

# Import the postprocessed module
tmpModule = __import__(tmpModuleName)

# Set first module's reference to point to the preprocessed module
sys.modules[moduleName] = tmpModule

moduleName is the name of the original module, and tmpModuleName is the name of the postprocessed code file.

The strange part is this solution still runs completely normal as if the first module completed loaded normally; unless you remove the last line, then you get a module not found error.

Hopefully someone on Stack Overflow know a lot more about imports than I do, because this one has me stumped.

Note: I will only award a solution, or, if this is not possible in Python; the best, most detailed explanation of why this is not impossible.

Update: For anybody who is interested, here is the working code.

if imp.lock_held() is True:
    del sys.modules[moduleName]
    sys.modules[tmpModuleName] = __import__(tmpModuleName)
    sys.modules[moduleName] = __import__(tmpModuleName)

The 'imp.lock_held' part detects whether the module is being loaded as a library. The following lines do the rest.

I'm working on pypreprocessor which is a preprocessor that takes c-style directives and I've been able to make it work like a traditional preprocessor (it's self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports.

The problem is: The preprocessor runs through the file, processes it, outputs to a temporary file, and exec() the temporary file. Libraries that are imported need to be handled a little different, because they aren't executed, but rather they are loaded and made accessible to the caller module.

What I need to be able to do is: Interrupt the import (since the preprocessor is being run in the middle of the import), load the postprocessed code as a tempModule, and replace the original import with the tempModule to trick the calling script with the import into believing that the tempModule is the original module.

I have searched everywhere and so far and have no solution.

This Stack Overflow question is the closest I've seen so far to providing an answer: Override namespace in Python

Here's what I have.

# Remove the bytecode file created by the first import
os.remove(moduleName + '.pyc')

# Remove the first import
del sys.modules[moduleName]

# Import the postprocessed module
tmpModule = __import__(tmpModuleName)

# Set first module's reference to point to the preprocessed module
sys.modules[moduleName] = tmpModule

moduleName is the name of the original module, and tmpModuleName is the name of the postprocessed code file.

The strange part is this solution still runs completely normal as if the first module completed loaded normally; unless you remove the last line, then you get a module not found error.

Hopefully someone on Stack Overflow know a lot more about imports than I do, because this one has me stumped.

Note: I will only award a solution, or, if this is not possible in Python; the best, most detailed explanation of why this is not impossible.

Update: For anybody who is interested, here is the working code.

if imp.lock_held() is True:
    del sys.modules[moduleName]
    sys.modules[tmpModuleName] = __import__(tmpModuleName)
    sys.modules[moduleName] = __import__(tmpModuleName)

The 'imp.lock_held' part detects whether the module is being loaded as a library. The following lines do the rest.

Copy edited.
Source Link
Peter Mortensen
  • 31.2k
  • 22
  • 110
  • 134

How do I override a pythonPython import?

So I'm working on pypreprocessor which is a preprocessor that takes c-style directives and I've been able to make it work like a traditional preprocessor (it's self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports.

The problem is.: The preprocessor runs through the file, processes'processes it, outputs to a temptemporary file, and exec() the temptemporary file. Libraries that are imported need to be handled a little different, because they aren't executed, but rather they are loaded and made accessible to the caller module.

What I need to be able to do is.: Interrupt the import (since the preprocessor is being run in the middle of the import), load the postprocessed code as a tempModule, and replace the original import with the tempModule to trick the calling script with the import into believing that the tempModule is the original module.

I have searched everywhere and so far, and have no solution.

This Stack Overflow question is the closest I've seen so far to providing an answer: Override namespace in Python

Here's what I have.

# removeRemove the bytecode file created by the first import
os.remove(moduleName + '.pyc') 

# removeRemove the first import
del sys.modules[moduleName] 

# importImport the postprocessed module
tmpModule = __import__(tmpModuleName) 

# setSet first module's reference to point to the preprocessed module
sys.modules[moduleName] = tmpModule

moduleName is the name of the original module, and tmpModuleName is the name of the postprocessed code file.

The strange part is, this solution still runs completely normal as if the first module completed loaded normally; unless you remove the last line, then you get a module not found error.

Hopefully someone on SOStack Overflow know a lot more about imports than I do, because this one has me stumped.

Note: I will only award a solution, or, if this is not possible in python;Python; the best, most detailed explanation of why this is not impossible.

Update: For anybody who is interested, here is the working code.

if imp.lock_held() is True:
    del sys.modules[moduleName]
    sys.modules[tmpModuleName] = __import__(tmpModuleName)
    sys.modules[moduleName] = __import__(tmpModuleName)

The 'imp.lock_held' part detects whether the module is being loaded as a library. The following lines do the rest.

How do I override a python import?

So I'm working on pypreprocessor which is a preprocessor that takes c-style directives and I've been able to make it work like a traditional preprocessor (it's self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports.

The problem is. The preprocessor runs through the file, processes' it, outputs to a temp file, and exec() the temp file. Libraries that are imported need to be handled a little different because they aren't executed but rather loaded and made accessible to the caller module.

What I need to be able to do is. Interrupt the import (since the preprocessor is being run in the middle of the import), load the postprocessed code as a tempModule, and replace the original import with the tempModule to trick the calling script with the import into believing that the tempModule is the original module.

I have searched everywhere and so far, have no solution.

This question is the closest I've seen so far to providing an answer: Override namespace in Python

Here's what I have.

# remove the bytecode file created by the first import
os.remove(moduleName + '.pyc')
# remove the first import
del sys.modules[moduleName]
# import the postprocessed module
tmpModule = __import__(tmpModuleName)
# set first module's reference to point to the preprocessed module
sys.modules[moduleName] = tmpModule

moduleName is the name of the original module, tmpModuleName is the name of the postprocessed code file.

The strange part is, this solution still runs completely normal as if the first module completed loaded normally; unless you remove the last line, then you get a module not found error.

Hopefully someone on SO know a lot more about imports than I do because this one has me stumped.

Note: I will only award a solution, or, if this is not possible in python; the best, most detailed explanation of why this is not impossible.

Update: For anybody who is interested here is the working code.

if imp.lock_held() is True:
    del sys.modules[moduleName]
    sys.modules[tmpModuleName] = __import__(tmpModuleName)
    sys.modules[moduleName] = __import__(tmpModuleName)

The 'imp.lock_held' part detects whether the module is being loaded as a library. The following lines do the rest.

How do I override a Python import?

I'm working on pypreprocessor which is a preprocessor that takes c-style directives and I've been able to make it work like a traditional preprocessor (it's self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports.

The problem is: The preprocessor runs through the file, processes it, outputs to a temporary file, and exec() the temporary file. Libraries that are imported need to be handled a little different, because they aren't executed, but rather they are loaded and made accessible to the caller module.

What I need to be able to do is: Interrupt the import (since the preprocessor is being run in the middle of the import), load the postprocessed code as a tempModule, and replace the original import with the tempModule to trick the calling script with the import into believing that the tempModule is the original module.

I have searched everywhere and so far and have no solution.

This Stack Overflow question is the closest I've seen so far to providing an answer: Override namespace in Python

Here's what I have.

# Remove the bytecode file created by the first import
os.remove(moduleName + '.pyc') 

# Remove the first import
del sys.modules[moduleName] 

# Import the postprocessed module
tmpModule = __import__(tmpModuleName) 

# Set first module's reference to point to the preprocessed module
sys.modules[moduleName] = tmpModule

moduleName is the name of the original module, and tmpModuleName is the name of the postprocessed code file.

The strange part is this solution still runs completely normal as if the first module completed loaded normally; unless you remove the last line, then you get a module not found error.

Hopefully someone on Stack Overflow know a lot more about imports than I do, because this one has me stumped.

Note: I will only award a solution, or, if this is not possible in Python; the best, most detailed explanation of why this is not impossible.

Update: For anybody who is interested, here is the working code.

if imp.lock_held() is True:
    del sys.modules[moduleName]
    sys.modules[tmpModuleName] = __import__(tmpModuleName)
    sys.modules[moduleName] = __import__(tmpModuleName)

The 'imp.lock_held' part detects whether the module is being loaded as a library. The following lines do the rest.

added 388 characters in body
Source Link
Evan Plaice
  • 14.2k
  • 6
  • 78
  • 95

So I'm working on [pypreprocessor][1]pypreprocessor which is a preprocessor that takes c-style directives and I've been able to make it work like a traditional preprocessor (it's self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports.

The problem is. The preprocessor runs through the file, processes' it, outputs to a temp file, and exec() the temp file. Libraries that are imported need to be handled a little different because they aren't executed but rather loaded and made accessible to the caller module.

What I need to be able to do is. Interrupt the import (since the preprocessor is being run in the middle of the import), load the postprocessed code as a tempModule, and replace the original import with the tempModule to trick the calling script with the import into believing that the tempModule is the original module.

I have searched everywhere and so far, have no solution.

This question is the closest I've seen so far to providing an answer: Override namespace in Python

Here's what I have.

# remove the bytecode file created by the first import
os.remove(moduleName + '.pyc')
# remove the first import
del sys.modules[moduleName]
# import the postprocessed module
tmpModule = __import__(tmpModuleName)
# set first module's reference to point to the preprocessed module
sys.modules[moduleName] = tmpModule

moduleName is the name of the original module, tmpModuleName is the name of the postprocessed code file.

The strange part is, this solution still runs completely normal as if the first module completed loaded normally; unless you remove the last line, then you get a module not found error.

Hopefully someone on SO know a lot more about imports than I do because this one has me stumped.

Note: I will only award a solution, or, if this is not possible in python; the best, most detailed explanation of why this is not impossible. [1]: http://code.google.com/p/pypreprocessor/

Update: For anybody who is interested here is the working code.

if imp.lock_held() is True:
    del sys.modules[moduleName]
    sys.modules[tmpModuleName] = __import__(tmpModuleName)
    sys.modules[moduleName] = __import__(tmpModuleName)

The 'imp.lock_held' part detects whether the module is being loaded as a library. The following lines do the rest.

So I'm working on [pypreprocessor][1] which is a preprocessor that takes c-style directives and I've been able to make it work like a traditional preprocessor (it's self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports.

The problem is. The preprocessor runs through the file, processes' it, outputs to a temp file, and exec() the temp file. Libraries that are imported need to be handled a little different because they aren't executed but rather loaded and made accessible to the caller module.

What I need to be able to do is. Interrupt the import (since the preprocessor is being run in the middle of the import), load the postprocessed code as a tempModule, and replace the original import with the tempModule to trick the calling script with the import into believing that the tempModule is the original module.

I have searched everywhere and so far, have no solution.

This question is the closest I've seen so far to providing an answer: Override namespace in Python

Here's what I have.

# remove the bytecode file created by the first import
os.remove(moduleName + '.pyc')
# remove the first import
del sys.modules[moduleName]
# import the postprocessed module
tmpModule = __import__(tmpModuleName)
# set first module's reference to point to the preprocessed module
sys.modules[moduleName] = tmpModule

moduleName is the name of the original module, tmpModuleName is the name of the postprocessed code file.

The strange part is, this solution still runs completely normal as if the first module completed loaded normally; unless you remove the last line, then you get a module not found error.

Hopefully someone on SO know a lot more about imports than I do because this one has me stumped.

Note: I will only award a solution, or, if this is not possible in python; the best, most detailed explanation of why this is not impossible. [1]: http://code.google.com/p/pypreprocessor/

So I'm working on pypreprocessor which is a preprocessor that takes c-style directives and I've been able to make it work like a traditional preprocessor (it's self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports.

The problem is. The preprocessor runs through the file, processes' it, outputs to a temp file, and exec() the temp file. Libraries that are imported need to be handled a little different because they aren't executed but rather loaded and made accessible to the caller module.

What I need to be able to do is. Interrupt the import (since the preprocessor is being run in the middle of the import), load the postprocessed code as a tempModule, and replace the original import with the tempModule to trick the calling script with the import into believing that the tempModule is the original module.

I have searched everywhere and so far, have no solution.

This question is the closest I've seen so far to providing an answer: Override namespace in Python

Here's what I have.

# remove the bytecode file created by the first import
os.remove(moduleName + '.pyc')
# remove the first import
del sys.modules[moduleName]
# import the postprocessed module
tmpModule = __import__(tmpModuleName)
# set first module's reference to point to the preprocessed module
sys.modules[moduleName] = tmpModule

moduleName is the name of the original module, tmpModuleName is the name of the postprocessed code file.

The strange part is, this solution still runs completely normal as if the first module completed loaded normally; unless you remove the last line, then you get a module not found error.

Hopefully someone on SO know a lot more about imports than I do because this one has me stumped.

Note: I will only award a solution, or, if this is not possible in python; the best, most detailed explanation of why this is not impossible.

Update: For anybody who is interested here is the working code.

if imp.lock_held() is True:
    del sys.modules[moduleName]
    sys.modules[tmpModuleName] = __import__(tmpModuleName)
    sys.modules[moduleName] = __import__(tmpModuleName)

The 'imp.lock_held' part detects whether the module is being loaded as a library. The following lines do the rest.

Bounty Ended with Ron's answer chosen by Evan Plaice
added 132 characters in body; added 15 characters in body
Source Link
Evan Plaice
  • 14.2k
  • 6
  • 78
  • 95
Loading
Bounty Started worth 400 reputation by Evan Plaice
Source Link
Evan Plaice
  • 14.2k
  • 6
  • 78
  • 95
Loading