Skip to content

Commit

Permalink
Fix func inspection for PyPy builtins methods, i.e. str.split()
Browse files Browse the repository at this point in the history
  • Loading branch information
Suor committed Nov 25, 2023
1 parent c2aa0fd commit f9e3417
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion funcy/_inspect.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import absolute_import
from inspect import CO_VARARGS, CO_VARKEYWORDS, signature
from collections import namedtuple
import platform
import re

from .decorators import unwrap


IS_PYPY = platform.python_implementation() == "PyPy"

# This provides sufficient introspection for *curry() functions.
#
# We only really need a number of required positional arguments.
Expand Down Expand Up @@ -121,7 +124,8 @@ def get_spec(func, _cache={}):
self_set = {func.__init__.__code__.co_varnames[0]}
return spec._replace(max_n=spec.max_n - 1, names=spec.names - self_set,
req_n=spec.req_n - 1, req_names=spec.req_names - self_set)
elif hasattr(func, '__code__'):
# In PyPy some builtins might have __code__ but no __defaults__, so we fall back to signature
elif not IS_PYPY and hasattr(func, '__code__'):
return _code_to_spec(func)
else:
# We use signature last to be fully backwards compatible. Also it's slower
Expand Down
4 changes: 2 additions & 2 deletions tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def test_rpartial():

def test_curry():
assert curry(lambda: 42)() == 42
assert curry(_ * 2)(21) == 42
assert curry(_ * _)(6)(7) == 42
assert curry(lambda x: x * 2)(21) == 42
assert curry(lambda x, y: x * y)(6)(7) == 42
assert curry(__add__, 2)(10)(1) == 11
assert curry(__add__)(10)(1) == 11 # Introspect builtin
assert curry(lambda x,y,z: x+y+z)('a')('b')('c') == 'abc'
Expand Down

0 comments on commit f9e3417

Please sign in to comment.