Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.8] bpo-41058: Use source file encoding in pdb.find_function(). (GH-21010) #21026

Merged
merged 1 commit into from Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/pdb.py
Expand Up @@ -79,6 +79,7 @@
import pprint
import signal
import inspect
import tokenize
import traceback
import linecache

Expand All @@ -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
Expand Down
52 changes: 39 additions & 13 deletions Lib/test/test_pdb.py
Expand Up @@ -5,6 +5,7 @@
import pdb
import sys
import types
import codecs
import unittest
import subprocess
import textwrap
Expand Down Expand Up @@ -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 (
Expand All @@ -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):
Expand Down
@@ -0,0 +1 @@
:func:`pdb.find_function` now correctly determines the source file encoding.