Skip to content

How can I use pytest within an embedded Python environment with __debug__ == False? #12460

Discussion options

You must be logged in to vote

It looks like sys.path_hooks[1] returns instances of an old FileFinder class that is not reachable at importlib.machinery.FileFinder. The spec objects returned by its find_spec method also refer to an old version of SourceFileLoader. That's what causes the isinstance check to fail.

The simplest solution I've found is to extract a reference to the old class and replace the new class, i.e.

importlib.machinery.SourceFileLoader = sys.path_hooks[1].__globals__["SourceFileLoader"]

As best I can tell, this doesn't cause any side effects.

This has been a strange adventure, but I think it ends here.

Replies: 1 comment 15 replies

Comment options

You must be logged in to vote
15 replies
@bradleyharden
Comment options

@bradleyharden
Comment options

@bradleyharden
Comment options

@bradleyharden
Comment options

Answer selected by bradleyharden
@RonnyPfannschmidt
Comment options

@bradleyharden
Comment options

@RonnyPfannschmidt
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants