From ca3f435fe6b5de970848eb3a5d8f8e6cd5d2f73c Mon Sep 17 00:00:00 2001
From: Terry Jan Reedy <tjreedy@udel.edu>
Date: Sat, 5 Sep 2015 19:13:26 -0400
Subject: [PATCH] Issue #16180: Exit pdb if file has syntax error, instead of
 trapping user in an infinite loop.  Patch by Xavier de Gaye.

---
 Lib/pdb.py           |  3 +++
 Lib/test/test_pdb.py | 12 ++++++++++++
 Misc/NEWS            |  3 +++
 3 files changed, 18 insertions(+)

diff --git a/Lib/pdb.py b/Lib/pdb.py
index e28564bdfa..7d58c2ab43 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -1669,6 +1669,9 @@ def main():
             # In most cases SystemExit does not warrant a post-mortem session.
             print("The program exited via sys.exit(). Exit status:", end=' ')
             print(sys.exc_info()[1])
+        except SyntaxError:
+            traceback.print_exc()
+            sys.exit(1)
         except:
             traceback.print_exc()
             print("Uncaught exception. Entering post mortem debugging")
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index ec8346c5ee..35044ad2a1 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -1043,6 +1043,18 @@ class PdbTestCase(unittest.TestCase):
         self.assertNotIn('Error', stdout.decode(),
                          "Got an error running test script under PDB")
 
+    def test_issue16180(self):
+        # A syntax error in the debuggee.
+        script = "def f: pass\n"
+        commands = ''
+        expected = "SyntaxError:"
+        stdout, stderr = self.run_pdb(script, commands)
+        self.assertIn(expected, stdout,
+            '\n\nExpected:\n{}\nGot:\n{}\n'
+            'Fail to handle a syntax error in the debuggee.'
+            .format(expected, stdout))
+
+
     def tearDown(self):
         support.unlink(support.TESTFN)
 
diff --git a/Misc/NEWS b/Misc/NEWS
index 66780edd27..dd175fead4 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #16180: Exit pdb if file has syntax error, instead of trapping user
+  in an infinite loop.  Patch by Xavier de Gaye.
+
 - Issue #21112: Fix regression in unittest.expectedFailure on subclasses.
   Patch from Berker Peksag.
 
-- 
2.50.1