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
bfd334a4
Commit
bfd334a4
authored
Nov 22, 2003
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extend temporary hashability to remove() and discard().
Brings the functionality back in line with sets.py.
parent
5a5b2430
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
11 deletions
+62
-11
Lib/test/test_set.py
Lib/test/test_set.py
+10
-0
Objects/setobject.c
Objects/setobject.c
+52
-11
No files found.
Lib/test/test_set.py
View file @
bfd334a4
...
...
@@ -182,12 +182,22 @@ class TestSet(TestJointOps):
self
.
assert_
(
'a'
not
in
self
.
s
)
self
.
assertRaises
(
KeyError
,
self
.
s
.
remove
,
'Q'
)
self
.
assertRaises
(
TypeError
,
self
.
s
.
remove
,
[])
s
=
self
.
thetype
([
frozenset
(
self
.
word
)])
self
.
assert_
(
self
.
thetype
(
self
.
word
)
in
s
)
s
.
remove
(
self
.
thetype
(
self
.
word
))
self
.
assert_
(
self
.
thetype
(
self
.
word
)
not
in
s
)
self
.
assertRaises
(
KeyError
,
self
.
s
.
remove
,
self
.
thetype
(
self
.
word
))
def
test_discard
(
self
):
self
.
s
.
discard
(
'a'
)
self
.
assert_
(
'a'
not
in
self
.
s
)
self
.
s
.
discard
(
'Q'
)
self
.
assertRaises
(
TypeError
,
self
.
s
.
discard
,
[])
s
=
self
.
thetype
([
frozenset
(
self
.
word
)])
self
.
assert_
(
self
.
thetype
(
self
.
word
)
in
s
)
s
.
discard
(
self
.
thetype
(
self
.
word
))
self
.
assert_
(
self
.
thetype
(
self
.
word
)
not
in
s
)
s
.
discard
(
self
.
thetype
(
self
.
word
))
def
test_pop
(
self
):
for
i
in
xrange
(
len
(
self
.
s
)):
...
...
Objects/setobject.c
View file @
bfd334a4
...
...
@@ -73,6 +73,21 @@ set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return
make_new_set
(
type
,
NULL
);
}
static
PyObject
*
frozenset_dict_wrapper
(
PyObject
*
d
)
{
PySetObject
*
w
;
assert
(
PyDict_Check
(
d
));
w
=
(
PySetObject
*
)
make_new_set
(
&
PyFrozenSet_Type
,
NULL
);
if
(
w
==
NULL
)
return
NULL
;
Py_DECREF
(
w
->
data
);
Py_INCREF
(
d
);
w
->
data
=
d
;
return
(
PyObject
*
)
w
;
}
static
void
set_dealloc
(
PySetObject
*
so
)
{
...
...
@@ -104,20 +119,16 @@ set_len(PySetObject *so)
static
int
set_contains
(
PySetObject
*
so
,
PyObject
*
key
)
{
PyObject
*
olddict
;
PySetObject
*
tmp
;
PyObject
*
tmp
;
int
result
;
result
=
PySequence_Contains
(
so
->
data
,
key
);
if
(
result
==
-
1
&&
PyType_IsSubtype
(
key
->
ob_type
,
&
PySet_Type
))
{
PyErr_Clear
();
tmp
=
(
PySetObject
*
)
make_new_set
(
&
PyFrozenSet_Type
,
NULL
);
tmp
=
frozenset_dict_wrapper
(((
PySetObject
*
)(
key
))
->
data
);
if
(
tmp
==
NULL
)
return
-
1
;
olddict
=
tmp
->
data
;
tmp
->
data
=
((
PySetObject
*
)(
key
))
->
data
;
result
=
PySequence_Contains
(
so
->
data
,
(
PyObject
*
)
tmp
);
tmp
->
data
=
olddict
;
result
=
PySequence_Contains
(
so
->
data
,
tmp
);
Py_DECREF
(
tmp
);
}
return
result
;
...
...
@@ -820,8 +831,21 @@ This has no effect if the element is already present.");
static
PyObject
*
set_remove
(
PySetObject
*
so
,
PyObject
*
item
)
{
if
(
PyDict_DelItem
(
so
->
data
,
item
)
==
-
1
)
return
NULL
;
PyObject
*
tmp
;
if
(
PyDict_DelItem
(
so
->
data
,
item
)
==
-
1
)
{
if
(
!
PyType_IsSubtype
(
item
->
ob_type
,
&
PySet_Type
))
return
NULL
;
PyErr_Clear
();
tmp
=
frozenset_dict_wrapper
(((
PySetObject
*
)(
item
))
->
data
);
if
(
tmp
==
NULL
)
return
NULL
;
if
(
PyDict_DelItem
(
so
->
data
,
tmp
)
==
-
1
)
{
Py_DECREF
(
tmp
);
return
NULL
;
}
Py_DECREF
(
tmp
);
}
Py_INCREF
(
Py_None
);
return
Py_None
;
}
...
...
@@ -834,11 +858,28 @@ If the element is not a member, raise a KeyError.");
static
PyObject
*
set_discard
(
PySetObject
*
so
,
PyObject
*
item
)
{
PyObject
*
tmp
;
if
(
PyDict_DelItem
(
so
->
data
,
item
)
==
-
1
)
{
if
(
PyErr_ExceptionMatches
(
PyExc_KeyError
))
PyErr_Clear
();
else
return
NULL
;
else
{
if
(
!
PyType_IsSubtype
(
item
->
ob_type
,
&
PySet_Type
))
return
NULL
;
PyErr_Clear
();
tmp
=
frozenset_dict_wrapper
(((
PySetObject
*
)(
item
))
->
data
);
if
(
tmp
==
NULL
)
return
NULL
;
if
(
PyDict_DelItem
(
so
->
data
,
tmp
)
==
-
1
)
{
if
(
PyErr_ExceptionMatches
(
PyExc_KeyError
))
PyErr_Clear
();
else
{
Py_DECREF
(
tmp
);
return
NULL
;
}
}
Py_DECREF
(
tmp
);
}
}
Py_INCREF
(
Py_None
);
return
Py_None
;
...
...
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