Robert <[email protected]> added the comment:
You see the usecase from the stack trace: PythonWin (the IDE from pywin32
package) uses pyclbr - to inspect arbitrary user code.
(Neither code is from me)
I'm not inspecting __main__ explicitely. The problem seems to arise in newer
Python versions (3.10+?) because the class browser now seems to parse imports
somehow recursively (_readmodule() several times in the stack trace) and when
user code somewhere contains e.g. "import __main__" ...
pyclbr should perhaps handle (not fail in) all legal cases w/o breaking: when
some strange builtin/extension/artificial has .__spec__ as None or not set or
no python code. (We cannot force any user code to monkey patch
__main__.__spec__ or potential other modules.)
>>> mod = types.ModuleType('mymod')
>>> mod.__spec__
# (None)
>>>
importlib.util._find_spec_from_path() has choosen to raise ValueError instead
of an extra custom Error (or a special return value) for those cases and to
return None for the similar case of 'not found') . Though those 3 cases are
similiar in consequence here. pyclbr also "cheaply abuses" ImportError /
ModuleNotFound to translate one of those cases (None return value) for internal
signaling. (There is never a real ImportError just remote linguistic similarity
;-) )
Hence the simple pragmatic fix by kind of reunification of signaling the "end
of the game" here - as the way of signaling here is anyway rather pragmatic and
evolution style.
ValueError is often (ab)used to signal application level errors "cheaply" (to
not define and distribute an extra Exception type) - and its a limited internal
case here where mix-up with errors from something like "math.sqrt(-1)" is not
possible w/o coding bugs (which are to be detected by tests)
But you may establish a more meticulous error / return value signaling system -
which though will require changes / refactoring in several places and
consideration for compatibility ...
(Think its hardly worth it)
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue43299>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com