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
1e12bf46
Commit
1e12bf46
authored
Feb 06, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
when indexing 'str', it's safe to infer 'str' for the result
parent
f76ca90f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
9 additions
and
3 deletions
+9
-3
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+3
-0
tests/run/type_inference.pyx
tests/run/type_inference.pyx
+6
-3
No files found.
Cython/Compiler/ExprNodes.py
View file @
1e12bf46
...
@@ -2074,6 +2074,9 @@ class IndexNode(ExprNode):
...
@@ -2074,6 +2074,9 @@ class IndexNode(ExprNode):
# to receive it, throw it away, and potentially rebuild it
# to receive it, throw it away, and potentially rebuild it
# on a subsequent PyObject coercion.
# on a subsequent PyObject coercion.
return
PyrexTypes
.
c_py_ucs4_type
return
PyrexTypes
.
c_py_ucs4_type
elif
base_type
is
str_type
:
# always returns str - Py2: bytes, Py3: unicode
return
base_type
elif
isinstance
(
self
.
base
,
BytesNode
):
elif
isinstance
(
self
.
base
,
BytesNode
):
#if env.global_scope().context.language_level >= 3:
#if env.global_scope().context.language_level >= 3:
# # infering 'char' can be made to work in Python 3 mode
# # infering 'char' can be made to work in Python 3 mode
...
...
tests/run/type_inference.pyx
View file @
1e12bf46
...
@@ -94,7 +94,7 @@ def indexing():
...
@@ -94,7 +94,7 @@ def indexing():
b
=
b"abc"
b
=
b"abc"
assert
typeof
(
b
)
==
"bytes object"
,
typeof
(
b
)
assert
typeof
(
b
)
==
"bytes object"
,
typeof
(
b
)
b1
=
b
[
1
]
b1
=
b
[
1
]
assert
typeof
(
b1
)
==
"Python object"
,
typeof
(
b1
)
assert
typeof
(
b1
)
==
"Python object"
,
typeof
(
b1
)
# Py2: bytes, Py3: int
u
=
u"xyz"
u
=
u"xyz"
assert
typeof
(
u
)
==
"unicode object"
,
typeof
(
u
)
assert
typeof
(
u
)
==
"unicode object"
,
typeof
(
u
)
u1
=
u
[
1
]
u1
=
u
[
1
]
...
@@ -102,7 +102,7 @@ def indexing():
...
@@ -102,7 +102,7 @@ def indexing():
s
=
"xyz"
s
=
"xyz"
assert
typeof
(
s
)
==
"str object"
,
typeof
(
s
)
assert
typeof
(
s
)
==
"str object"
,
typeof
(
s
)
s1
=
s
[
1
]
s1
=
s
[
1
]
assert
typeof
(
s1
)
==
"
Python
object"
,
typeof
(
s1
)
assert
typeof
(
s1
)
==
"
str
object"
,
typeof
(
s1
)
L
=
[
1
,
2
,
3
]
L
=
[
1
,
2
,
3
]
assert
typeof
(
L
)
==
"list object"
,
typeof
(
L
)
assert
typeof
(
L
)
==
"list object"
,
typeof
(
L
)
L1
=
L
[
1
]
L1
=
L
[
1
]
...
@@ -296,6 +296,7 @@ def loop_over_bytes():
...
@@ -296,6 +296,7 @@ def loop_over_bytes():
Python object
Python object
"""
"""
cdef
bytes
bytes_string
=
b'abcdefg'
cdef
bytes
bytes_string
=
b'abcdefg'
# bytes in Py2, int in Py3
for
c
in
bytes_string
:
for
c
in
bytes_string
:
pass
pass
return
typeof
(
c
)
return
typeof
(
c
)
...
@@ -303,9 +304,10 @@ def loop_over_bytes():
...
@@ -303,9 +304,10 @@ def loop_over_bytes():
def
loop_over_str
():
def
loop_over_str
():
"""
"""
>>> print( loop_over_str() )
>>> print( loop_over_str() )
Python
object
str
object
"""
"""
cdef
str
string
=
'abcdefg'
cdef
str
string
=
'abcdefg'
# str (bytes) in Py2, str (unicode) in Py3
for
c
in
string
:
for
c
in
string
:
pass
pass
return
typeof
(
c
)
return
typeof
(
c
)
...
@@ -316,6 +318,7 @@ def loop_over_unicode():
...
@@ -316,6 +318,7 @@ def loop_over_unicode():
Py_UCS4
Py_UCS4
"""
"""
cdef
unicode
ustring
=
u'abcdefg'
cdef
unicode
ustring
=
u'abcdefg'
# Py_UCS4 can represent any Unicode character
for
uchar
in
ustring
:
for
uchar
in
ustring
:
pass
pass
return
typeof
(
uchar
)
return
typeof
(
uchar
)
...
...
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