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
d3091283
Commit
d3091283
authored
Oct 10, 2007
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.
parent
665ca01e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
12 deletions
+45
-12
Doc/library/collections.rst
Doc/library/collections.rst
+1
-1
Lib/test/test_deque.py
Lib/test/test_deque.py
+18
-1
Modules/_collectionsmodule.c
Modules/_collectionsmodule.c
+26
-10
No files found.
Doc/library/collections.rst
View file @
d3091283
...
...
@@ -51,7 +51,7 @@ ordered dictionaries.
.. versionadded:: 2.4
If *maxlen* is not specified or is *
-1
*, deques may grow to an
If *maxlen* is not specified or is *
None
*, deques may grow to an
arbitrary length. Otherwise, the deque is bounded to the specified maximum
length. Once a bounded length deque is full, when new items are added, a
corresponding number of items are discarded from the opposite end. Bounded
...
...
Lib/test/test_deque.py
View file @
d3091283
...
...
@@ -48,6 +48,7 @@ class TestBasic(unittest.TestCase):
self
.
assertEqual
(
list
(
d
),
range
(
50
,
150
))
def
test_maxlen
(
self
):
self
.
assertRaises
(
ValueError
,
deque
,
'abc'
,
-
1
)
self
.
assertRaises
(
ValueError
,
deque
,
'abc'
,
-
2
)
d
=
deque
(
range
(
10
),
maxlen
=
3
)
self
.
assertEqual
(
repr
(
d
),
'deque([7, 8, 9], maxlen=3)'
)
...
...
@@ -73,7 +74,7 @@ class TestBasic(unittest.TestCase):
fo
.
close
()
os
.
remove
(
test_support
.
TESTFN
)
d
=
deque
(
range
(
10
),
maxlen
=
-
1
)
d
=
deque
(
range
(
10
),
maxlen
=
None
)
self
.
assertEqual
(
repr
(
d
),
'deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])'
)
try
:
fo
=
open
(
test_support
.
TESTFN
,
"wb"
)
...
...
@@ -489,6 +490,22 @@ class TestSubclass(unittest.TestCase):
self
.
assertEqual
(
type
(
d
),
type
(
e
))
self
.
assertEqual
(
list
(
d
),
list
(
e
))
d
=
Deque
(
'abcde'
,
maxlen
=
4
)
e
=
d
.
__copy__
()
self
.
assertEqual
(
type
(
d
),
type
(
e
))
self
.
assertEqual
(
list
(
d
),
list
(
e
))
e
=
Deque
(
d
)
self
.
assertEqual
(
type
(
d
),
type
(
e
))
self
.
assertEqual
(
list
(
d
),
list
(
e
))
s
=
pickle
.
dumps
(
d
)
e
=
pickle
.
loads
(
s
)
self
.
assertNotEqual
(
id
(
d
),
id
(
e
))
self
.
assertEqual
(
type
(
d
),
type
(
e
))
self
.
assertEqual
(
list
(
d
),
list
(
e
))
## def test_pickle(self):
## d = Deque('abc')
## d.append(d)
...
...
Modules/_collectionsmodule.c
View file @
d3091283
...
...
@@ -598,6 +598,9 @@ deque_nohash(PyObject *self)
static
PyObject
*
deque_copy
(
PyObject
*
deque
)
{
if
(((
dequeobject
*
)
deque
)
->
maxlen
==
-
1
)
return
PyObject_CallFunction
((
PyObject
*
)(
Py_Type
(
deque
)),
"O"
,
deque
,
NULL
);
else
return
PyObject_CallFunction
((
PyObject
*
)(
Py_Type
(
deque
)),
"Oi"
,
deque
,
((
dequeobject
*
)
deque
)
->
maxlen
,
NULL
);
}
...
...
@@ -617,10 +620,17 @@ deque_reduce(dequeobject *deque)
Py_XDECREF
(
dict
);
return
NULL
;
}
if
(
dict
==
NULL
)
if
(
dict
==
NULL
)
{
if
(
deque
->
maxlen
==
-
1
)
result
=
Py_BuildValue
(
"O(O)"
,
Py_Type
(
deque
),
aslist
);
else
result
=
Py_BuildValue
(
"O(Oi)"
,
Py_Type
(
deque
),
aslist
,
deque
->
maxlen
);
}
else
{
if
(
deque
->
maxlen
==
-
1
)
result
=
Py_BuildValue
(
"O(OO)O"
,
Py_Type
(
deque
),
aslist
,
Py_None
,
dict
);
else
result
=
Py_BuildValue
(
"O(Oi)O"
,
Py_Type
(
deque
),
aslist
,
deque
->
maxlen
,
dict
);
}
Py_XDECREF
(
dict
);
Py_DECREF
(
aslist
);
return
result
;
...
...
@@ -797,14 +807,20 @@ static int
deque_init
(
dequeobject
*
deque
,
PyObject
*
args
,
PyObject
*
kwdargs
)
{
PyObject
*
iterable
=
NULL
;
PyObject
*
maxlenobj
=
NULL
;
int
maxlen
=
-
1
;
char
*
kwlist
[]
=
{
"iterable"
,
"maxlen"
,
0
};
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwdargs
,
"|O
i:deque"
,
kwlist
,
&
iterable
,
&
maxlen
))
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwdargs
,
"|O
O:deque"
,
kwlist
,
&
iterable
,
&
maxlenobj
))
return
-
1
;
if
(
maxlen
<
-
1
)
{
PyErr_SetString
(
PyExc_ValueError
,
"maxlen must be -1 or greater"
);
if
(
maxlenobj
!=
NULL
&&
maxlenobj
!=
Py_None
)
{
maxlen
=
PyInt_AsLong
(
maxlenobj
);
if
(
maxlen
==
-
1
&&
PyErr_Occurred
())
return
-
1
;
if
(
maxlen
<
0
)
{
PyErr_SetString
(
PyExc_ValueError
,
"maxlen must be non-negative"
);
return
-
1
;
}
}
deque
->
maxlen
=
maxlen
;
if
(
iterable
!=
NULL
)
{
...
...
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