Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
48ad7c0b
Kaydet (Commit)
48ad7c0b
authored
Agu 20, 2014
tarafından
Benjamin Peterson
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
use __qualname__ to compute bound method repr (closes #21389)
Patch from Steven Barker.
üst
2b7ccbda
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
32 deletions
+74
-32
test_defaultdict.py
Lib/test/test_defaultdict.py
+3
-2
test_descr.py
Lib/test/test_descr.py
+55
-0
NEWS
Misc/NEWS
+3
-0
classobject.c
Objects/classobject.c
+13
-30
No files found.
Lib/test/test_defaultdict.py
Dosyayı görüntüle @
48ad7c0b
...
...
@@ -157,8 +157,9 @@ class TestDefaultDict(unittest.TestCase):
def
_factory
(
self
):
return
[]
d
=
sub
()
self
.
assertTrue
(
repr
(
d
)
.
startswith
(
"defaultdict(<bound method sub._factory of defaultdict(..."
))
self
.
assertRegex
(
repr
(
d
),
r"defaultdict\(<bound method .*sub\._factory "
r"of defaultdict\(\.\.\., \{\}\)>, \{\}\)"
)
# NOTE: printing a subclass of a builtin type does not call its
# tp_print slot. So this part is essentially the same test as above.
...
...
Lib/test/test_descr.py
Dosyayı görüntüle @
48ad7c0b
...
...
@@ -4423,6 +4423,61 @@ order (MRO) for bases """
self
.
assertIn
(
"__dict__"
,
Base
.
__dict__
)
self
.
assertNotIn
(
"__dict__"
,
Sub
.
__dict__
)
def
test_bound_method_repr
(
self
):
class
Foo
:
def
method
(
self
):
pass
self
.
assertRegex
(
repr
(
Foo
()
.
method
),
r"<bound method .*Foo\.method of <.*Foo object at .*>>"
)
class
Base
:
def
method
(
self
):
pass
class
Derived1
(
Base
):
pass
class
Derived2
(
Base
):
def
method
(
self
):
pass
base
=
Base
()
derived1
=
Derived1
()
derived2
=
Derived2
()
super_d2
=
super
(
Derived2
,
derived2
)
self
.
assertRegex
(
repr
(
base
.
method
),
r"<bound method .*Base\.method of <.*Base object at .*>>"
)
self
.
assertRegex
(
repr
(
derived1
.
method
),
r"<bound method .*Base\.method of <.*Derived1 object at .*>>"
)
self
.
assertRegex
(
repr
(
derived2
.
method
),
r"<bound method .*Derived2\.method of <.*Derived2 object at .*>>"
)
self
.
assertRegex
(
repr
(
super_d2
.
method
),
r"<bound method .*Base\.method of <.*Derived2 object at .*>>"
)
class
Foo
:
@classmethod
def
method
(
cls
):
pass
foo
=
Foo
()
self
.
assertRegex
(
repr
(
foo
.
method
),
# access via instance
r"<bound method .*Foo\.method of <class '.*Foo'>>"
)
self
.
assertRegex
(
repr
(
Foo
.
method
),
# access via the class
r"<bound method .*Foo\.method of <class '.*Foo'>>"
)
class
MyCallable
:
def
__call__
(
self
,
arg
):
pass
func
=
MyCallable
()
# func has no __name__ or __qualname__ attributes
instance
=
object
()
method
=
types
.
MethodType
(
func
,
instance
)
self
.
assertRegex
(
repr
(
method
),
r"<bound method \? of <object object at .*>>"
)
func
.
__name__
=
"name"
self
.
assertRegex
(
repr
(
method
),
r"<bound method name of <object object at .*>>"
)
func
.
__qualname__
=
"qualname"
self
.
assertRegex
(
repr
(
method
),
r"<bound method qualname of <object object at .*>>"
)
class
DictProxyTests
(
unittest
.
TestCase
):
def
setUp
(
self
):
...
...
Misc/NEWS
Dosyayı görüntüle @
48ad7c0b
...
...
@@ -10,6 +10,9 @@ Release date: TBA
Core and Builtins
-----------------
- Issue #21389: Displaying the __qualname__ of the underlying function in the
repr of a bound method.
- Issue #22206: Using pthread, PyThread_create_key() now sets errno to ENOMEM
and returns -1 (error) on integer overflow.
...
...
Objects/classobject.c
Dosyayı görüntüle @
48ad7c0b
...
...
@@ -15,6 +15,7 @@ static int numfree = 0;
#endif
_Py_IDENTIFIER
(
__name__
);
_Py_IDENTIFIER
(
__qualname__
);
PyObject
*
PyMethod_Function
(
PyObject
*
im
)
...
...
@@ -243,51 +244,33 @@ method_repr(PyMethodObject *a)
{
PyObject
*
self
=
a
->
im_self
;
PyObject
*
func
=
a
->
im_func
;
PyObject
*
klass
;
PyObject
*
funcname
=
NULL
,
*
klassname
=
NULL
,
*
result
=
NULL
;
char
*
defname
=
"?"
;
PyObject
*
funcname
=
NULL
,
*
result
=
NULL
;
const
char
*
defname
=
"?"
;
if
(
self
==
NULL
)
{
PyErr_BadInternalCall
();
return
NULL
;
}
klass
=
(
PyObject
*
)
Py_TYPE
(
self
);
funcname
=
_PyObject_GetAttrId
(
func
,
&
PyId___name__
);
funcname
=
_PyObject_GetAttrId
(
func
,
&
PyId___qualname__
);
if
(
funcname
==
NULL
)
{
if
(
!
PyErr_ExceptionMatches
(
PyExc_AttributeError
))
return
NULL
;
PyErr_Clear
();
}
else
if
(
!
PyUnicode_Check
(
funcname
))
{
Py_DECREF
(
funcname
);
funcname
=
NULL
;
}
if
(
klass
==
NULL
)
klassname
=
NULL
;
else
{
klassname
=
_PyObject_GetAttrId
(
klass
,
&
PyId___name__
);
if
(
klassname
==
NULL
)
{
if
(
!
PyErr_ExceptionMatches
(
PyExc_AttributeError
))
{
Py_XDECREF
(
funcname
);
funcname
=
_PyObject_GetAttrId
(
func
,
&
PyId___name__
);
if
(
funcname
==
NULL
)
{
if
(
!
PyErr_ExceptionMatches
(
PyExc_AttributeError
))
return
NULL
;
}
PyErr_Clear
();
}
else
if
(
!
PyUnicode_Check
(
klassname
))
{
Py_DECREF
(
klassname
);
klassname
=
NULL
;
}
}
if
(
funcname
!=
NULL
&&
!
PyUnicode_Check
(
funcname
))
{
Py_DECREF
(
funcname
);
funcname
=
NULL
;
}
/* XXX Shouldn't use repr()/%R here! */
result
=
PyUnicode_FromFormat
(
"<bound method %V.%V of %R>"
,
klassname
,
defname
,
result
=
PyUnicode_FromFormat
(
"<bound method %V of %R>"
,
funcname
,
defname
,
self
);
Py_XDECREF
(
funcname
);
Py_XDECREF
(
klassname
);
return
result
;
}
...
...
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