Having had a look through the importlib source code, I believe you could subclass PyLoader in the _bootstrap module and 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 you know what you're doing, but on behalf of programmers everywhere I believe I should say: ugh =p