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
f203f2d5
Kaydet (Commit)
f203f2d5
authored
Eyl 06, 2013
tarafından
Ethan Furman
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Close #18924: Block naive attempts to change an Enum member.
üst
96d848ac
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
0 deletions
+18
-0
enum.py
Lib/enum.py
+13
-0
test_enum.py
Lib/test/test_enum.py
+5
-0
No files found.
Lib/enum.py
Dosyayı görüntüle @
f203f2d5
...
...
@@ -263,6 +263,19 @@ class EnumMeta(type):
def
__repr__
(
cls
):
return
"<enum
%
r>"
%
cls
.
__name__
def
__setattr__
(
cls
,
name
,
value
):
"""Block attempts to reassign Enum members.
A simple assignment to the class namespace only changes one of the
several possible ways to get an Enum member from the Enum class,
resulting in an inconsistent Enumeration.
"""
member_map
=
cls
.
__dict__
.
get
(
'_member_map_'
,
{})
if
name
in
member_map
:
raise
AttributeError
(
'Cannot reassign members.'
)
super
()
.
__setattr__
(
name
,
value
)
def
_create_
(
cls
,
class_name
,
names
=
None
,
*
,
module
=
None
,
type
=
None
):
"""Convenience method to create a new Enum class.
...
...
Lib/test/test_enum.py
Dosyayı görüntüle @
f203f2d5
...
...
@@ -152,6 +152,11 @@ class TestEnum(unittest.TestCase):
with
self
.
assertRaises
(
AttributeError
):
Season
.
SPRING
.
value
=
2
def
test_changing_member
(
self
):
Season
=
self
.
Season
with
self
.
assertRaises
(
AttributeError
):
Season
.
WINTER
=
'really cold'
def
test_invalid_names
(
self
):
with
self
.
assertRaises
(
ValueError
):
class
Wrong
(
Enum
):
...
...
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