Unknown encoding: idna in socket.py

Now beginning to wonder if this is a Python 3.15 only problem.

Found a similar import hook example here: https://stackoverflow.com/questions/43571737/how-to-implement-an-import-hook-that-can-modify-the-source-code-on-the-fly-using/43573798#comment141219085_43573798 (second example by Dunes) which also suffers from the same issue.

I’m the one who posted the original question on that StackOverflow thread which eventually led me to implement my ideas library which I have mentioned before and which works with Python 3.15 (and can handle source code transformations). It also never calls sys.path_importer_cache.clear().

@aroberge so how did you get that working without the sys.path_importer_cache.clear()

Inserting a finder as the first item in sys.path_hooks means that it will be the first finder Python will use to attempt to find a script to be imported; if it works, having other finders available is irrelevant as they will not be used.

However, instead of inserting a finder in sys.path_hooks like you do, I insert it in sys.meta_path. Perhaps doing the same might solve your problem.

Have now tried

sys.meta_path.insert(0, FileFinder.path_hook( (PreProcessLoader, [".py"]) ))

but source_to_code just doesn’t ever get called then.

Further testing suggests that when it comes to import unicodedata Python is not even calling my source_to_code SourceFileLoader.

I can’t promise this’ll solve all of your issues, but decorating a class with @staticmethod is not likely to help

Changed that now

It isn’t, the problem is in sys.path_hooks.insert(0, FileFinder.path_hook( (PreProcessLoader, \[".py"\]) )). That statement only adds support for .py files.

PEP 302 says this about path hooks:

sys.path_hooks is a list of callables, which will be checked in sequence to determine if they can handle a given path item. The callable is called with one argument, the path item. The callable must raise ImportErrorif it is unable to handle the path item, and return an importer object if it can handle the path item.

The path hook for FileFinder handles every existing directory on sys.path. Because of this your hook replaces the default hook that loads extension modules (and modules that only have a bytecode file)

So the question is then how do I stop my custom FileFinder path hook from intercepting import calls for extension, and other, modules? I would have thought the [".py"] should do that but obviously not.

raise ImportError doesn’t seem to help much:

import sys
from importlib.machinery import SourceFileLoader, FileFinder

class PreProcessLoader(SourceFileLoader): # SourceLoader
    def source_to_code(self, data, path, fullname, *, _optimize: int = -1):
        print("In source_to_code 1", path)
        raise ImportError
        return super().source_to_code(data, path, fullname, _optimize=_optimize)

sys.path_hooks.insert(0, FileFinder.path_hook( (PreProcessLoader, [".py"]) ))
sys.path_importer_cache.clear()

import unicodedata

Still fails with:
ModuleNotFoundError: Standard library module 'unicodedata' was not found

You shouldn’t be raising it unconditionally. Not sure if it’ll solve the issue though.

@jonathandung Only did that to demonstrate for testing; it proved that raising the error doesn’t then pass the path item search onto the next finder as PEP 302 suggests it should.