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
cbc3c84c
Commit
cbc3c84c
authored
Aug 25, 2017
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add required copy and assignment operators for C++ classes with python object attributes.
parent
2cad794f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
2 deletions
+61
-2
Cython/Compiler/Code.py
Cython/Compiler/Code.py
+5
-2
Cython/Compiler/ModuleNode.py
Cython/Compiler/ModuleNode.py
+22
-0
tests/run/cpp_classes_def.pyx
tests/run/cpp_classes_def.pyx
+34
-0
No files found.
Cython/Compiler/Code.py
View file @
cbc3c84c
...
...
@@ -1916,9 +1916,12 @@ class CCodeWriter(object):
if
entry
.
type
.
is_pyobject
:
self
.
putln
(
"__Pyx_XGIVEREF(%s);"
%
self
.
entry_as_pyobject
(
entry
))
def
put_var_incref
(
self
,
entry
):
def
put_var_incref
(
self
,
entry
,
nanny
=
True
):
if
entry
.
type
.
is_pyobject
:
self
.
putln
(
"__Pyx_INCREF(%s);"
%
self
.
entry_as_pyobject
(
entry
))
if
nanny
:
self
.
putln
(
"__Pyx_INCREF(%s);"
%
self
.
entry_as_pyobject
(
entry
))
else
:
self
.
putln
(
"Py_INCREF(%s);"
%
self
.
entry_as_pyobject
(
entry
))
def
put_var_xincref
(
self
,
entry
):
if
entry
.
type
.
is_pyobject
:
...
...
Cython/Compiler/ModuleNode.py
View file @
cbc3c84c
...
...
@@ -951,6 +951,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code
.
put_var_xdecref
(
attr
,
nanny
=
False
);
code
.
put_release_ensured_gil
()
code
.
putln
(
"}"
)
if
py_attrs
:
# Also need copy constructor and assignment operators.
code
.
putln
(
"%s(const %s& __Pyx_other) {"
%
(
type
.
cname
,
type
.
cname
))
code
.
put_ensure_gil
()
for
attr
in
scope
.
var_entries
:
if
not
attr
.
type
.
is_cfunction
:
code
.
putln
(
"%s = __Pyx_other.%s;"
%
(
attr
.
cname
,
attr
.
cname
))
code
.
put_var_incref
(
attr
,
nanny
=
False
)
code
.
put_release_ensured_gil
()
code
.
putln
(
"}"
)
code
.
putln
(
"%s& operator=(const %s& __Pyx_other) {"
%
(
type
.
cname
,
type
.
cname
))
code
.
putln
(
"if (this != &__Pyx_other) {"
)
code
.
put_ensure_gil
()
for
attr
in
scope
.
var_entries
:
if
not
attr
.
type
.
is_cfunction
:
code
.
put_var_xdecref
(
attr
,
nanny
=
False
);
code
.
putln
(
"%s = __Pyx_other.%s;"
%
(
attr
.
cname
,
attr
.
cname
))
code
.
put_var_incref
(
attr
,
nanny
=
False
)
code
.
put_release_ensured_gil
()
code
.
putln
(
"}"
)
code
.
putln
(
"return *this;"
)
code
.
putln
(
"}"
)
code
.
putln
(
"};"
)
def
generate_enum_definition
(
self
,
entry
,
code
):
...
...
tests/run/cpp_classes_def.pyx
View file @
cbc3c84c
...
...
@@ -6,6 +6,8 @@ cdef double pi
from
math
import
pi
from
libc.math
cimport
sin
,
cos
from
libcpp
cimport
bool
from
libcpp.vector
cimport
vector
from
cython.operator
cimport
dereference
as
deref
cdef
extern
from
"shapes.h"
namespace
"shapes"
:
cdef
cppclass
Shape
:
...
...
@@ -163,6 +165,8 @@ cdef class NoisyAlloc(object):
print
"NoisyAlloc.__dealloc__"
,
self
.
name
except
:
pass
# Suppress unraisable exception warning.
def
__repr__
(
self
):
return
"NoisyAlloc[%s]"
%
self
.
name
cdef
cppclass
CppClassWithObjectMember
:
NoisyAlloc
o
...
...
@@ -188,3 +192,33 @@ def test_CppClassWithObjectMember(name):
"""
x
=
new
CppClassWithObjectMember
(
name
)
del
x
def
test_CppClassWithObjectMemberCopyAssign
(
name
):
"""
>>> test_CppClassWithObjectMemberCopyAssign("gretel")
CppClassWithObjectMember.__init__ gretel
NoisyAlloc.__init__ gretel
CppClassWithObjectMember.__dealloc__ gretel
Alive in vector NoisyAlloc[gretel]
CppClassWithObjectMember.__init__ leterg
NoisyAlloc.__init__ leterg
NoisyAlloc.__dealloc__ gretel
CppClassWithObjectMember.__dealloc__ leterg
Alive in vector NoisyAlloc[leterg]
CppClassWithObjectMember.__dealloc__ leterg
NoisyAlloc.__dealloc__ leterg
Nothing alive.
"""
x
=
new
CppClassWithObjectMember
(
name
)
cdef
vector
[
CppClassWithObjectMember
]
v
# Invokes copy constructor.
v
.
push_back
(
deref
(
x
))
del
x
print
"Alive in vector"
,
v
[
0
].
o
y
=
new
CppClassWithObjectMember
(
name
[::
-
1
])
# Invokes copy assignment.
v
[
0
]
=
deref
(
y
)
del
y
print
"Alive in vector"
,
v
[
0
].
o
v
.
clear
()
print
"Nothing alive."
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