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
Boxiang Sun
cython
Commits
2d2fba93
Commit
2d2fba93
authored
Oct 30, 2010
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
numpy and extension types for runtime cython
parent
d0a66db8
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
7 deletions
+31
-7
Cython/Build/Inline.py
Cython/Build/Inline.py
+31
-7
No files found.
Cython/Build/Inline.py
View file @
2d2fba93
...
...
@@ -7,45 +7,69 @@ except ImportError:
import
md5
as
hashlib
from
distutils.dist
import
Distribution
from
distutils.core
import
Extension
from
Cython.Distutils.extension
import
Extension
from
Cython.Distutils
import
build_ext
from
Cython.Compiler.Main
import
Context
,
CompilationOptions
,
default_options
code_cache
=
{}
def
get_type
(
arg
):
def
get_type
(
arg
,
context
=
None
):
py_type
=
type
(
arg
)
# TODO: numpy
# TODO: extension types
if
py_type
in
[
list
,
tuple
,
dict
,
str
]:
return
py_type
.
__name__
elif
py_type
is
float
:
return
'double'
elif
py_type
is
bool
:
return
'bint'
elif
py_type
is
int
:
return
'long'
elif
'numpy'
in
sys
.
modules
and
isinstance
(
arg
,
sys
.
modules
[
'numpy'
].
ndarray
):
return
'numpy.ndarray[numpy.%s_t, ndim=%s]'
%
(
arg
.
dtype
.
name
,
arg
.
ndim
)
else
:
for
base_type
in
py_type
.
mro
():
if
base_type
.
__module__
==
'__builtin__'
:
return
'object'
module
=
context
.
find_module
(
base_type
.
__module__
,
need_pxd
=
False
)
if
module
:
entry
=
module
.
lookup
(
base_type
.
__name__
)
if
entry
.
is_type
:
return
'%s.%s'
%
(
base_type
.
__module__
,
base_type
.
__name__
)
return
'object'
# TODO: use locals/globals for unbound variables
def
cython_inline
(
code
,
types
=
'aggressive'
,
lib_dir
=
os
.
path
.
expanduser
(
'~/.cython/inline'
),
**
kwds
):
def
cython_inline
(
code
,
types
=
'aggressive'
,
lib_dir
=
os
.
path
.
expanduser
(
'~/.cython/inline'
),
include_dirs
=
[
'.'
],
**
kwds
):
ctx
=
Context
(
include_dirs
,
default_options
)
_
,
pyx_file
=
tempfile
.
mkstemp
(
'.pyx'
)
arg_names
=
kwds
.
keys
()
arg_names
.
sort
()
arg_sigs
=
tuple
((
get_type
(
kwds
[
arg
]),
arg
)
for
arg
in
arg_names
)
arg_sigs
=
tuple
((
get_type
(
kwds
[
arg
]
,
ctx
),
arg
)
for
arg
in
arg_names
)
key
=
code
,
arg_sigs
module
=
code_cache
.
get
(
key
)
if
not
module
:
cimports
=
''
qualified
=
re
.
compile
(
r'([.\
w]+)[.]
')
for type, _ in arg_sigs:
m = qualified.match(type)
if m:
cimports += '
\
ncimport
%
s
' % m.groups()[0]
module_body, func_body = extract_func_code(code)
params = '
,
'.join('
%
s
%
s
' % a for a in arg_sigs)
module_code = """
%(cimports)s
%(module_body)s
def __invoke(%(params)s):
%(func_body)s
""" % locals()
print module_code
open(pyx_file, '
w
').write(module_code)
module = "_" + hashlib.md5(code + str(arg_sigs)).hexdigest()
extension = Extension(
name = module,
sources
=
[
pyx_file
])
sources = [pyx_file],
pyrex_include_dirs = include_dirs)
build_extension = build_ext(Distribution())
build_extension.finalize_options()
build_extension.extensions = [extension]
...
...
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