Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replaced line breaker with parenthesis, added test case
  • Loading branch information
gaogaotiantian committed Jul 22, 2023
commit 13f977515c9e2d8fd7bce21f8ea23d9ab886a708
4 changes: 2 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,8 @@ def get_lineno(self):

# First, let's see if there are any method definitions
for member in self.cls.__dict__.values():
if isinstance(member, types.FunctionType) and \
member.__module__ == self.cls.__module__:
if (isinstance(member, types.FunctionType) and
member.__module__ == self.cls.__module__):
for lineno, end_lineno in self.lineno_found:
if lineno <= member.__code__.co_firstlineno <= end_lineno:
return lineno
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import shutil
import sys
import types
import tempfile
import textwrap
import unicodedata
import unittest
Expand Down Expand Up @@ -963,6 +964,33 @@ def test_nested_class_definition_inside_function(self):
self.assertSourceEqual(mod2.cls213, 218, 222)
self.assertSourceEqual(mod2.cls213().func219(), 220, 221)

def test_class_with_method_from_other_module(self):
with tempfile.TemporaryDirectory() as tempdir:
with open(os.path.join(tempdir, 'inspect_actual%spy' % os.extsep),
'w', encoding='utf-8') as f:
f.write(textwrap.dedent("""\
Comment thread
gaogaotiantian marked this conversation as resolved.
Outdated
import inspect_other
class A:
def f(self):
pass
class A:
def f(self):
pass # correct one
A.f = inspect_other.A.f
"""))

with open(os.path.join(tempdir, 'inspect_other%spy' % os.extsep),
'w', encoding='utf-8') as f:
f.write(textwrap.dedent("""\
Comment thread
gaogaotiantian marked this conversation as resolved.
Outdated
class A:
def f(self):
pass
"""))

with DirsOnSysPath(tempdir):
import inspect_actual
self.assertIn("correct", inspect.getsource(inspect_actual.A))

@unittest.skipIf(
support.is_emscripten or support.is_wasi,
"socket.accept is broken"
Expand Down