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
Kirill Smelkov
cython
Commits
d737295f
Commit
d737295f
authored
Jul 21, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
generate the expected (fast) code for isinstance() checks on builtin types
parent
9215510d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
12 deletions
+104
-12
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+53
-0
tests/run/isinstance.pyx
tests/run/isinstance.pyx
+51
-12
No files found.
Cython/Compiler/Optimize.py
View file @
d737295f
...
@@ -1839,6 +1839,59 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
...
@@ -1839,6 +1839,59 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
is_temp
=
False
)
is_temp
=
False
)
return
ExprNodes
.
CastNode
(
node
,
PyrexTypes
.
py_object_type
)
return
ExprNodes
.
CastNode
(
node
,
PyrexTypes
.
py_object_type
)
Py_type_check_func_type
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
c_bint_type
,
[
PyrexTypes
.
CFuncTypeArg
(
"arg"
,
PyrexTypes
.
py_object_type
,
None
)
])
def
_handle_simple_function_isinstance
(
self
,
node
,
pos_args
):
"""Replace isinstance() checks against builtin types by the
corresponding C-API call.
"""
if
len
(
pos_args
)
!=
2
:
return
node
arg
,
types
=
pos_args
temp
=
None
if
isinstance
(
types
,
ExprNodes
.
TupleNode
):
types
=
types
.
args
arg
=
temp
=
UtilNodes
.
ResultRefNode
(
arg
)
elif
types
.
type
is
Builtin
.
type_type
:
types
=
[
types
]
else
:
return
node
tests
=
[]
test_nodes
=
[]
env
=
self
.
current_env
()
for
test_type_node
in
types
:
if
not
test_type_node
.
entry
:
return
node
entry
=
env
.
lookup
(
test_type_node
.
entry
.
name
)
if
not
entry
or
not
entry
.
type
or
not
entry
.
type
.
is_builtin_type
:
return
node
type_check_function
=
entry
.
type
.
type_check_function
(
exact
=
False
)
if
not
type_check_function
:
return
node
if
type_check_function
not
in
tests
:
tests
.
append
(
type_check_function
)
test_nodes
.
append
(
ExprNodes
.
PythonCapiCallNode
(
test_type_node
.
pos
,
type_check_function
,
self
.
Py_type_check_func_type
,
args
=
[
arg
],
is_temp
=
True
,
))
def
join_with_or
(
a
,
b
,
make_binop_node
=
ExprNodes
.
binop_node
):
or_node
=
make_binop_node
(
node
.
pos
,
'or'
,
a
,
b
)
or_node
.
type
=
PyrexTypes
.
c_bint_type
or_node
.
is_temp
=
True
return
or_node
test_node
=
reduce
(
join_with_or
,
test_nodes
).
coerce_to
(
node
.
type
,
env
)
if
temp
is
not
None
:
test_node
=
UtilNodes
.
EvalWithTempExprNode
(
temp
,
test_node
)
return
test_node
### special methods
### special methods
Pyx_tp_new_func_type
=
PyrexTypes
.
CFuncType
(
Pyx_tp_new_func_type
=
PyrexTypes
.
CFuncType
(
...
...
tests/run/isinstance.pyx
View file @
d737295f
cimport
cython
cdef
class
A
:
cdef
class
A
:
pass
pass
def
test_all
():
@
cython
.
test_assert_path_exists
(
'//SimpleCallNode//SimpleCallNode'
)
@
cython
.
test_fail_if_path_exists
(
'//SimpleCallNode//PythonCapiCallNode'
,
'//PythonCapiCallNode//SimpleCallNode'
)
def
test_non_optimised
():
"""
"""
>>> test_all()
>>> test_non_optimised()
True
"""
# Non-optimized
cdef
object
foo
=
A
assert
isinstance
(
A
(),
foo
)
assert
isinstance
(
0
,
(
int
,
long
))
assert
not
isinstance
(
u"xyz"
,
(
int
,
long
))
assert
isinstance
(
complex
(),
complex
)
# FIXME: this should be optimised, too!
return
True
@
cython
.
test_assert_path_exists
(
'//PythonCapiCallNode'
,
'//PythonCapiCallNode//SimpleCallNode'
)
@
cython
.
test_fail_if_path_exists
(
'//SimpleCallNode//SimpleCallNode'
,
'//SimpleCallNode//PythonCapiCallNode'
)
def
test_optimised
():
"""
>>> test_optimised()
True
True
"""
"""
new_type
=
type
(
'a'
,(),{})
new_type
=
type
(
'a'
,(),{})
...
@@ -14,23 +37,39 @@ def test_all():
...
@@ -14,23 +37,39 @@ def test_all():
assert
isinstance
(
int
(),
int
)
assert
isinstance
(
int
(),
int
)
assert
isinstance
(
long
(),
long
)
assert
isinstance
(
long
(),
long
)
assert
isinstance
(
float
(),
float
)
assert
isinstance
(
float
(),
float
)
assert
isinstance
(
complex
(),
complex
)
assert
isinstance
(
bytes
(),
bytes
)
assert
isinstance
(
bytes
(),
bytes
)
assert
isinstance
(
str
(),
str
)
assert
isinstance
(
str
(),
str
)
assert
isinstance
(
unicode
(),
unicode
)
assert
isinstance
(
unicode
(),
unicode
)
assert
isinstance
(
tuple
(),
tuple
)
assert
isinstance
(
tuple
(),
tuple
)
assert
isinstance
(
list
(),
list
)
assert
isinstance
(
list
(),
list
)
assert
isinstance
(
dict
(),
dict
)
assert
isinstance
(
dict
(),
dict
)
# if py_ver > (2, 3):
assert
isinstance
(
set
(),
set
)
# assert isinstance(set(), set)
assert
isinstance
(
slice
(
0
),
slice
)
assert
isinstance
(
slice
(
0
),
slice
)
assert
isinstance
(
A
,
type
)
assert
isinstance
(
A
(),
A
)
assert
not
isinstance
(
u"foo"
,
int
)
assert
not
isinstance
(
u"foo"
,
int
)
assert
isinstance
(
A
,
type
)
return
True
# Non-optimized
@
cython
.
test_assert_path_exists
(
'//PythonCapiCallNode'
)
cdef
object
foo
=
A
@
cython
.
test_fail_if_path_exists
(
'//SimpleCallNode//SimpleCallNode'
,
assert
isinstance
(
A
(),
foo
)
'//SimpleCallNode//PythonCapiCallNode'
,
assert
isinstance
(
0
,
(
int
,
long
))
'//TupleNode//NameNode'
)
assert
not
isinstance
(
u"xyz"
,
(
int
,
long
))
def
test_optimised_tuple
():
"""
>>> test_optimised_tuple()
True
"""
assert
isinstance
(
bool
(),
(
bool
,
int
,
long
,
float
,
bytes
,
str
,
unicode
,
tuple
,
list
,
dict
,
set
,
slice
))
assert
isinstance
(
int
(),
(
bool
,
int
,
long
,
float
,
bytes
,
str
,
unicode
,
tuple
,
list
,
dict
,
set
,
slice
))
assert
isinstance
(
list
(),
(
bool
,
int
,
long
,
float
,
bytes
,
str
,
unicode
,
tuple
,
list
,
dict
,
set
,
slice
))
return
True
@
cython
.
test_assert_path_exists
(
'//SimpleCallNode//SimpleCallNode'
)
@
cython
.
test_fail_if_path_exists
(
'//SimpleCallNode//PythonCapiCallNode'
,
'//PythonCapiCallNode//SimpleCallNode'
)
def
test_custom
():
"""
>>> test_custom()
True
"""
assert
isinstance
(
A
(),
A
)
return
True
return
True
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