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
b4aa6d3f
Commit
b4aa6d3f
authored
Apr 15, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
transform bool(x) into a type coercion
parent
e8526d8d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
0 deletions
+56
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+9
-0
tests/run/builtins_truth_test.pyx
tests/run/builtins_truth_test.pyx
+47
-0
No files found.
Cython/Compiler/Optimize.py
View file @
b4aa6d3f
...
...
@@ -1264,6 +1264,15 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
utility_code
=
pyobject_as_double_utility_code
,
py_name
=
"float"
)
def
_handle_simple_function_bool
(
self
,
node
,
pos_args
):
"""Transform bool(x) into a type coercion to a boolean.
"""
if
len
(
pos_args
)
!=
1
:
self
.
_error_wrong_arg_count
(
'bool'
,
node
,
pos_args
,
1
)
return
node
return
pos_args
[
0
].
coerce_to_boolean
(
self
.
current_env
()).
coerce_to_pyobject
(
self
.
current_env
())
### builtin functions
PyObject_GetAttr2_func_type
=
PyrexTypes
.
CFuncType
(
...
...
tests/run/builtins_truth_test.pyx
View file @
b4aa6d3f
def
bool_list
(
list
obj
):
"""
>>> bool_list( [] )
False
>>> bool_list( [1] )
True
>>> bool_list(None)
False
"""
return
bool
(
obj
)
def
if_list
(
list
obj
):
"""
>>> if_list( [] )
...
...
@@ -31,6 +42,18 @@ def if_list_literal(t):
else
:
return
False
def
bool_tuple
(
tuple
obj
):
"""
>>> bool_tuple( () )
False
>>> bool_tuple( (1,) )
True
>>> bool_tuple(None)
False
"""
return
bool
(
obj
)
def
if_tuple
(
tuple
obj
):
"""
>>> if_tuple( () )
...
...
@@ -63,9 +86,21 @@ def if_tuple_literal(t):
else
:
return
False
b0
=
b''
b1
=
b'abc'
def
bool_bytes
(
bytes
obj
):
"""
>>> bool_bytes(b0)
False
>>> bool_bytes(b1)
True
>>> bool_bytes(None)
False
"""
return
bool
(
obj
)
def
if_bytes
(
bytes
obj
):
"""
>>> if_bytes(b0)
...
...
@@ -98,9 +133,21 @@ def if_bytes_literal(t):
else
:
return
False
u0
=
u''
u1
=
u'abc'
def
bool_unicode
(
unicode
obj
):
"""
>>> bool_unicode(u0)
False
>>> bool_unicode(u1)
True
>>> bool_unicode(None)
False
"""
return
bool
(
obj
)
def
if_unicode
(
unicode
obj
):
"""
>>> if_unicode(u0)
...
...
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