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
a59eab52
Commit
a59eab52
authored
Feb 11, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
type inference for some common operations between builtin types
parent
3c0051cb
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
5 deletions
+52
-5
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+22
-0
tests/run/type_inference.pyx
tests/run/type_inference.pyx
+30
-5
No files found.
Cython/Compiler/ExprNodes.py
View file @
a59eab52
...
...
@@ -4748,6 +4748,28 @@ class BinopNode(ExprNode):
def
result_type
(
self
,
type1
,
type2
):
if
self
.
is_py_operation_types
(
type1
,
type2
):
if
type2
.
is_string
:
type2
=
Builtin
.
bytes_type
if
type1
.
is_string
:
type1
=
Builtin
.
bytes_type
elif
self
.
operator
==
'%'
\
and
type1
in
(
Builtin
.
str_type
,
Builtin
.
unicode_type
):
# note that b'%s' % b'abc' doesn't work in Py3
return
type1
if
type1
.
is_builtin_type
:
if
type1
is
type2
:
if
self
.
operator
in
'**%+|&^'
:
# FIXME: at least these operators should be safe - others?
return
type1
elif
self
.
operator
==
'*'
:
if
type1
in
(
Builtin
.
bytes_type
,
Builtin
.
str_type
,
Builtin
.
unicode_type
):
return
type1
# multiplication of containers/numbers with an
# integer value always (?) returns the same type
if
type1
.
is_int
:
return
type2
elif
type2
.
is_int
:
return
type1
return
py_object_type
else
:
return
self
.
compute_c_result_type
(
type1
,
type2
)
...
...
tests/run/type_inference.pyx
View file @
a59eab52
...
...
@@ -93,13 +93,38 @@ def arithmetic():
>>> arithmetic()
"""
a
=
1
+
2
assert
typeof
(
a
)
==
"long"
assert
typeof
(
a
)
==
"long"
,
typeof
(
a
)
b
=
1
+
1.5
assert
typeof
(
b
)
==
"double"
assert
typeof
(
b
)
==
"double"
,
typeof
(
b
)
c
=
1
+
<
object
>
2
assert
typeof
(
c
)
==
"Python object"
d
=
"abc %s"
%
"x"
assert
typeof
(
d
)
==
"Python object"
assert
typeof
(
c
)
==
"Python object"
,
typeof
(
c
)
def
builtin_type_operations
():
"""
>>> builtin_type_operations()
"""
b1
=
b'a'
*
10
assert
typeof
(
b1
)
==
"bytes object"
,
typeof
(
b1
)
b2
=
b'a'
+
b'b'
assert
typeof
(
b2
)
==
"bytes object"
,
typeof
(
b2
)
u1
=
u'a'
*
10
assert
typeof
(
u1
)
==
"unicode object"
,
typeof
(
u1
)
u2
=
u'a'
+
u'b'
assert
typeof
(
u2
)
==
"unicode object"
,
typeof
(
u2
)
s1
=
"abc %s"
%
"x"
assert
typeof
(
s1
)
==
"str object"
,
typeof
(
s1
)
s2
=
"abc %s"
+
"x"
assert
typeof
(
s2
)
==
"str object"
,
typeof
(
s2
)
s3
=
"abc %s"
*
10
assert
typeof
(
s3
)
==
"str object"
,
typeof
(
s3
)
L1
=
[]
+
[]
assert
typeof
(
L1
)
==
"list object"
,
typeof
(
L1
)
L2
=
[]
*
2
assert
typeof
(
L2
)
==
"list object"
,
typeof
(
L2
)
T1
=
()
+
()
assert
typeof
(
T1
)
==
"tuple object"
,
typeof
(
T1
)
T2
=
()
*
2
assert
typeof
(
T2
)
==
"tuple object"
,
typeof
(
T2
)
def
cascade
():
"""
...
...
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