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
Gwenaël Samain
cython
Commits
93ade469
Commit
93ade469
authored
Sep 08, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
infer type of slices for some builtin types
parent
ae84b380
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
2 deletions
+13
-2
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+9
-2
tests/run/type_inference.pyx
tests/run/type_inference.pyx
+4
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
93ade469
...
...
@@ -1942,8 +1942,12 @@ class IndexNode(ExprNode):
return
self
.
base
.
type_dependencies
(
env
)
def
infer_type
(
self
,
env
):
is_slice
=
isinstance
(
self
.
index
,
SliceNode
)
if
isinstance
(
self
.
base
,
BytesNode
):
return
py_object_type
if
is_slice
:
return
bytes_type
else
:
return
py_object_type
# Py2/3 return different types
base_type
=
self
.
base
.
infer_type
(
env
)
if
base_type
.
is_ptr
or
base_type
.
is_array
:
return
base_type
.
base_type
...
...
@@ -1957,7 +1961,10 @@ class IndexNode(ExprNode):
# on a subsequent PyObject coercion.
return
PyrexTypes
.
c_py_unicode_type
elif
base_type
in
(
str_type
,
unicode_type
):
# these types will always return themselves on Python indexing
# these types will always return their own type on Python indexing/slicing
return
base_type
elif
is_slice
and
base_type
in
(
bytes_type
,
list_type
,
tuple_type
):
# slicing these returns the same type
return
base_type
else
:
# TODO: Handle buffers (hopefully without too much redundancy).
...
...
tests/run/type_inference.pyx
View file @
93ade469
...
...
@@ -68,10 +68,14 @@ def slicing():
assert
typeof
(
L
)
==
"list object"
,
typeof
(
L
)
L1
=
L
[
1
:
2
]
assert
typeof
(
L1
)
==
"list object"
,
typeof
(
L1
)
L2
=
L
[
1
:
2
:
2
]
assert
typeof
(
L2
)
==
"list object"
,
typeof
(
L2
)
t
=
(
4
,
5
,
6
)
assert
typeof
(
t
)
==
"tuple object"
,
typeof
(
t
)
t1
=
t
[
1
:
2
]
assert
typeof
(
t1
)
==
"tuple object"
,
typeof
(
t1
)
t2
=
t
[
1
:
2
:
2
]
assert
typeof
(
t2
)
==
"tuple object"
,
typeof
(
t2
)
def
indexing
():
"""
...
...
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