Skip to content

Commit d58e392

Browse files
gpotter2sebastinas
authored andcommitted
Fix __signature__ support if object has a __file__
1 parent db6a559 commit d58e392

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

bpython/inspection.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,13 @@ def getfuncprops(func: str, f: Callable) -> Optional[FuncProps]:
289289
return None
290290
try:
291291
argspec = _get_argspec_from_signature(f)
292-
fprops = FuncProps(
293-
func, _fix_default_values(f, argspec), is_bound_method
294-
)
292+
try:
293+
argspec = _fix_default_values(f, argspec)
294+
except KeyError as ex:
295+
# Parsing of the source failed. If f has a __signature__, we trust it.
296+
if not hasattr(f, "__signature__"):
297+
raise ex
298+
fprops = FuncProps(func, argspec, is_bound_method)
295299
except (TypeError, KeyError, ValueError):
296300
argspec_pydoc = _getpydocspec(f)
297301
if argspec_pydoc is None:

bpython/test/test_repl.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections
22
import inspect
3+
import os
34
import socket
45
import sys
56
import tempfile
@@ -523,13 +524,19 @@ def __init__(self, *args, **kwargs):
523524
inspect.Parameter("pinetree", inspect.Parameter.KEYWORD_ONLY),
524525
])
525526
"""
526-
for line in code.split("\n"):
527-
print(line[8:])
528-
self.repl.push(line[8:])
527+
code = [x[8:] for x in code.split("\n")]
528+
for line in code:
529+
self.repl.push(line)
529530

530-
self.assertTrue(self.repl.complete())
531-
self.assertTrue(hasattr(self.repl.matches_iter, "matches"))
532-
self.assertEqual(self.repl.matches_iter.matches, ["apple2=", "apple="])
531+
with mock.patch(
532+
"bpython.inspection.inspect.getsourcelines",
533+
return_value=(code, None),
534+
):
535+
self.assertTrue(self.repl.complete())
536+
self.assertTrue(hasattr(self.repl.matches_iter, "matches"))
537+
self.assertEqual(
538+
self.repl.matches_iter.matches, ["apple2=", "apple="]
539+
)
533540

534541

535542
if __name__ == "__main__":

0 commit comments

Comments
 (0)