-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-102799: Let pydoc use the exception instead of sys.exc_info #102830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -389,6 +389,9 @@ def synopsis(filename, cache={}): | |
class ErrorDuringImport(Exception): | ||
"""Errors that occurred while trying to import something to document it.""" | ||
def __init__(self, filename, exc_info): | ||
if not isinstance(exc_info, tuple): | ||
assert isinstance(exc_info, BaseException) | ||
exc_info = type(exc_info), exc_info, exc_info.__traceback__ | ||
self.filename = filename | ||
self.exc, self.value, self.tb = exc_info | ||
|
||
|
@@ -411,8 +414,8 @@ def importfile(path): | |
spec = importlib.util.spec_from_file_location(name, path, loader=loader) | ||
try: | ||
return importlib._bootstrap._load(spec) | ||
except: | ||
raise ErrorDuringImport(path, sys.exc_info()) | ||
except BaseException as e: | ||
raise ErrorDuringImport(path, e) | ||
|
||
def safeimport(path, forceload=0, cache={}): | ||
"""Import a module; handle errors; return None if the module isn't found. | ||
|
@@ -440,21 +443,20 @@ def safeimport(path, forceload=0, cache={}): | |
cache[key] = sys.modules[key] | ||
del sys.modules[key] | ||
module = __import__(path) | ||
except: | ||
except BaseException as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally prefer 'err' to 'e'. I have no idea of any general consensus. |
||
# Did the error occur before or after the module was found? | ||
(exc, value, tb) = info = sys.exc_info() | ||
if path in sys.modules: | ||
# An error occurred while executing the imported module. | ||
raise ErrorDuringImport(sys.modules[path].__file__, info) | ||
elif exc is SyntaxError: | ||
raise ErrorDuringImport(sys.modules[path].__file__, e) | ||
elif type(e) is SyntaxError: | ||
# A SyntaxError occurred before we could execute the module. | ||
raise ErrorDuringImport(value.filename, info) | ||
elif issubclass(exc, ImportError) and value.name == path: | ||
raise ErrorDuringImport(e.filename, e) | ||
elif isinstance(e, ImportError) and e.name == path: | ||
# No such module in the path. | ||
return None | ||
else: | ||
# Some other error occurred during the importing process. | ||
raise ErrorDuringImport(path, sys.exc_info()) | ||
raise ErrorDuringImport(path, e) | ||
for part in path.split('.')[1:]: | ||
try: module = getattr(module, part) | ||
except AttributeError: return None | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate suggestions:
I am tempted to think that no user will ever raise this exception. pydoc is only documented as a command-line app, not as importable library. But this is not idlelib. Can/should we emit a DeprecationWarning in the isinstance clause? It could then be removed someday. Since there is no doc of the module content, no changed notice is needed.