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
Xavier Thompson
cython
Commits
307ad639
Commit
307ad639
authored
Jul 16, 2009
by
Kurt Smith
Committed by
Mark Florisson
Sep 30, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
memoryview utility code pulled in when memview array syntax used in Cython source.
parent
3bed371c
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
91 additions
and
19 deletions
+91
-19
Cython/Compiler/CythonScope.py
Cython/Compiler/CythonScope.py
+19
-10
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+27
-1
Cython/Compiler/MemoryView.py
Cython/Compiler/MemoryView.py
+31
-0
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+2
-1
Cython/Compiler/Pipeline.py
Cython/Compiler/Pipeline.py
+0
-2
Cython/Compiler/PyrexTypes.py
Cython/Compiler/PyrexTypes.py
+10
-2
tests/run/memoryview.pyx
tests/run/memoryview.pyx
+2
-3
No files found.
Cython/Compiler/CythonScope.py
View file @
307ad639
...
...
@@ -91,12 +91,11 @@ class CythonScope(ModuleScope):
#
# cython.view.memoryview declaration
#
name
=
u'memoryview'
entry
=
viewscope
.
declare_c_class
(
name
,
None
,
self
.
memviewentry
=
entry
=
viewscope
.
declare_c_class
(
memview_name
,
None
,
implementing
=
1
,
objstruct_cname
=
'__pyx_obj_'
+
name
,
typeobj_cname
=
'__pyx_tobj_'
+
name
,
typeptr_cname
=
Naming
.
typeptr_prefix
+
name
)
objstruct_cname
=
memviewext_objstruct_c
name
,
typeobj_cname
=
memviewext_typeobj_c
name
,
typeptr_cname
=
memviewext_typeptr_c
name
)
entry
.
utility_code_definition
=
view_utility_code
...
...
@@ -130,8 +129,7 @@ class CythonScope(ModuleScope):
for
idx
,
name
in
enumerate
((
'__getbuffer__'
,
'__releasebuffer__'
)):
entry
=
arr_scope
.
declare_pyfunction
(
name
,
None
)
# XXX: absolutely horrendous hack right here!!!
# To be fixed!!!
# FIXME XXX: hack right here!!!
entry
.
func_cname
=
'__pyx_pf_9__pyxutil_5array_%d'
%
(
idx
+
1
)
+
name
entry
.
utility_code_definition
=
cython_array_utility_code
...
...
@@ -236,6 +234,10 @@ cdef object _testscope(int value):
return "hello from cython.view scope, value=%d" % value
"""
)
memview_name
=
u'memoryview'
memviewext_typeptr_cname
=
Naming
.
typeptr_prefix
+
memview_name
memviewext_typeobj_cname
=
'__pyx_tobj_'
+
memview_name
memviewext_objstruct_cname
=
'__pyx_obj_'
+
memview_name
view_utility_code
=
CythonUtilityCode
(
u"""
cdef class Enum:
cdef object name
...
...
@@ -258,12 +260,19 @@ cdef extern from *:
cdef class memoryview:
cdef Py_buffer view
cdef int gotbuf_flag
def __cinit__(self):
self.gotbuf_flag = 0
def __cinit__
(memoryview self, obj, int flags):
cdef memoryview from_obj
(memoryview self, obj, int flags):
__Pyx_GetBuffer(obj, &self.view, flags)
self.gotbuf_flag = 1
def __dealloc__(memoryview self):
__Pyx_ReleaseBuffer(&self.view)
if self.gotbuf_flag:
__Pyx_ReleaseBuffer(&self.view)
self.gotbuf_flag = 0
"""
,
prefix
=
"__pyx_viewaxis_"
)
...
...
@@ -387,4 +396,4 @@ cdef class array:
self.shape = NULL
self.format = NULL
self.itemsize = 0
'''
,
prefix
=
cyarray_prefix
)
\ No newline at end of file
'''
,
prefix
=
cyarray_prefix
)
Cython/Compiler/ExprNodes.py
View file @
307ad639
...
...
@@ -584,6 +584,11 @@ class ExprNode(Node):
if
dst_type
.
is_reference
:
dst_type
=
dst_type
.
ref_base_type
if
dst_type
.
is_memoryview
:
src
=
CoerceToMemViewNode
(
src
,
dst_type
,
env
)
# src = AcquireBufferFromNode(src, env)
# src = CheckMemoryViewFormatNode(src, dst_type, env)
if
dst_type
.
is_pyobject
:
if
not
src
.
type
.
is_pyobject
:
if
dst_type
is
bytes_type
and
src
.
type
.
is_int
:
...
...
@@ -7565,6 +7570,28 @@ class CoercionNode(ExprNode):
file
,
line
,
col
=
self
.
pos
code
.
annotate
((
file
,
line
,
col
-
1
),
AnnotationItem
(
style
=
'coerce'
,
tag
=
'coerce'
,
text
=
'[%s] to [%s]'
%
(
self
.
arg
.
type
,
self
.
type
)))
class
CoerceToMemViewNode
(
CoercionNode
):
def
__init__
(
self
,
arg
,
dst_type
,
env
):
assert
dst_type
.
is_memoryview
CoercionNode
.
__init__
(
self
,
arg
)
self
.
type
=
dst_type
self
.
is_temp
=
1
self
.
env
=
env
def
generate_result_code
(
self
,
code
):
# create a cython.memoryview object.
# declare a new temporary cython.memoryview variable.
import
MemoryView
# -) initialize cython.memview object with self.arg, it calls
# __Pyx_GetBuffer on it.
# -) check the axes specifiers for the underlying memview's Py_buffer,
# make sure they're compatible with the dst_type's axes specs.
# -) output the temp assignment code (see
# CoerceFromPyTypeNode.generate_result_code for example)
pass
class
CastNode
(
CoercionNode
):
# Wrap a node in a C type cast.
...
...
@@ -7809,7 +7836,6 @@ class CoerceFromPyTypeNode(CoercionNode):
CoercionNode
.
__init__
(
self
,
arg
)
self
.
type
=
result_type
self
.
is_temp
=
1
import
pdb
;
pdb
.
set_trace
()
if
not
result_type
.
create_from_py_utility_code
(
env
):
error
(
arg
.
pos
,
"Cannot convert Python object to '%s'"
%
result_type
)
...
...
Cython/Compiler/MemoryView.py
View file @
307ad639
from
Errors
import
CompileError
from
ExprNodes
import
IntNode
,
NoneNode
,
IntBinopNode
,
NameNode
,
AttributeNode
from
Visitor
import
CythonTransform
import
Options
import
CythonScope
from
Code
import
UtilityCode
START_ERR
=
"there must be nothing or the value 0 (zero) in the start slot."
STOP_ERR
=
"Axis specification only allowed in the 'stop' slot."
...
...
@@ -12,6 +15,14 @@ INVALID_ERR = "Invalid axis specification."
EXPR_ERR
=
"no expressions allowed in axis spec, only names (e.g. cython.view.contig)."
CF_ERR
=
"Invalid axis specification for a C/Fortran contiguous array."
def
use_memview_util_code
(
env
):
import
CythonScope
cythonscope
=
env
.
global_scope
().
context
.
cython_scope
viewscope
=
cythonscope
.
viewscope
memview_entry
=
viewscope
.
lookup_here
(
CythonScope
.
memview_name
)
assert
memview_entry
is
cythonscope
.
memviewentry
memview_entry
.
used
=
1
def
get_axes_specs
(
env
,
axes
):
'''
get_axes_specs(env, axes) -> list of (access, packing) specs for each axis.
...
...
@@ -227,3 +238,23 @@ class MemoryViewTransform(CythonTransform):
def
visit_SingleAssignmentNode
(
self
,
node
):
import
pdb
;
pdb
.
set_trace
()
return
node
memviewstruct_cname
=
u'__Pyx_memviewstruct'
memviewstruct_declare_code
=
UtilityCode
(
proto
=
"""
/* memoryview struct */
typedef struct {
Py_ssize_t shape, strides, suboffsets;
} __Pyx_mv_DimInfo;
typedef struct {
struct %s *memviewext;
char *data;
__Pyx_mv_DimInfo diminfo[%d];
} %s;
"""
%
(
CythonScope
.
memviewext_objstruct_cname
,
Options
.
buffer_max_dims
,
memviewstruct_cname
)
)
Cython/Compiler/Nodes.py
View file @
307ad639
...
...
@@ -822,7 +822,8 @@ class MemoryViewTypeNode(CBaseTypeNode):
self
.
type
=
PyrexTypes
.
ErrorType
()
return
self
.
type
self
.
type
=
PyrexTypes
.
MemoryViewType
(
base_type
,
axes_specs
)
self
.
type
=
PyrexTypes
.
MemoryViewType
(
base_type
,
axes_specs
,
env
)
MemoryView
.
use_memview_util_code
(
env
)
return
self
.
type
class
CNestedBaseTypeNode
(
CBaseTypeNode
):
...
...
Cython/Compiler/Pipeline.py
View file @
307ad639
...
...
@@ -113,7 +113,6 @@ def create_pipeline(context, mode, exclude_classes=()):
from
Optimize
import
DropRefcountingTransform
from
Buffer
import
IntroduceBufferAuxiliaryVars
from
ModuleNode
import
check_c_declarations
,
check_c_declarations_pxd
from
MemoryView
import
MemoryViewTransform
from
ModuleNode
import
check_c_declarations
...
...
@@ -158,7 +157,6 @@ def create_pipeline(context, mode, exclude_classes=()):
# MarkAssignments(context),
MarkOverflowingArithmetic
(
context
),
IntroduceBufferAuxiliaryVars
(
context
),
MemoryViewTransform
(
context
),
_check_c_declarations
,
AnalyseExpressionsTransform
(
context
),
CreateClosureClasses
(
context
),
## After all lookups and type inference
...
...
Cython/Compiler/PyrexTypes.py
View file @
307ad639
...
...
@@ -313,7 +313,7 @@ class MemoryViewType(PyrexType):
is_memoryview
=
1
def
__init__
(
self
,
base_dtype
,
axes
):
def
__init__
(
self
,
base_dtype
,
axes
,
env
):
'''
MemoryViewType(base, axes)
...
...
@@ -350,6 +350,7 @@ class MemoryViewType(PyrexType):
self
.
dtype
=
base_dtype
self
.
axes
=
axes
self
.
env
=
env
def
is_complete
(
self
):
# incomplete since the underlying struct doesn't have a memoryview.
...
...
@@ -357,7 +358,14 @@ class MemoryViewType(PyrexType):
def
declaration_code
(
self
,
entity_code
,
for_display
=
0
,
dll_linkage
=
None
,
pyrex
=
0
):
return
'foo'
# XXX: we put these guards in for now...
assert
not
pyrex
assert
not
dll_linkage
import
MemoryView
self
.
env
.
use_utility_code
(
MemoryView
.
memviewstruct_declare_code
)
return
self
.
base_declaration_code
(
MemoryView
.
memviewstruct_cname
,
entity_code
)
class
BufferType
(
BaseType
):
#
...
...
tests/run/memoryview.pyx
View file @
307ad639
...
...
@@ -23,9 +23,8 @@ cdef class Foo:
class
pyfoo
:
def
__init__
(
self
):
cdef
int
arr
[
10
]
# self.mview = array((10,), itemsize=sizeof(long), format='l')
self
.
mview
=
arr
self
.
mview
=
array
((
10
,),
itemsize
=
sizeof
(
long
),
format
=
'l'
)
# self.mview = arr
cdef
cdg
():
cdef
double
[:]
dmv
=
array
((
10
,),
itemsize
=
sizeof
(
double
),
format
=
'd'
)
...
...
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