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
159eac92
Commit
159eac92
authored
Jun 23, 2009
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 6329: Fix iteration for memoryviews.
parent
b7706b58
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
32 deletions
+60
-32
Lib/test/test_memoryview.py
Lib/test/test_memoryview.py
+6
-0
Misc/NEWS
Misc/NEWS
+3
-0
Objects/memoryobject.c
Objects/memoryobject.c
+51
-32
No files found.
Lib/test/test_memoryview.py
View file @
159eac92
...
@@ -48,6 +48,12 @@ class AbstractMemoryTests:
...
@@ -48,6 +48,12 @@ class AbstractMemoryTests:
for
tp
in
self
.
_types
:
for
tp
in
self
.
_types
:
self
.
check_getitem_with_type
(
tp
)
self
.
check_getitem_with_type
(
tp
)
def
test_iter
(
self
):
for
tp
in
self
.
_types
:
b
=
tp
(
self
.
_source
)
m
=
self
.
_view
(
b
)
self
.
assertEqual
(
list
(
m
),
[
m
[
i
]
for
i
in
range
(
len
(
m
))])
def
test_setitem_readonly
(
self
):
def
test_setitem_readonly
(
self
):
if
not
self
.
ro_type
:
if
not
self
.
ro_type
:
return
return
...
...
Misc/NEWS
View file @
159eac92
...
@@ -12,6 +12,9 @@ What's New in Python 3.1?
...
@@ -12,6 +12,9 @@ What's New in Python 3.1?
Core and Builtins
Core and Builtins
-----------------
-----------------
- Issue #6329: Fixed iteration for memoryview objects (it was being blocked
because it wasn't recognized as a sequence).
Library
Library
-------
-------
...
...
Objects/memoryobject.c
View file @
159eac92
...
@@ -505,37 +505,19 @@ memory_length(PyMemoryViewObject *self)
...
@@ -505,37 +505,19 @@ memory_length(PyMemoryViewObject *self)
return
get_shape0
(
&
self
->
view
);
return
get_shape0
(
&
self
->
view
);
}
}
/*
/* Alternate version of memory_subcript that only accepts indices.
mem[obj] returns a bytes object holding the data for one element if
Used by PySeqIter_New().
obj fully indexes the memory view or another memory-view object
*/
if it does not.
0-d memory-view objects can be referenced using ... or () but
not with anything else.
*/
static
PyObject
*
static
PyObject
*
memory_
subscript
(
PyMemoryViewObject
*
self
,
PyObject
*
key
)
memory_
item
(
PyMemoryViewObject
*
self
,
Py_ssize_t
result
)
{
{
Py_buffer
*
view
;
Py_buffer
*
view
=
&
(
self
->
view
);
view
=
&
(
self
->
view
);
if
(
view
->
ndim
==
0
)
{
if
(
view
->
ndim
==
0
)
{
if
(
key
==
Py_Ellipsis
||
(
PyTuple_Check
(
key
)
&&
PyTuple_GET_SIZE
(
key
)
==
0
))
{
Py_INCREF
(
self
);
return
(
PyObject
*
)
self
;
}
else
{
PyErr_SetString
(
PyExc_IndexError
,
PyErr_SetString
(
PyExc_IndexError
,
"invalid indexing of 0-dim memory"
);
"invalid indexing of 0-dim memory"
);
return
NULL
;
return
NULL
;
}
}
}
if
(
PyIndex_Check
(
key
))
{
Py_ssize_t
result
;
result
=
PyNumber_AsSsize_t
(
key
,
NULL
);
if
(
result
==
-
1
&&
PyErr_Occurred
())
return
NULL
;
if
(
view
->
ndim
==
1
)
{
if
(
view
->
ndim
==
1
)
{
/* Return a bytes object */
/* Return a bytes object */
char
*
ptr
;
char
*
ptr
;
...
@@ -557,16 +539,47 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
...
@@ -557,16 +539,47 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
ptr
=
*
((
char
**
)
ptr
)
+
view
->
suboffsets
[
0
];
ptr
=
*
((
char
**
)
ptr
)
+
view
->
suboffsets
[
0
];
}
}
return
PyBytes_FromStringAndSize
(
ptr
,
view
->
itemsize
);
return
PyBytes_FromStringAndSize
(
ptr
,
view
->
itemsize
);
}
}
else
{
else
{
/* Return a new memory-view object */
/* Return a new memory-view object */
Py_buffer
newview
;
Py_buffer
newview
;
memset
(
&
newview
,
0
,
sizeof
(
newview
));
memset
(
&
newview
,
0
,
sizeof
(
newview
));
/* XXX: This needs to be fixed so it
/* XXX: This needs to be fixed so it actually returns a sub-view */
actually returns a sub-view
*/
return
PyMemoryView_FromBuffer
(
&
newview
);
return
PyMemoryView_FromBuffer
(
&
newview
);
}
}
}
/*
mem[obj] returns a bytes object holding the data for one element if
obj fully indexes the memory view or another memory-view object
if it does not.
0-d memory-view objects can be referenced using ... or () but
not with anything else.
*/
static
PyObject
*
memory_subscript
(
PyMemoryViewObject
*
self
,
PyObject
*
key
)
{
Py_buffer
*
view
;
view
=
&
(
self
->
view
);
if
(
view
->
ndim
==
0
)
{
if
(
key
==
Py_Ellipsis
||
(
PyTuple_Check
(
key
)
&&
PyTuple_GET_SIZE
(
key
)
==
0
))
{
Py_INCREF
(
self
);
return
(
PyObject
*
)
self
;
}
else
{
PyErr_SetString
(
PyExc_IndexError
,
"invalid indexing of 0-dim memory"
);
return
NULL
;
}
}
if
(
PyIndex_Check
(
key
))
{
Py_ssize_t
result
;
result
=
PyNumber_AsSsize_t
(
key
,
NULL
);
if
(
result
==
-
1
&&
PyErr_Occurred
())
return
NULL
;
return
memory_item
(
self
,
result
);
}
}
else
if
(
PySlice_Check
(
key
))
{
else
if
(
PySlice_Check
(
key
))
{
Py_ssize_t
start
,
stop
,
step
,
slicelength
;
Py_ssize_t
start
,
stop
,
step
,
slicelength
;
...
@@ -771,6 +784,12 @@ static PyMappingMethods memory_as_mapping = {
...
@@ -771,6 +784,12 @@ static PyMappingMethods memory_as_mapping = {
(
objobjargproc
)
memory_ass_sub
,
/* mp_ass_subscript */
(
objobjargproc
)
memory_ass_sub
,
/* mp_ass_subscript */
};
};
static
PySequenceMethods
memory_as_sequence
=
{
0
,
/* sq_length */
0
,
/* sq_concat */
0
,
/* sq_repeat */
(
ssizeargfunc
)
memory_item
,
/* sq_item */
};
/* Buffer methods */
/* Buffer methods */
...
@@ -792,7 +811,7 @@ PyTypeObject PyMemoryView_Type = {
...
@@ -792,7 +811,7 @@ PyTypeObject PyMemoryView_Type = {
0
,
/* tp_reserved */
0
,
/* tp_reserved */
(
reprfunc
)
memory_repr
,
/* tp_repr */
(
reprfunc
)
memory_repr
,
/* tp_repr */
0
,
/* tp_as_number */
0
,
/* tp_as_number */
0
,
/* tp_as_sequence */
&
memory_as_sequence
,
/* tp_as_sequence */
&
memory_as_mapping
,
/* tp_as_mapping */
&
memory_as_mapping
,
/* tp_as_mapping */
0
,
/* tp_hash */
0
,
/* tp_hash */
0
,
/* tp_call */
0
,
/* tp_call */
...
...
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