How to use importlib to overwrite bytecode?

I am looking for a way to use importlib in Python 2.x to overwrite the bytecode of imported modules in fly-by mode. In other words, I need to link my own function between the compilation and execution steps during import. In addition, I want the import function to work the same as the built-in function.

I already did this with imputil, but this library does not apply to all cases and is deprecated anyway.

+3
source share
1 answer

Looking through the source code importlib, I believe that you can subclass PyLoaderin the module _bootstrapand override get_code:

class PyLoader:
    ...

    def get_code(self, fullname):
    """Get a code object from source."""
    source_path = self.source_path(fullname)
    if source_path is None:
        message = "a source path must exist to load {0}".format(fullname)
        raise ImportError(message)
    source = self.get_data(source_path)
    # Convert to universal newlines.
    line_endings = b'\n'
    for index, c in enumerate(source):
        if c == ord(b'\n'):
            break
        elif c == ord(b'\r'):
            line_endings = b'\r'
            try:
                if source[index+1] == ord(b'\n'):
                    line_endings += b'\n'
            except IndexError:
                pass
            break
    if line_endings != b'\n':
        source = source.replace(line_endings, b'\n')

    # modified here
    code = compile(source, source_path, 'exec', dont_inherit=True)
    return rewrite_code(code)

I assume that you know what you are doing, but on behalf of the programmers everywhere I think I have to say: ugh = p

+2
source

Source: https://habr.com/ru/post/1765996/


All Articles