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
4db28d33
Commit
4db28d33
authored
Mar 03, 2011
by
Eli Bendersky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #10516: added copy() and clear() methods to bytearrays as well
parent
91221f28
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
63 additions
and
5 deletions
+63
-5
Doc/library/stdtypes.rst
Doc/library/stdtypes.rst
+3
-4
Lib/test/test_bytes.py
Lib/test/test_bytes.py
+33
-0
Misc/NEWS
Misc/NEWS
+1
-1
Objects/bytearrayobject.c
Objects/bytearrayobject.c
+26
-0
No files found.
Doc/library/stdtypes.rst
View file @
4db28d33
...
...
@@ -1675,10 +1675,10 @@ Note that while lists allow their items to be of any type, bytearray object
| ``s.extend(x)`` | same as ``s[len(s):len(s)] = | \(2) |
| | x`` | |
+------------------------------+--------------------------------+---------------------+
| ``s.clear()`` | remove all items from ``s`` |
\(8)
|
| ``s.clear()`` | remove all items from ``s`` |
|
| | | |
+------------------------------+--------------------------------+---------------------+
| ``s.copy()`` | return a shallow copy of ``s`` |
\(8)
|
| ``s.copy()`` | return a shallow copy of ``s`` |
|
| | | |
+------------------------------+--------------------------------+---------------------+
| ``s.count(x)`` | return number of *i*'s for | |
...
...
@@ -1757,8 +1757,7 @@ Notes:
detect that the list has been mutated during a sort.
(8)
:meth:`clear`, :meth:`!copy` and :meth:`sort` are not supported by
:class:`bytearray` objects.
:meth:`sort` is not supported by :class:`bytearray` objects.
.. versionadded:: 3.3
:meth:`clear` and :meth:`!copy` methods.
...
...
Lib/test/test_bytes.py
View file @
4db28d33
...
...
@@ -564,6 +564,39 @@ class ByteArrayTest(BaseBytesTest):
b
.
reverse
()
self
.
assertFalse
(
b
)
def
test_clear
(
self
):
b
=
bytearray
(
b'python'
)
b
.
clear
()
self
.
assertEqual
(
b
,
b''
)
b
=
bytearray
(
b''
)
b
.
clear
()
self
.
assertEqual
(
b
,
b''
)
b
=
bytearray
(
b''
)
b
.
append
(
ord
(
'r'
))
b
.
clear
()
b
.
append
(
ord
(
'p'
))
self
.
assertEqual
(
b
,
b'p'
)
def
test_copy
(
self
):
b
=
bytearray
(
b'abc'
)
bb
=
b
.
copy
()
self
.
assertEqual
(
bb
,
b'abc'
)
b
=
bytearray
(
b''
)
bb
=
b
.
copy
()
self
.
assertEqual
(
bb
,
b''
)
# test that it's indeed a copy and not a reference
b
=
bytearray
(
b'abc'
)
bb
=
b
.
copy
()
self
.
assertEqual
(
b
,
bb
)
self
.
assertIsNot
(
b
,
bb
)
bb
.
append
(
ord
(
'd'
))
self
.
assertEqual
(
bb
,
b'abcd'
)
self
.
assertEqual
(
b
,
b'abc'
)
def
test_regexps
(
self
):
def
by
(
s
):
return
bytearray
(
map
(
ord
,
s
))
...
...
Misc/NEWS
View file @
4db28d33
...
...
@@ -47,7 +47,7 @@ Core and Builtins
- Check for NULL result in PyType_FromSpec.
- Issue #10516: New copy() and clear() methods for lists.
- Issue #10516: New copy() and clear() methods for lists
and bytearrays
.
Library
-------
...
...
Objects/bytearrayobject.c
View file @
4db28d33
...
...
@@ -1148,6 +1148,30 @@ bytearray_count(PyByteArrayObject *self, PyObject *args)
return
count_obj
;
}
PyDoc_STRVAR
(
clear__doc__
,
"B.clear() -> None
\n
\
\n
\
Remove all items from B."
);
static
PyObject
*
bytearray_clear
(
PyByteArrayObject
*
self
)
{
if
(
PyByteArray_Resize
((
PyObject
*
)
self
,
0
)
<
0
)
return
NULL
;
Py_RETURN_NONE
;
}
PyDoc_STRVAR
(
copy__doc__
,
"B.copy() -> bytearray
\n
\
\n
\
Return a copy of B."
);
static
PyObject
*
bytearray_copy
(
PyByteArrayObject
*
self
)
{
return
PyByteArray_FromStringAndSize
(
PyByteArray_AS_STRING
((
PyObject
*
)
self
),
PyByteArray_GET_SIZE
(
self
));
}
PyDoc_STRVAR
(
index__doc__
,
"B.index(sub[, start[, end]]) -> int
\n
\
...
...
@@ -2730,6 +2754,8 @@ bytearray_methods[] = {
{
"capitalize"
,
(
PyCFunction
)
stringlib_capitalize
,
METH_NOARGS
,
_Py_capitalize__doc__
},
{
"center"
,
(
PyCFunction
)
stringlib_center
,
METH_VARARGS
,
center__doc__
},
{
"clear"
,
(
PyCFunction
)
bytearray_clear
,
METH_NOARGS
,
clear__doc__
},
{
"copy"
,
(
PyCFunction
)
bytearray_copy
,
METH_NOARGS
,
copy__doc__
},
{
"count"
,
(
PyCFunction
)
bytearray_count
,
METH_VARARGS
,
count__doc__
},
{
"decode"
,
(
PyCFunction
)
bytearray_decode
,
METH_VARARGS
|
METH_KEYWORDS
,
decode_doc
},
{
"endswith"
,
(
PyCFunction
)
bytearray_endswith
,
METH_VARARGS
,
endswith__doc__
},
...
...
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