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
e690883c
Kaydet (Commit)
e690883c
authored
Şub 19, 2003
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
üst
a43fd0c8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
33 deletions
+66
-33
copy.py
Lib/copy.py
+34
-22
copy_reg.py
Lib/copy_reg.py
+8
-3
test_copy.py
Lib/test/test_copy.py
+24
-8
No files found.
Lib/copy.py
Dosyayı görüntüle @
e690883c
...
...
@@ -79,14 +79,20 @@ def copy(x):
return
copier
(
x
)
reductor
=
dispatch_table
.
get
(
cls
)
if
not
reductor
:
reductor
=
getattr
(
cls
,
"__reduce__"
,
None
)
if
reductor
==
object
.
__reduce__
:
reductor
=
_better_reduce
elif
not
reductor
:
raise
Error
(
"un(shallow)copyable object of type
%
s"
%
cls
)
return
_reconstruct
(
x
,
reductor
(
x
),
0
)
if
reductor
:
rv
=
reductor
(
x
)
else
:
reductor
=
getattr
(
x
,
"__reduce_ex__"
,
None
)
if
reductor
:
rv
=
reductor
(
2
)
else
:
reductor
=
getattr
(
x
,
"__reduce__"
,
None
)
if
reductor
:
rv
=
reductor
()
else
:
raise
Error
(
"un(shallow)copyable object of type
%
s"
%
cls
)
return
_reconstruct
(
x
,
rv
,
0
)
_copy_dispatch
=
d
=
{}
...
...
@@ -176,21 +182,27 @@ def deepcopy(x, memo=None, _nil=[]):
except
TypeError
:
# cls is not a class (old Boost; see SF #502085)
issc
=
0
if
issc
:
copier
=
_deepcopy_atomic
y
=
_deepcopy_atomic
(
x
,
memo
)
else
:
copier
=
getattr
(
cls
,
"__deepcopy__"
,
None
)
if
copier
:
y
=
copier
(
x
,
memo
)
else
:
reductor
=
dispatch_table
.
get
(
cls
)
if
not
reductor
:
reductor
=
getattr
(
cls
,
"__reduce__"
,
None
)
if
reductor
==
object
.
__reduce__
:
reductor
=
_better_reduce
elif
not
reductor
:
raise
Error
(
"un(deep)copyable object of type
%
s"
%
cls
)
y
=
_reconstruct
(
x
,
reductor
(
x
),
1
,
memo
)
copier
=
getattr
(
x
,
"__deepcopy__"
,
None
)
if
copier
:
y
=
copier
(
memo
)
else
:
reductor
=
dispatch_table
.
get
(
cls
)
if
reductor
:
rv
=
reductor
(
x
)
else
:
reductor
=
getattr
(
x
,
"__reduce_ex__"
,
None
)
if
reductor
:
rv
=
reductor
(
2
)
else
:
reductor
=
getattr
(
x
,
"__reduce__"
,
None
)
if
reductor
:
rv
=
reductor
()
else
:
raise
Error
(
"un(deep)copyable object of type
%
s"
%
cls
)
y
=
_reconstruct
(
x
,
rv
,
1
,
memo
)
memo
[
d
]
=
y
_keep_alive
(
x
,
memo
)
# Make sure x lives at least as long as d
...
...
Lib/copy_reg.py
Dosyayı görüntüle @
e690883c
...
...
@@ -113,9 +113,14 @@ def _better_reduce(obj):
def
_reduce_ex
(
obj
,
proto
=
0
):
obj_reduce
=
getattr
(
obj
,
"__reduce__"
,
None
)
if
obj_reduce
and
obj
.
__class__
.
__reduce__
is
not
object
.
__reduce__
:
return
obj_reduce
()
elif
proto
<
2
:
# XXX This fails in test_copy.py line 61
if
obj_reduce
:
try
:
if
obj
.
__class__
.
__reduce__
is
not
object
.
__reduce__
:
return
obj_reduce
()
except
AttributeError
:
pass
if
proto
<
2
:
return
_reduce
(
obj
)
else
:
return
_better_reduce
(
obj
)
...
...
Lib/test/test_copy.py
Dosyayı görüntüle @
e690883c
...
...
@@ -46,6 +46,16 @@ class TestCopy(unittest.TestCase):
copy_reg
.
pickle
(
C
,
pickle_C
,
C
)
y
=
copy
.
copy
(
x
)
def
test_copy_reduce_ex
(
self
):
class
C
(
object
):
def
__reduce_ex__
(
self
,
proto
):
return
""
def
__reduce__
(
self
):
raise
test_support
.
TestFailed
,
"shouldn't call this"
x
=
C
()
y
=
copy
.
copy
(
x
)
self
.
assert_
(
y
is
x
)
def
test_copy_reduce
(
self
):
class
C
(
object
):
def
__reduce__
(
self
):
...
...
@@ -55,13 +65,11 @@ class TestCopy(unittest.TestCase):
self
.
assert_
(
y
is
x
)
def
test_copy_cant
(
self
):
class
Meta
(
type
):
class
C
(
object
):
def
__getattribute__
(
self
,
name
):
if
name
==
"__reduce__"
:
if
name
.
startswith
(
"__reduce"
)
:
raise
AttributeError
,
name
return
object
.
__getattribute__
(
self
,
name
)
class
C
:
__metaclass__
=
Meta
x
=
C
()
self
.
assertRaises
(
copy
.
Error
,
copy
.
copy
,
x
)
...
...
@@ -209,6 +217,16 @@ class TestCopy(unittest.TestCase):
copy_reg
.
pickle
(
C
,
pickle_C
,
C
)
y
=
copy
.
deepcopy
(
x
)
def
test_deepcopy_reduce_ex
(
self
):
class
C
(
object
):
def
__reduce_ex__
(
self
,
proto
):
return
""
def
__reduce__
(
self
):
raise
test_support
.
TestFailed
,
"shouldn't call this"
x
=
C
()
y
=
copy
.
deepcopy
(
x
)
self
.
assert_
(
y
is
x
)
def
test_deepcopy_reduce
(
self
):
class
C
(
object
):
def
__reduce__
(
self
):
...
...
@@ -218,13 +236,11 @@ class TestCopy(unittest.TestCase):
self
.
assert_
(
y
is
x
)
def
test_deepcopy_cant
(
self
):
class
Meta
(
type
):
class
C
(
object
):
def
__getattribute__
(
self
,
name
):
if
name
==
"__reduce__"
:
if
name
.
startswith
(
"__reduce"
)
:
raise
AttributeError
,
name
return
object
.
__getattribute__
(
self
,
name
)
class
C
:
__metaclass__
=
Meta
x
=
C
()
self
.
assertRaises
(
copy
.
Error
,
copy
.
deepcopy
,
x
)
...
...
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