Skip to content

Commit 10b249a

Browse files
author
Bruno da Silva de Oliveira
committed
- Using the new Boost.Python facility for wrapping pure virtual functions
[SVN r19792]
1 parent 5fc5fce commit 10b249a

5 files changed

Lines changed: 63 additions & 16 deletions

File tree

pyste/src/Pyste/ClassExporter.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -711,24 +711,16 @@ def DefaultImpl(method, param_names):
711711
wrapper = self.info[method.name].wrapper
712712
if not wrapper:
713713
# return the default implementation of the class
714-
if method.abstract:
715-
s = indent2 + 'PyErr_SetString(PyExc_RuntimeError, "pure virtual function called");\n' +\
716-
indent2 + 'throw_error_already_set();\n'
717-
params = ', '.join(param_names)
718-
s += indent2 + '%s%s(%s);\n' % \
719-
(return_str, method.name, params)
720-
return s
721-
else:
722-
return indent2 + '%s%s(%s);\n' % \
723-
(return_str, method.FullName(), ', '.join(param_names))
714+
return indent2 + '%s%s(%s);\n' % \
715+
(return_str, method.FullName(), ', '.join(param_names))
724716
else:
725717
if wrapper.code:
726718
self.codeunit.Write('declaration-outside', wrapper.code)
727719
# return a call for the wrapper
728720
params = ', '.join(['this'] + param_names)
729721
return indent2 + '%s%s(%s);\n' % (return_str, wrapper.FullName(), params)
730722

731-
if method.visibility != Scope.private:
723+
if not method.abstract and method.visibility != Scope.private:
732724
minArgs = method.minArgs
733725
maxArgs = method.maxArgs
734726
impl_names = self.DefaultImplementationNames(method)
@@ -759,7 +751,9 @@ def MethodDefinition(self, method):
759751
# create a list of default-impl pointers
760752
minArgs = method.minArgs
761753
maxArgs = method.maxArgs
762-
if is_method_unique:
754+
if method.abstract:
755+
default_pointers = []
756+
elif is_method_unique:
763757
default_pointers = ['&%s::%s' % (wrapper_name, x) for x in default_names]
764758
else:
765759
default_pointers = []
@@ -772,6 +766,8 @@ def MethodDefinition(self, method):
772766

773767
# get the pointer of the method
774768
pointer = method.PointerDeclaration()
769+
if method.abstract:
770+
pointer = namespaces.python + ('pure_virtual(%s)' % pointer)
775771

776772
# Add policy to overloaded methods also
777773
policy = self.info[method.name].policy or ''
@@ -781,9 +777,12 @@ def MethodDefinition(self, method):
781777
# generate the defs
782778
definitions = []
783779
# basic def
784-
definitions.append('.def("%s", %s, %s%s)' % (rename, pointer, default_pointers[-1], policy))
785-
for default_pointer in default_pointers[:-1]:
786-
definitions.append('.def("%s", %s%s)' % (rename, default_pointer, policy))
780+
if default_pointers:
781+
definitions.append('.def("%s", %s, %s%s)' % (rename, pointer, default_pointers[-1], policy))
782+
for default_pointer in default_pointers[:-1]:
783+
definitions.append('.def("%s", %s%s)' % (rename, default_pointer, policy))
784+
else:
785+
definitions.append('.def("%s", %s%s)' % (rename, pointer, policy))
787786
return definitions
788787

789788

pyste/src/Pyste/pyste.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import time
4444
import declarations
4545

46-
__version__ = '0.9.21'
46+
__version__ = '0.9.22'
4747

4848
def RecursiveIncludes(include):
4949
'Return a list containg the include dir and all its subdirectories'

pyste/tests/inherit4.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace inherit4 {
2+
3+
struct A
4+
{
5+
int x;
6+
};
7+
8+
struct B: A
9+
{
10+
int y;
11+
};
12+
13+
struct C: B
14+
{
15+
int z;
16+
};
17+
18+
}

pyste/tests/inherit4.pyste

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Class('inherit4::A', 'inherit4.h')
2+
Class('inherit4::B', 'inherit4.h')
3+
Class('inherit4::C', 'inherit4.h')

pyste/tests/inherit4UT.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
from _inherit4 import *
3+
4+
class TestInherit4(unittest.TestCase):
5+
6+
def testIt(self):
7+
self.assert_(issubclass(B, A))
8+
self.assert_(issubclass(C, A))
9+
self.assert_(issubclass(C, B))
10+
a = A()
11+
a.x = 1
12+
b = B()
13+
b.x = 10
14+
b.y = 20
15+
c = C()
16+
c.x = 100
17+
c.y = 200
18+
c.z = 300
19+
self.assertEqual(a.x, 1)
20+
self.assertEqual(b.x, 10)
21+
self.assertEqual(b.y, 20)
22+
self.assertEqual(c.x, 100)
23+
self.assertEqual(c.y, 200)
24+
self.assertEqual(c.z, 300)
25+
26+
if __name__ == '__main__':
27+
unittest.main()

0 commit comments

Comments
 (0)