diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2012-07-29 10:30:36 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2012-07-29 10:30:36 (GMT) |
commit | 5ee9892406c4299826f553b6b73c046229c6040c (patch) | |
tree | ae358fd32ceea1f0debd2fad2a94e7100e734d7c /Python/import.c | |
parent | bb9b1c165d0193e6783981c1ac1e0ab9bf171c4c (diff) | |
download | cpython-5ee9892406c4299826f553b6b73c046229c6040c.zip cpython-5ee9892406c4299826f553b6b73c046229c6040c.tar.gz cpython-5ee9892406c4299826f553b6b73c046229c6040c.tar.bz2 |
Close #15425: Eliminate more importlib related traceback noise
Diffstat (limited to 'Python/import.c')
-rw-r--r-- | Python/import.c | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/Python/import.c b/Python/import.c index 982c389..c4edfac 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1154,14 +1154,27 @@ remove_importlib_frames(void) { const char *importlib_filename = "<frozen importlib._bootstrap>"; const char *exec_funcname = "_exec_module"; + const char *get_code_funcname = "get_code"; + const char *recursive_import = "_recursive_import"; int always_trim = 0; + int trim_get_code = 0; int in_importlib = 0; PyObject *exception, *value, *base_tb, *tb; PyObject **prev_link, **outer_link = NULL; /* Synopsis: if it's an ImportError, we trim all importlib chunks - from the traceback. Otherwise, we trim only those chunks which - end with a call to "_exec_module". */ + from the traceback. If it's a SyntaxError, we trim any chunks that + end with a call to "get_code", We always trim chunks + which end with a call to "_exec_module". */ + + /* Thanks to issue 15425, we also strip any chunk ending with + * _recursive_import. This is used when making a recursive call to the + * full import machinery which means the inner stack gets stripped early + * and the normal heuristics won't fire properly for outer frames. A + * more elegant mechanism would be nice, as this one can misfire if + * builtins.__import__ has been replaced with a custom implementation. + * However, the current approach at least gets the job done. + */ PyErr_Fetch(&exception, &value, &base_tb); if (!exception || Py_VerboseFlag) @@ -1169,6 +1182,9 @@ remove_importlib_frames(void) if (PyType_IsSubtype((PyTypeObject *) exception, (PyTypeObject *) PyExc_ImportError)) always_trim = 1; + if (PyType_IsSubtype((PyTypeObject *) exception, + (PyTypeObject *) PyExc_SyntaxError)) + trim_get_code = 1; prev_link = &base_tb; tb = base_tb; @@ -1191,8 +1207,14 @@ remove_importlib_frames(void) if (in_importlib && (always_trim || - PyUnicode_CompareWithASCIIString(code->co_name, - exec_funcname) == 0)) { + (PyUnicode_CompareWithASCIIString(code->co_name, + exec_funcname) == 0) || + (PyUnicode_CompareWithASCIIString(code->co_name, + recursive_import) == 0) || + (trim_get_code && + PyUnicode_CompareWithASCIIString(code->co_name, + get_code_funcname) == 0) + )) { PyObject *tmp = *outer_link; *outer_link = next; Py_XINCREF(next); |