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
8c563333
Commit
8c563333
authored
Jul 25, 2015
by
scoder
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #421 from robertwb/bool
Support operator bool() for C++ classes.
parents
3511ed4e
9f850fb4
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
59 additions
and
1 deletion
+59
-1
CHANGES.rst
CHANGES.rst
+2
-0
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+6
-0
Cython/Compiler/Parsing.py
Cython/Compiler/Parsing.py
+9
-1
tests/run/cpp_operators.pyx
tests/run/cpp_operators.pyx
+34
-0
tests/run/cpp_operators_helper.h
tests/run/cpp_operators_helper.h
+8
-0
No files found.
CHANGES.rst
View file @
8c563333
...
...
@@ -68,6 +68,8 @@ Features added
* External C++ classes that overload the assignment operator can be used.
Patch by Ian Henriksen.
* Support operator bool() for C++ classes so they can be used in if statements.
Bugs fixed
----------
...
...
Cython/Compiler/ExprNodes.py
View file @
8c563333
...
...
@@ -846,6 +846,12 @@ class ExprNode(Node):
return
self
elif
type
.
is_pyobject
or
type
.
is_int
or
type
.
is_ptr
or
type
.
is_float
:
return
CoerceToBooleanNode
(
self
,
env
)
elif
type
.
is_cpp_class
:
return
SimpleCallNode
(
self
.
pos
,
function
=
AttributeNode
(
self
.
pos
,
obj
=
self
,
attribute
=
'operator bool'
),
args
=
[]).
analyse_types
(
env
)
elif
type
.
is_ctuple
:
bool_value
=
len
(
type
.
components
)
==
0
return
BoolNode
(
self
.
pos
,
value
=
bool_value
,
...
...
Cython/Compiler/Parsing.py
View file @
8c563333
...
...
@@ -2543,7 +2543,8 @@ supported_overloaded_operators = cython.declare(set, set([
'+'
,
'-'
,
'*'
,
'/'
,
'%'
,
'++'
,
'--'
,
'~'
,
'|'
,
'&'
,
'^'
,
'<<'
,
'>>'
,
','
,
'=='
,
'!='
,
'>='
,
'>'
,
'<='
,
'<'
,
'[]'
,
'()'
,
'!'
,
'='
'[]'
,
'()'
,
'!'
,
'='
,
'bool'
,
]))
def
p_c_simple_declarator
(
s
,
ctx
,
empty
,
is_type
,
cmethod_flag
,
...
...
@@ -2620,6 +2621,13 @@ def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
s
.
error
(
"Overloading operator '%s' not yet supported."
%
op
,
fatal
=
False
)
name
+=
op
elif
op
==
'IDENT'
:
op
=
s
.
systring
;
if
op
not
in
supported_overloaded_operators
:
s
.
error
(
"Overloading operator '%s' not yet supported."
%
op
,
fatal
=
False
)
name
=
name
+
' '
+
op
s
.
next
()
result
=
Nodes
.
CNameDeclaratorNode
(
pos
,
name
=
name
,
cname
=
cname
,
default
=
rhs
)
result
.
calling_convention
=
calling_convention
...
...
tests/run/cpp_operators.pyx
View file @
8c563333
...
...
@@ -7,6 +7,7 @@ cimport cython.operator
from
cython.operator
cimport
dereference
as
deref
from
libc.string
cimport
const_char
from
libcpp
cimport
bool
cdef
out
(
s
,
result_type
=
None
):
print
'%s [%s]'
%
(
s
.
decode
(
'ascii'
),
result_type
)
...
...
@@ -49,6 +50,12 @@ cdef extern from "cpp_operators_helper.h":
const_char
*
operator
[](
int
)
const_char
*
operator
()(
int
)
cppclass
TruthClass
:
TruthClass
()
TruthClass
(
bool
)
bool
operator
bool
()
bool
value
def
test_unops
():
"""
>>> test_unops()
...
...
@@ -148,3 +155,30 @@ def test_index_call():
out
(
t
[
0
][
100
],
typeof
(
t
[
0
][
100
]))
out
(
t
[
0
](
100
),
typeof
(
t
[
0
](
100
)))
del
t
def
test_bool_op
():
"""
>>> test_bool_op()
"""
cdef
TruthClass
yes
=
TruthClass
(
True
)
cdef
TruthClass
no
=
TruthClass
(
False
)
if
yes
:
pass
else
:
assert
False
if
no
:
assert
False
def
test_bool_cond
():
"""
>>> test_bool_cond()
"""
assert
(
TruthClass
(
False
)
or
TruthClass
(
False
)).
value
==
False
assert
(
TruthClass
(
False
)
or
TruthClass
(
True
)).
value
==
True
assert
(
TruthClass
(
True
)
or
TruthClass
(
False
)).
value
==
True
assert
(
TruthClass
(
True
)
or
TruthClass
(
True
)).
value
==
True
assert
(
TruthClass
(
False
)
and
TruthClass
(
False
)).
value
==
False
assert
(
TruthClass
(
False
)
and
TruthClass
(
True
)).
value
==
False
assert
(
TruthClass
(
True
)
and
TruthClass
(
False
)).
value
==
False
assert
(
TruthClass
(
True
)
and
TruthClass
(
True
)).
value
==
True
tests/run/cpp_operators_helper.h
View file @
8c563333
...
...
@@ -45,3 +45,11 @@ public:
BIN_OP
(());
};
class
TruthClass
{
public:
TruthClass
()
:
value
(
false
)
{}
TruthClass
(
bool
value
)
:
value
(
value
)
{}
operator
bool
()
{
return
value
;
}
bool
value
;
};
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