Skip to content

Commit 19fcffa

Browse files
bpo-41058: Use source file encoding in pdb.find_function(). (GH-21010)
1 parent 6c4e0bd commit 19fcffa

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

Lib/pdb.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import pprint
8080
import signal
8181
import inspect
82+
import tokenize
8283
import traceback
8384
import linecache
8485

@@ -93,7 +94,7 @@ class Restart(Exception):
9394
def find_function(funcname, filename):
9495
cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname))
9596
try:
96-
fp = open(filename)
97+
fp = tokenize.open(filename)
9798
except OSError:
9899
return None
99100
# consumer of this info expects the first line to be 1

Lib/test/test_pdb.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pdb
66
import sys
77
import types
8+
import codecs
89
import unittest
910
import subprocess
1011
import textwrap
@@ -1226,9 +1227,7 @@ def run_pdb_module(self, script, commands):
12261227
return self._run_pdb(['-m', self.module_name], commands)
12271228

12281229
def _assert_find_function(self, file_content, func_name, expected):
1229-
file_content = textwrap.dedent(file_content)
1230-
1231-
with open(support.TESTFN, 'w') as f:
1230+
with open(support.TESTFN, 'wb') as f:
12321231
f.write(file_content)
12331232

12341233
expected = None if not expected else (
@@ -1237,22 +1236,49 @@ def _assert_find_function(self, file_content, func_name, expected):
12371236
expected, pdb.find_function(func_name, support.TESTFN))
12381237

12391238
def test_find_function_empty_file(self):
1240-
self._assert_find_function('', 'foo', None)
1239+
self._assert_find_function(b'', 'foo', None)
12411240

12421241
def test_find_function_found(self):
12431242
self._assert_find_function(
12441243
"""\
1245-
def foo():
1246-
pass
1244+
def foo():
1245+
pass
12471246
1248-
def bar():
1249-
pass
1247+
def bœr():
1248+
pass
12501249
1251-
def quux():
1252-
pass
1253-
""",
1254-
'bar',
1255-
('bar', 4),
1250+
def quux():
1251+
pass
1252+
""".encode(),
1253+
'bœr',
1254+
('bœr', 4),
1255+
)
1256+
1257+
def test_find_function_found_with_encoding_cookie(self):
1258+
self._assert_find_function(
1259+
"""\
1260+
# coding: iso-8859-15
1261+
def foo():
1262+
pass
1263+
1264+
def bœr():
1265+
pass
1266+
1267+
def quux():
1268+
pass
1269+
""".encode('iso-8859-15'),
1270+
'bœr',
1271+
('bœr', 5),
1272+
)
1273+
1274+
def test_find_function_found_with_bom(self):
1275+
self._assert_find_function(
1276+
codecs.BOM_UTF8 + """\
1277+
def bœr():
1278+
pass
1279+
""".encode(),
1280+
'bœr',
1281+
('bœr', 1),
12561282
)
12571283

12581284
def test_issue7964(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`pdb.find_function` now correctly determines the source file encoding.

0 commit comments

Comments
 (0)