[Python-checkins] bpo-41058: Use source file encoding in pdb.find_function(). (GH-21010)

Miss Islington (bot) webhook-mailer at python.org
Sun Jun 21 15:36:32 EDT 2020


https://github.com/python/cpython/commit/14195597b3a877209c20d00e0ec844234e624d13
commit: 14195597b3a877209c20d00e0ec844234e624d13
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-06-21T12:36:23-07:00
summary:

bpo-41058: Use source file encoding in pdb.find_function(). (GH-21010)

(cherry picked from commit 19fcffa92773e008e4f5efb80047420a0cfafeec)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
A Misc/NEWS.d/next/Library/2020-06-20-21-03-55.bpo-41058.gztdZy.rst
M Lib/pdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/pdb.py b/Lib/pdb.py
index bf503f1e73ee1..931a039446187 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -79,6 +79,7 @@
 import pprint
 import signal
 import inspect
+import tokenize
 import traceback
 import linecache
 
@@ -93,7 +94,7 @@ class Restart(Exception):
 def find_function(funcname, filename):
     cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname))
     try:
-        fp = open(filename)
+        fp = tokenize.open(filename)
     except OSError:
         return None
     # consumer of this info expects the first line to be 1
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 4c38e919a83b7..0e7ae1d86ed80 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -5,6 +5,7 @@
 import pdb
 import sys
 import types
+import codecs
 import unittest
 import subprocess
 import textwrap
@@ -1226,9 +1227,7 @@ def run_pdb_module(self, script, commands):
         return self._run_pdb(['-m', self.module_name], commands)
 
     def _assert_find_function(self, file_content, func_name, expected):
-        file_content = textwrap.dedent(file_content)
-
-        with open(support.TESTFN, 'w') as f:
+        with open(support.TESTFN, 'wb') as f:
             f.write(file_content)
 
         expected = None if not expected else (
@@ -1237,22 +1236,49 @@ def _assert_find_function(self, file_content, func_name, expected):
             expected, pdb.find_function(func_name, support.TESTFN))
 
     def test_find_function_empty_file(self):
-        self._assert_find_function('', 'foo', None)
+        self._assert_find_function(b'', 'foo', None)
 
     def test_find_function_found(self):
         self._assert_find_function(
             """\
-            def foo():
-                pass
+def foo():
+    pass
 
-            def bar():
-                pass
+def bœr():
+    pass
 
-            def quux():
-                pass
-            """,
-            'bar',
-            ('bar', 4),
+def quux():
+    pass
+""".encode(),
+            'bœr',
+            ('bœr', 4),
+        )
+
+    def test_find_function_found_with_encoding_cookie(self):
+        self._assert_find_function(
+            """\
+# coding: iso-8859-15
+def foo():
+    pass
+
+def bœr():
+    pass
+
+def quux():
+    pass
+""".encode('iso-8859-15'),
+            'bœr',
+            ('bœr', 5),
+        )
+
+    def test_find_function_found_with_bom(self):
+        self._assert_find_function(
+            codecs.BOM_UTF8 + """\
+def bœr():
+    pass
+""".encode(),
+            'bœr',
+            ('bœr', 1),
         )
 
     def test_issue7964(self):
diff --git a/Misc/NEWS.d/next/Library/2020-06-20-21-03-55.bpo-41058.gztdZy.rst b/Misc/NEWS.d/next/Library/2020-06-20-21-03-55.bpo-41058.gztdZy.rst
new file mode 100644
index 0000000000000..6ac90098aa52b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-06-20-21-03-55.bpo-41058.gztdZy.rst
@@ -0,0 +1 @@
+:func:`pdb.find_function` now correctly determines the source file encoding.



More information about the Python-checkins mailing list