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
4054b172
Commit
4054b172
authored
May 20, 2018
by
Aaron Hall, MBA
Committed by
Serhiy Storchaka
May 21, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-26103: Fix inspect.isdatadescriptor() and a data descriptor definition. (GH-1959)
Look for '__set__' or '__delete__'.
parent
aef639f6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
62 additions
and
4 deletions
+62
-4
Doc/howto/descriptor.rst
Doc/howto/descriptor.rst
+1
-1
Lib/inspect.py
Lib/inspect.py
+2
-2
Lib/test/test_inspect.py
Lib/test/test_inspect.py
+56
-1
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS.d/next/Library/2018-05-14-09-07-14.bpo-26103._zU8E2.rst
...S.d/next/Library/2018-05-14-09-07-14.bpo-26103._zU8E2.rst
+2
-0
No files found.
Doc/howto/descriptor.rst
View file @
4054b172
...
...
@@ -58,7 +58,7 @@ That is all there is to it. Define any of these methods and an object is
considered a descriptor and can override default behavior upon being looked up
as an attribute.
If an object defines
both :meth:`__get__` and :meth:`__set
__`, it is considered
If an object defines
:meth:`__set__` or :meth:`__delete
__`, it is considered
a data descriptor. Descriptors that only define :meth:`__get__` are called
non-data descriptors (they are typically used for methods but other uses are
possible).
...
...
Lib/inspect.py
View file @
4054b172
...
...
@@ -110,7 +110,7 @@ def ismethoddescriptor(object):
def
isdatadescriptor
(
object
):
"""Return true if the object is a data descriptor.
Data descriptors have
both a __get__ and a __set
__ attribute. Examples are
Data descriptors have
a __set__ or a __delete
__ attribute. Examples are
properties (defined in Python) and getsets and members (defined in C).
Typically, data descriptors will also have __name__ and __doc__ attributes
(properties, getsets, and members have both of these attributes), but this
...
...
@@ -119,7 +119,7 @@ def isdatadescriptor(object):
# mutual exclusion
return
False
tp
=
type
(
object
)
return
hasattr
(
tp
,
"__set__"
)
and
hasattr
(
tp
,
"__get
__"
)
return
hasattr
(
tp
,
"__set__"
)
or
hasattr
(
tp
,
"__delete
__"
)
if
hasattr
(
types
,
'MemberDescriptorType'
):
# CPython and equivalent
...
...
Lib/test/test_inspect.py
View file @
4054b172
...
...
@@ -1134,6 +1134,61 @@ class TestClassesAndFunctions(unittest.TestCase):
attrs
=
[
a
[
0
]
for
a
in
inspect
.
getmembers
(
C
)]
self
.
assertNotIn
(
'missing'
,
attrs
)
class
TestIsDataDescriptor
(
unittest
.
TestCase
):
def
test_custom_descriptors
(
self
):
class
NonDataDescriptor
:
def
__get__
(
self
,
value
,
type
=
None
):
pass
class
DataDescriptor0
:
def
__set__
(
self
,
name
,
value
):
pass
class
DataDescriptor1
:
def
__delete__
(
self
,
name
):
pass
class
DataDescriptor2
:
__set__
=
None
self
.
assertFalse
(
inspect
.
isdatadescriptor
(
NonDataDescriptor
()),
'class with only __get__ not a data descriptor'
)
self
.
assertTrue
(
inspect
.
isdatadescriptor
(
DataDescriptor0
()),
'class with __set__ is a data descriptor'
)
self
.
assertTrue
(
inspect
.
isdatadescriptor
(
DataDescriptor1
()),
'class with __delete__ is a data descriptor'
)
self
.
assertTrue
(
inspect
.
isdatadescriptor
(
DataDescriptor2
()),
'class with __set__ = None is a data descriptor'
)
def
test_slot
(
self
):
class
Slotted
:
__slots__
=
'foo'
,
self
.
assertTrue
(
inspect
.
isdatadescriptor
(
Slotted
.
foo
),
'a slot is a data descriptor'
)
def
test_property
(
self
):
class
Propertied
:
@
property
def
a_property
(
self
):
pass
self
.
assertTrue
(
inspect
.
isdatadescriptor
(
Propertied
.
a_property
),
'a property is a data descriptor'
)
def
test_functions
(
self
):
class
Test
(
object
):
def
instance_method
(
self
):
pass
@
classmethod
def
class_method
(
cls
):
pass
@
staticmethod
def
static_method
():
pass
def
function
():
pass
a_lambda
=
lambda
:
None
self
.
assertFalse
(
inspect
.
isdatadescriptor
(
Test
().
instance_method
),
'a instance method is not a data descriptor'
)
self
.
assertFalse
(
inspect
.
isdatadescriptor
(
Test
().
class_method
),
'a class method is not a data descriptor'
)
self
.
assertFalse
(
inspect
.
isdatadescriptor
(
Test
().
static_method
),
'a static method is not a data descriptor'
)
self
.
assertFalse
(
inspect
.
isdatadescriptor
(
function
),
'a function is not a data descriptor'
)
self
.
assertFalse
(
inspect
.
isdatadescriptor
(
a_lambda
),
'a lambda is not a data descriptor'
)
_global_ref
=
object
()
class
TestGetClosureVars
(
unittest
.
TestCase
):
...
...
@@ -3792,7 +3847,7 @@ def test_main():
TestGetcallargsUnboundMethods
,
TestGetattrStatic
,
TestGetGeneratorState
,
TestNoEOL
,
TestSignatureObject
,
TestSignatureBind
,
TestParameterObject
,
TestBoundArguments
,
TestSignaturePrivateHelpers
,
TestSignatureDefinitions
,
TestSignatureDefinitions
,
TestIsDataDescriptor
,
TestGetClosureVars
,
TestUnwrap
,
TestMain
,
TestReload
,
TestGetCoroutineState
)
...
...
Misc/ACKS
View file @
4054b172
...
...
@@ -603,6 +603,7 @@ Peter Haight
Václav Haisman
Zbigniew Halas
Walker Hale IV
Aaron Christopher Hall
Bob Halley
Jesse Hallio
Jun Hamano
...
...
Misc/NEWS.d/next/Library/2018-05-14-09-07-14.bpo-26103._zU8E2.rst
0 → 100644
View file @
4054b172
Correct ``inspect.isdatadescriptor`` to look for ``__set__`` or
``__delete__``. Patch by Aaron Hall.
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