Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1864754c
Commit
1864754c
authored
Jan 29, 2014
by
Yury Selivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
inspect.Signature: Make from_builtin to raise an exception if no signature can
be provided #20422
parent
8843d56e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
10 deletions
+24
-10
Lib/inspect.py
Lib/inspect.py
+20
-10
Lib/test/test_inspect.py
Lib/test/test_inspect.py
+4
-0
No files found.
Lib/inspect.py
View file @
1864754c
...
...
@@ -1514,18 +1514,24 @@ def _signature_bound_method(sig):
return
sig
.
replace
(
parameters
=
params
)
def
_signature_is_builtin
(
obj
):
# Internal helper to test if `obj` is a callable that might
# support Argument Clinic's __text_signature__ protocol.
return
(
isinstance
(
obj
,
_NonUserDefinedCallables
)
or
ismethoddescriptor
(
obj
)
or
# Can't test 'isinstance(type)' here, as it would
# also be True for regular python classes
obj
in
(
type
,
object
))
def
signature
(
obj
):
'''Get a signature object for the passed callable.'''
if
not
callable
(
obj
):
raise
TypeError
(
'{!r} is not a callable object'
.
format
(
obj
))
if
(
isinstance
(
obj
,
_NonUserDefinedCallables
)
or
ismethoddescriptor
(
obj
)
or
isinstance
(
obj
,
type
)):
sig
=
Signature
.
from_builtin
(
obj
)
if
sig
:
return
sig
if
_signature_is_builtin
(
obj
):
return
Signature
.
from_builtin
(
obj
)
if
isinstance
(
obj
,
types
.
MethodType
):
# In this case we skip the first parameter of the underlying
...
...
@@ -2017,9 +2023,13 @@ class Signature:
@
classmethod
def
from_builtin
(
cls
,
func
):
if
not
_signature_is_builtin
(
func
):
raise
TypeError
(
"{!r} is not a Python builtin "
"function"
.
format
(
func
))
s
=
getattr
(
func
,
"__text_signature__"
,
None
)
if
not
s
:
r
eturn
None
r
aise
ValueError
(
"no signature found for builtin {!r}"
.
format
(
func
))
Parameter
=
cls
.
_parameter_cls
...
...
@@ -2038,9 +2048,10 @@ class Signature:
try
:
module
=
ast
.
parse
(
s
)
except
SyntaxError
:
return
None
module
=
None
if
not
isinstance
(
module
,
ast
.
Module
):
r
eturn
None
r
aise
ValueError
(
"{!r} builtin has invalid signature"
.
format
(
func
))
f
=
module
.
body
[
0
]
...
...
@@ -2149,7 +2160,6 @@ class Signature:
return
cls
(
parameters
,
return_annotation
=
cls
.
empty
)
@
property
def
parameters
(
self
):
return
self
.
_parameters
...
...
Lib/test/test_inspect.py
View file @
1864754c
...
...
@@ -1667,6 +1667,10 @@ class TestSignatureObject(unittest.TestCase):
with
self
.
assertRaisesRegex
(
TypeError
,
'is not a Python function'
):
inspect
.
Signature
.
from_function
(
42
)
def
test_signature_from_builtin_errors
(
self
):
with
self
.
assertRaisesRegex
(
TypeError
,
'is not a Python builtin'
):
inspect
.
Signature
.
from_builtin
(
42
)
def
test_signature_on_method
(
self
):
class
Test
:
def
__init__
(
*
args
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment