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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
d8cc99c7
Commit
d8cc99c7
authored
Dec 06, 2009
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
infer types of special args/kwargs parameters
parent
8b7c9fef
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
3 deletions
+27
-3
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+5
-2
Cython/Compiler/TypeInference.py
Cython/Compiler/TypeInference.py
+14
-1
tests/run/type_inference.pyx
tests/run/type_inference.pyx
+8
-0
No files found.
Cython/Compiler/Nodes.py
View file @
d8cc99c7
...
@@ -1872,8 +1872,11 @@ class DefNode(FuncDefNode):
...
@@ -1872,8 +1872,11 @@ class DefNode(FuncDefNode):
def
declare_python_arg
(
self
,
env
,
arg
):
def
declare_python_arg
(
self
,
env
,
arg
):
if
arg
:
if
arg
:
entry
=
env
.
declare_var
(
arg
.
name
,
if
env
.
directives
[
'infer_types'
]
!=
'none'
:
PyrexTypes
.
py_object_type
,
arg
.
pos
)
type
=
PyrexTypes
.
unspecified_type
else
:
type
=
py_object_type
entry
=
env
.
declare_var
(
arg
.
name
,
type
,
arg
.
pos
)
entry
.
used
=
1
entry
.
used
=
1
entry
.
init
=
"0"
entry
.
init
=
"0"
entry
.
init_to_none
=
0
entry
.
init_to_none
=
0
...
...
Cython/Compiler/TypeInference.py
View file @
d8cc99c7
import
ExprNodes
import
ExprNodes
import
Nodes
import
Builtin
import
PyrexTypes
import
PyrexTypes
from
PyrexTypes
import
py_object_type
,
unspecified_type
,
spanning_type
from
PyrexTypes
import
py_object_type
,
unspecified_type
,
spanning_type
from
Visitor
import
CythonTransform
from
Visitor
import
CythonTransform
...
@@ -20,7 +22,7 @@ object_expr = TypedExprNode(py_object_type)
...
@@ -20,7 +22,7 @@ object_expr = TypedExprNode(py_object_type)
class
MarkAssignments
(
CythonTransform
):
class
MarkAssignments
(
CythonTransform
):
def
mark_assignment
(
self
,
lhs
,
rhs
):
def
mark_assignment
(
self
,
lhs
,
rhs
):
if
isinstance
(
lhs
,
ExprNodes
.
NameNode
):
if
isinstance
(
lhs
,
(
ExprNodes
.
NameNode
,
Nodes
.
PyArgDeclNode
)
):
if
lhs
.
entry
is
None
:
if
lhs
.
entry
is
None
:
# TODO: This shouldn't happen...
# TODO: This shouldn't happen...
# It looks like comprehension loop targets are not declared soon enough.
# It looks like comprehension loop targets are not declared soon enough.
...
@@ -100,6 +102,17 @@ class MarkAssignments(CythonTransform):
...
@@ -100,6 +102,17 @@ class MarkAssignments(CythonTransform):
self
.
visitchildren
(
node
)
self
.
visitchildren
(
node
)
return
node
return
node
def
visit_DefNode
(
self
,
node
):
# use fake expressions with the right result type
if
node
.
star_arg
:
self
.
mark_assignment
(
node
.
star_arg
,
TypedExprNode
(
Builtin
.
tuple_type
))
if
node
.
starstar_arg
:
self
.
mark_assignment
(
node
.
starstar_arg
,
TypedExprNode
(
Builtin
.
dict_type
))
self
.
visitchildren
(
node
)
return
node
class
PyObjectTypeInferer
:
class
PyObjectTypeInferer
:
"""
"""
...
...
tests/run/type_inference.pyx
View file @
d8cc99c7
...
@@ -195,3 +195,11 @@ def safe_only():
...
@@ -195,3 +195,11 @@ def safe_only():
assert
typeof
(
b
)
==
"Python object"
,
typeof
(
c
)
assert
typeof
(
b
)
==
"Python object"
,
typeof
(
c
)
c
=
MyType
()
c
=
MyType
()
assert
typeof
(
c
)
==
"MyType"
,
typeof
(
c
)
assert
typeof
(
c
)
==
"MyType"
,
typeof
(
c
)
@
infer_types
(
'safe'
)
def
args_tuple_keywords
(
*
args
,
**
kwargs
):
"""
>>> args_tuple_keywords(1,2,3, a=1, b=2)
"""
assert
typeof
(
args
)
==
"tuple object"
,
typeof
(
args
)
assert
typeof
(
kwargs
)
==
"dict object"
,
typeof
(
kwargs
)
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