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
805f8f9a
Commit
805f8f9a
authored
Aug 25, 2019
by
Berker Peksag
Committed by
Raymond Hettinger
Aug 24, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-19072: Make @classmethod support chained decorators (GH-8405)
parent
0dfc025c
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
71 additions
and
2 deletions
+71
-2
Doc/library/functions.rst
Doc/library/functions.rst
+4
-2
Lib/test/test_decorators.py
Lib/test/test_decorators.py
+39
-0
Lib/test/test_property.py
Lib/test/test_property.py
+21
-0
Misc/NEWS.d/next/Core and Builtins/2018-07-23-13-09-54.bpo-19072.Gc59GS.rst
...ore and Builtins/2018-07-23-13-09-54.bpo-19072.Gc59GS.rst
+3
-0
Objects/funcobject.c
Objects/funcobject.c
+4
-0
No files found.
Doc/library/functions.rst
View file @
805f8f9a
...
...
@@ -222,10 +222,12 @@ are always available. They are listed here in alphabetical order.
implied first argument.
Class methods are different than C++ or Java static methods. If you want those,
see :func:`staticmethod`.
see :func:`staticmethod` in this section.
For more information on class methods, see :ref:`types`.
.. versionchanged:: 3.9
Class methods can now wrap other :term:`descriptors <descriptor>` such as
:func:`property`.
.. function:: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
...
...
Lib/test/test_decorators.py
View file @
805f8f9a
...
...
@@ -265,6 +265,45 @@ class TestDecorators(unittest.TestCase):
self
.
assertEqual
(
bar
(),
42
)
self
.
assertEqual
(
actions
,
expected_actions
)
def
test_wrapped_descriptor_inside_classmethod
(
self
):
class
BoundWrapper
:
def
__init__
(
self
,
wrapped
):
self
.
__wrapped__
=
wrapped
def
__call__
(
self
,
*
args
,
**
kwargs
):
return
self
.
__wrapped__
(
*
args
,
**
kwargs
)
class
Wrapper
:
def
__init__
(
self
,
wrapped
):
self
.
__wrapped__
=
wrapped
def
__get__
(
self
,
instance
,
owner
):
bound_function
=
self
.
__wrapped__
.
__get__
(
instance
,
owner
)
return
BoundWrapper
(
bound_function
)
def
decorator
(
wrapped
):
return
Wrapper
(
wrapped
)
class
Class
:
@
decorator
@
classmethod
def
inner
(
cls
):
# This should already work.
return
'spam'
@
classmethod
@
decorator
def
outer
(
cls
):
# Raised TypeError with a message saying that the 'Wrapper'
# object is not callable.
return
'eggs'
self
.
assertEqual
(
Class
.
inner
(),
'spam'
)
self
.
assertEqual
(
Class
.
outer
(),
'eggs'
)
self
.
assertEqual
(
Class
().
inner
(),
'spam'
)
self
.
assertEqual
(
Class
().
outer
(),
'eggs'
)
class
TestClassDecorators
(
unittest
.
TestCase
):
def
test_simple
(
self
):
...
...
Lib/test/test_property.py
View file @
805f8f9a
...
...
@@ -183,6 +183,27 @@ class PropertyTests(unittest.TestCase):
fake_prop
.
__init__
(
'fget'
,
'fset'
,
'fdel'
,
'doc'
)
self
.
assertAlmostEqual
(
gettotalrefcount
()
-
refs_before
,
0
,
delta
=
10
)
@
unittest
.
skipIf
(
sys
.
flags
.
optimize
>=
2
,
"Docstrings are omitted with -O2 and above"
)
def
test_class_property
(
self
):
class
A
:
@
classmethod
@
property
def
__doc__
(
cls
):
return
'A doc for %r'
%
cls
.
__name__
self
.
assertEqual
(
A
.
__doc__
,
"A doc for 'A'"
)
@
unittest
.
skipIf
(
sys
.
flags
.
optimize
>=
2
,
"Docstrings are omitted with -O2 and above"
)
def
test_class_property_override
(
self
):
class
A
:
"""First"""
@
classmethod
@
property
def
__doc__
(
cls
):
return
'Second'
self
.
assertEqual
(
A
.
__doc__
,
'Second'
)
# Issue 5890: subclasses of property do not preserve method __doc__ strings
class
PropertySub
(
property
):
...
...
Misc/NEWS.d/next/Core and Builtins/2018-07-23-13-09-54.bpo-19072.Gc59GS.rst
0 → 100644
View file @
805f8f9a
The :class:`classmethod` decorator can now wrap other descriptors
such as property objects. Adapted from a patch written by Graham
Dumpleton.
Objects/funcobject.c
View file @
805f8f9a
...
...
@@ -741,6 +741,10 @@ cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
}
if
(
type
==
NULL
)
type
=
(
PyObject
*
)(
Py_TYPE
(
obj
));
if
(
Py_TYPE
(
cm
->
cm_callable
)
->
tp_descr_get
!=
NULL
)
{
return
Py_TYPE
(
cm
->
cm_callable
)
->
tp_descr_get
(
cm
->
cm_callable
,
type
,
NULL
);
}
return
PyMethod_New
(
cm
->
cm_callable
,
type
);
}
...
...
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