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
6d55fd18
Commit
6d55fd18
authored
Oct 24, 2015
by
memeplex
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add typecheck keyword.
parent
c1b040db
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
2 deletions
+34
-2
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/ParseTreeTransforms.py
+22
-1
Cython/Shadow.py
Cython/Shadow.py
+3
-1
docs/src/tutorial/pure.rst
docs/src/tutorial/pure.rst
+9
-0
No files found.
Cython/Compiler/ParseTreeTransforms.py
View file @
6d55fd18
...
...
@@ -2703,7 +2703,8 @@ class TransformBuiltinMethods(EnvTransform):
node
.
function
.
pos
,
operand1
=
node
.
args
[
0
],
operand2
=
node
.
args
[
1
])
elif
function
==
u'cast'
:
if
len
(
node
.
args
)
!=
2
:
error
(
node
.
function
.
pos
,
u"cast() takes exactly two arguments"
)
error
(
node
.
function
.
pos
,
u"cast() takes exactly two arguments and an optional typecheck keyword"
)
else
:
type
=
node
.
args
[
0
].
analyse_as_type
(
self
.
current_env
())
if
type
:
...
...
@@ -2754,6 +2755,26 @@ class TransformBuiltinMethods(EnvTransform):
return
self
.
_inject_super
(
node
,
func_name
)
return
node
def
visit_GeneralCallNode
(
self
,
node
):
function
=
node
.
function
.
as_cython_attribute
()
if
function
:
args
=
node
.
positional_args
.
args
kwargs
=
node
.
keyword_args
.
compile_time_value
(
None
)
if
function
==
u'cast'
:
if
(
len
(
args
)
!=
2
or
len
(
kwargs
)
>
1
or
(
len
(
kwargs
)
==
1
and
'typecheck'
not
in
kwargs
)):
error
(
node
.
function
.
pos
,
u"cast() takes exactly two arguments and an optional typecheck keyword"
)
else
:
type
=
args
[
0
].
analyse_as_type
(
self
.
current_env
())
if
type
:
typecheck
=
kwargs
.
get
(
'typecheck'
,
False
)
node
=
ExprNodes
.
TypecastNode
(
node
.
function
.
pos
,
type
=
type
,
operand
=
args
[
1
],
typecheck
=
typecheck
)
else
:
error
(
args
[
0
].
pos
,
"Not a type"
)
return
node
class
ReplaceFusedTypeChecks
(
VisitorTransform
):
"""
...
...
Cython/Shadow.py
View file @
6d55fd18
...
...
@@ -136,7 +136,9 @@ def cmod(a, b):
# Emulated language constructs
def
cast
(
type
,
*
args
):
def
cast
(
type
,
*
args
,
**
kwargs
):
kwargs
.
pop
(
'typecheck'
,
None
)
assert
not
kwargs
if
hasattr
(
type
,
'__call__'
):
return
type
(
*
args
)
else
:
...
...
docs/src/tutorial/pure.rst
View file @
6d55fd18
...
...
@@ -287,6 +287,15 @@ Further Cython functions and declarations
T = cython.typedef(cython.p_int) # ctypedef int* T
* ``cast`` will (unsafely) reinterpret an expression type. ``cython.cast(T, t)``
is equivalent to ``<T>t``. The first attribute must be a type, the second is
the expression to cast. Specifying the optional keyword argument
``typecheck=True`` has the semantics of ``<T?>t``.
::
t1 = cython.cast(T, t)
t2 = cython.cast(T, t, typecheck=True)
.. _magic_attributes_pxd:
...
...
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