[Python-checkins] gh-102799: use exception instance instead of sys.exc_info() (#102885)

iritkatriel webhook-mailer at python.org
Fri Mar 31 06:23:14 EDT 2023


https://github.com/python/cpython/commit/44bd3fe570da9115bec67694404b8da26716a1d7
commit: 44bd3fe570da9115bec67694404b8da26716a1d7
branch: main
author: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2023-03-31T11:23:02+01:00
summary:

gh-102799: use exception instance instead of sys.exc_info() (#102885)

files:
M Lib/logging/__init__.py
M Lib/pickle.py
M Lib/site.py
M Lib/test/inspect_fodder.py
M Lib/test/test_inspect.py
M Lib/test/test_with.py
M Lib/tkinter/filedialog.py

diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 9241d73d0fd0..056380fb2287 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -173,8 +173,8 @@ def currentframe():
         """Return the frame object for the caller's stack frame."""
         try:
             raise Exception
-        except Exception:
-            return sys.exc_info()[2].tb_frame.f_back
+        except Exception as exc:
+            return exc.__traceback__.tb_frame.f_back
 
 #
 # _srcfile is used when walking the stack to check when we've got the first
diff --git a/Lib/pickle.py b/Lib/pickle.py
index 15fa5f6e5799..fe86f80f51d3 100644
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -1481,7 +1481,7 @@ def _instantiate(self, klass, args):
                 value = klass(*args)
             except TypeError as err:
                 raise TypeError("in constructor for %s: %s" %
-                                (klass.__name__, str(err)), sys.exc_info()[2])
+                                (klass.__name__, str(err)), err.__traceback__)
         else:
             value = klass.__new__(klass)
         self.append(value)
diff --git a/Lib/site.py b/Lib/site.py
index 7faf1c6f6af2..5c1ff31f4e0f 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -190,11 +190,11 @@ def addpackage(sitedir, name, known_paths):
                 if not dircase in known_paths and os.path.exists(dir):
                     sys.path.append(dir)
                     known_paths.add(dircase)
-            except Exception:
+            except Exception as exc:
                 print("Error processing line {:d} of {}:\n".format(n+1, fullname),
                       file=sys.stderr)
                 import traceback
-                for record in traceback.format_exception(*sys.exc_info()):
+                for record in traceback.format_exception(exc):
                     for line in record.splitlines():
                         print('  '+line, file=sys.stderr)
                 print("\nRemainder of file ignored", file=sys.stderr)
diff --git a/Lib/test/inspect_fodder.py b/Lib/test/inspect_fodder.py
index e1287a315901..567dfbab8048 100644
--- a/Lib/test/inspect_fodder.py
+++ b/Lib/test/inspect_fodder.py
@@ -1,7 +1,7 @@
 # line 1
 'A module docstring.'
 
-import sys, inspect
+import inspect
 # line 5
 
 # line 7
@@ -41,8 +41,8 @@ def abuse(self, a, b, c):
     def argue(self, a, b, c):
         try:
             spam(a, b, c)
-        except:
-            self.ex = sys.exc_info()
+        except BaseException as e:
+            self.ex = e
             self.tr = inspect.trace()
 
     @property
@@ -78,8 +78,8 @@ async def lobbest(grenade):
 currentframe = inspect.currentframe()
 try:
     raise Exception()
-except:
-    tb = sys.exc_info()[2]
+except BaseException as e:
+    tb = e.__traceback__
 
 class Callable:
     def __call__(self, *args):
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 803b259d961f..3a3646f1861e 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -430,7 +430,7 @@ def __init__(self, *args, **kwargs):
         git.abuse(7, 8, 9)
 
     def test_abuse_done(self):
-        self.istest(inspect.istraceback, 'git.ex[2]')
+        self.istest(inspect.istraceback, 'git.ex.__traceback__')
         self.istest(inspect.isframe, 'mod.fr')
 
     def test_stack(self):
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py
index 07522bda6a55..d81902327a7e 100644
--- a/Lib/test/test_with.py
+++ b/Lib/test/test_with.py
@@ -79,11 +79,11 @@ def __exit__(self, *exc_info):
             try:
                 if mgr.__exit__(*ex):
                     ex = (None, None, None)
-            except:
-                ex = sys.exc_info()
+            except BaseException as e:
+                ex = (type(e), e, e.__traceback__)
         self.entered = None
         if ex is not exc_info:
-            raise ex[0](ex[1]).with_traceback(ex[2])
+            raise ex
 
 
 class MockNested(Nested):
diff --git a/Lib/tkinter/filedialog.py b/Lib/tkinter/filedialog.py
index 600d0bd49fe2..e2eff98e601c 100644
--- a/Lib/tkinter/filedialog.py
+++ b/Lib/tkinter/filedialog.py
@@ -461,7 +461,6 @@ def test():
 
     # Start off with UTF-8
     enc = "utf-8"
-    import sys
 
     # See whether CODESET is defined
     try:
@@ -477,9 +476,9 @@ def test():
     try:
         fp=open(openfilename,"r")
         fp.close()
-    except:
+    except BaseException as exc:
         print("Could not open File: ")
-        print(sys.exc_info()[1])
+        print(exc)
 
     print("open", openfilename.encode(enc))
 



More information about the Python-checkins mailing list