Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
7bebbddd
Commit
7bebbddd
authored
Aug 15, 2018
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support memoryviews in PEP-489 notation.
See #2529.
parent
6005320d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
111 additions
and
1 deletion
+111
-1
CHANGES.rst
CHANGES.rst
+3
-0
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+12
-1
tests/memoryview/memoryview_pep489_typing.pyx
tests/memoryview/memoryview_pep489_typing.pyx
+96
-0
No files found.
CHANGES.rst
View file @
7bebbddd
...
@@ -17,6 +17,9 @@ Features added
...
@@ -17,6 +17,9 @@ Features added
module that can be used for typing in Python code.
module that can be used for typing in Python code.
Original patch by Julian Gethmann. (Github issue #1965)
Original patch by Julian Gethmann. (Github issue #1965)
* Memoryviews are supported in PEP-489 style type declarations.
(Github issue #2529)
* Raising exceptions from nogil code will automatically acquire the GIL, instead
* Raising exceptions from nogil code will automatically acquire the GIL, instead
of requiring an explicit ``with gil`` block.
of requiring an explicit ``with gil`` block.
...
...
Cython/Compiler/ExprNodes.py
View file @
7bebbddd
...
@@ -3400,10 +3400,21 @@ class IndexNode(_IndexingBaseNode):
...
@@ -3400,10 +3400,21 @@ class IndexNode(_IndexingBaseNode):
positional_args
=
template_values
,
positional_args
=
template_values
,
keyword_args
=
None
)
keyword_args
=
None
)
return
type_node
.
analyse
(
env
,
base_type
=
base_type
)
return
type_node
.
analyse
(
env
,
base_type
=
base_type
)
elif
self
.
index
.
is_slice
or
self
.
index
.
is_sequence_constructor
:
# memory view
from
.
import
MemoryView
axes
=
[
self
.
index
]
if
self
.
index
.
is_slice
else
list
(
self
.
index
.
args
)
return
PyrexTypes
.
MemoryViewSliceType
(
base_type
,
MemoryView
.
get_axes_specs
(
env
,
axes
))
else
:
else
:
# C array
index
=
self
.
index
.
compile_time_value
(
env
)
index
=
self
.
index
.
compile_time_value
(
env
)
if
index
is
not
None
:
if
index
is
not
None
:
return
PyrexTypes
.
CArrayType
(
base_type
,
int
(
index
))
try
:
index
=
int
(
index
)
except
(
ValueError
,
TypeError
):
pass
else
:
return
PyrexTypes
.
CArrayType
(
base_type
,
index
)
error
(
self
.
pos
,
"Array size must be a compile time constant"
)
error
(
self
.
pos
,
"Array size must be a compile time constant"
)
return
None
return
None
...
...
tests/memoryview/memoryview_pep489_typing.pyx
0 → 100644
View file @
7bebbddd
# mode: run
# tag: pep489, memoryview
cimport
cython
include
"../buffers/mockbuffers.pxi"
def
get_int_2d
(
mslice
:
cython
.
int
[:,
:],
i
:
cython
.
int
,
j
:
cython
.
int
)
->
cython
.
int
:
"""
>>> C = IntMockBuffer("C", range(6), (2,3))
>>> get_int_2d(C, 1, 1)
acquired C
released C
4
Check negative indexing:
>>> get_int_2d(C, -1, 0)
acquired C
released C
3
>>> get_int_2d(C, -1, -2)
acquired C
released C
4
>>> get_int_2d(C, -2, -3)
acquired C
released C
0
Out-of-bounds errors:
>>> get_int_2d(C, 2, 0)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 0)
>>> get_int_2d(C, 0, -4)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 1)
"""
buf
=
mslice
return
buf
[
i
,
j
]
def
set_int_2d
(
mslice
:
cython
.
int
[:,
:],
i
:
cython
.
int
,
j
:
cython
.
int
,
value
:
cython
.
int
)
->
cython
.
int
:
"""
Uses get_int_2d to read back the value afterwards. For pure
unit test, one should support reading in MockBuffer instead.
>>> C = IntMockBuffer("C", range(6), (2,3))
>>> set_int_2d(C, 1, 1, 10)
acquired C
released C
>>> get_int_2d(C, 1, 1)
acquired C
released C
10
Check negative indexing:
>>> set_int_2d(C, -1, 0, 3)
acquired C
released C
>>> get_int_2d(C, -1, 0)
acquired C
released C
3
>>> set_int_2d(C, -1, -2, 8)
acquired C
released C
>>> get_int_2d(C, -1, -2)
acquired C
released C
8
>>> set_int_2d(C, -2, -3, 9)
acquired C
released C
>>> get_int_2d(C, -2, -3)
acquired C
released C
9
Out-of-bounds errors:
>>> set_int_2d(C, 2, 0, 19)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 0)
>>> set_int_2d(C, 0, -4, 19)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 1)
"""
buf
=
mslice
buf
[
i
,
j
]
=
value
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