2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 04:22:16 +00:00

- New tests

[SVN r18629]
This commit is contained in:
Bruno da Silva de Oliveira
2003-05-31 21:18:09 +00:00
parent bb55c4a855
commit d52f0c7d40
55 changed files with 927 additions and 220 deletions

View File

@@ -4,5 +4,4 @@
*.obj
*.arg
*.dll
*.cpp
.sconsign

View File

@@ -1,59 +0,0 @@
import glob
import sys
import os
# constants
if sys.platform == 'win32':
BOOST_ROOT = 'D:/Programming/Libraries/boost-cvs'
STLPORT_ROOT = 'D:/Programming/Libraries/stlport-4.5.3'
PYTHON_ROOT = 'C:/Python'
if BOOST_ROOT:
BOOST_INCLUDE = BOOST_ROOT + '/boost'
BOOST_LIB = BOOST_ROOT + '/lib'
if STLPORT_ROOT:
STLPORT_INCLUDE = STLPORT_ROOT + '/stlport'
STLPORT_LIB = STLPORT_ROOT + '/lib'
if PYTHON_ROOT:
PYTHON_INCLUDE = PYTHON_ROOT + '/include'
PYTHON_LIB = PYTHON_ROOT + '/libs'
LIBS = ['boost_python', 'python22']
INCLUDES = ['../example']
if sys.platform == 'win32':
CXX = 'icl'
CXXFLAGS='/GR /GX /MD /nologo'
INCLUDES += [BOOST_INCLUDE, STLPORT_INCLUDE, PYTHON_INCLUDE]
LIBPATH = [STLPORT_LIB, PYTHON_LIB, BOOST_LIB] + os.environ['LIB'].split(os.pathsep)
else:
CXX = 'g++'
CXXFLAGS = ''
LIBPATH = []
#INCLUDES = ['..']
# Create the environment
env = Environment(
CXX=CXX,
CXXFLAGS=CXXFLAGS,
CPPPATH=INCLUDES,
LIBS=LIBS,
LIBPATH=LIBPATH)
# Build all the cpp files
modules = [os.path.splitext(os.path.basename(x))[0] for x in glob.glob('../example/*.pyste')]
for module in modules:
multiple = ARGUMENTS.get('multiple', '')
example_cpp = '../example/%s.cpp' % module
if os.path.isfile(example_cpp):
sources = [example_cpp]
else:
sources = []
if multiple:
env.SharedLibrary(target=module, source=sources + glob.glob('_%s/*.cpp'%module))
else:
env.SharedLibrary(target=module, source=sources + ['_%s.cpp' % module])

83
pyste/tests/_smart_tr.cpp Normal file
View File

@@ -0,0 +1,83 @@
// Includes ====================================================================
#include <boost/python.hpp>
#include <smart_ptr.h>
// Using =======================================================================
using namespace boost::python;
// Declarations ================================================================
namespace {
struct smart_ptr_A_Wrapper: smart_ptr::A
{
smart_ptr_A_Wrapper(PyObject* self_, const smart_ptr::A & p0):
smart_ptr::A(p0), self(self_) {}
smart_ptr_A_Wrapper(PyObject* self_):
smart_ptr::A(), self(self_) {}
int f() {
return call_method< int >(self, "f");
}
PyObject* self;
};
}// namespace
// Module ======================================================================
BOOST_PYTHON_MODULE(_smart_tr)
{
scope* smart_ptr_A_scope = new scope(
class_< smart_ptr::A, boost::noncopyable, smart_ptr_A_Wrapper >("A", init< >())
);
// Temporary code for smart pointers
objects::class_value_wrapper<
boost::shared_ptr< smart_ptr::A >, objects::make_ptr_instance<
smart_ptr::A, objects::pointer_holder<
boost::shared_ptr< smart_ptr::A >, smart_ptr::A >
>
>();
delete smart_ptr_A_scope;
scope* smart_ptr_C_scope = new scope(
class_< smart_ptr::C >("C", init< >())
.def(init< const smart_ptr::C & >())
.def_readwrite("value", &smart_ptr::C::value)
);
// Temporary code for smart pointers
objects::class_value_wrapper<
boost::shared_ptr< smart_ptr::C >, objects::make_ptr_instance<
smart_ptr::C, objects::pointer_holder<
boost::shared_ptr< smart_ptr::C >, smart_ptr::C >
>
>();
delete smart_ptr_C_scope;
scope* smart_ptr_D_scope = new scope(
class_< smart_ptr::D >("D", init< >())
.def(init< const smart_ptr::D & >())
.def("Get", &smart_ptr::D::Get)
.def("Set", &smart_ptr::D::Set)
);
// Temporary code for smart pointers
objects::class_value_wrapper<
std::auto_ptr< smart_ptr::D >, objects::make_ptr_instance<
smart_ptr::D, objects::pointer_holder<
std::auto_ptr< smart_ptr::D >, smart_ptr::D >
>
>();
delete smart_ptr_D_scope;
def("GetA", &smart_ptr::GetA);
def("NewA", &smart_ptr::NewA);
def("NewC", &smart_ptr::NewC);
def("NewD", &smart_ptr::NewD);
}

8
pyste/tests/basic.cpp Normal file
View File

@@ -0,0 +1,8 @@
#include "basic.h"
namespace basic {
int C::static_value = 3;
const int C::const_static_value = 100;
}

59
pyste/tests/basic.h Normal file
View File

@@ -0,0 +1,59 @@
#ifndef BASIC_H
#define BASIC_H
#include <string>
namespace basic {
struct C
{
// test virtuallity
C(): value(1), const_value(0) {}
virtual int f(int x = 10)
{
return x*2;
}
int foo(int x=1){
return x+1;
}
const std::string& name() { return _name; }
void set_name(const std::string& name) { _name = name; }
std::string _name;
// test data members
static int static_value;
static const int const_static_value;
int value;
const int const_value;
// test static functions
static int mul(int x=2, int y=3) { return x*y; }
};
inline int call_f(C& c)
{
return c.f();
}
inline int call_f(C& c, int x)
{
return c.f(x);
}
inline int get_static()
{
return C::static_value;
}
inline int get_value(C& c)
{
return c.value;
}
}
#endif

5
pyste/tests/basic.pyste Normal file
View File

@@ -0,0 +1,5 @@
Class('basic::C', 'basic.h')
Function('basic::call_f', 'basic.h')
Function('basic::get_static', 'basic.h')
Function('basic::get_value', 'basic.h')

View File

@@ -1,5 +1,5 @@
import unittest
from basic import *
from _basic import *
class BasicExampleTest(unittest.TestCase):

24
pyste/tests/enums.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef ENUMS_H
#define ENUMS_H
namespace enums {
enum color { red, blue };
struct X
{
enum choices
{
good = 1,
bad = 2
};
int set(choices c)
{
return (int)c;
}
};
}
#endif

8
pyste/tests/enums.pyste Normal file
View File

@@ -0,0 +1,8 @@
color = Enum('enums::color', 'enums.h')
rename(color.red, 'Red')
rename(color.blue, 'Blue')
X = Class('enums::X', 'enums.h')
rename(X.choices.bad, 'Bad')
rename(X.choices.good, 'Good')
rename(X.choices, 'Choices')

View File

@@ -1,5 +1,5 @@
import unittest
from enums import *
from _enums import *
class EnumsTest(unittest.TestCase):

38
pyste/tests/header_test.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef HEADER_TEST_H
#define HEADER_TEST_H
#include <map>
#include <string>
namespace header_test {
enum choice { red, blue };
inline std::string choice_str(choice c)
{
std::map<choice, std::string> choice_map;
choice_map[red] = "red";
choice_map[blue] = "blue";
return choice_map[c];
}
struct C
{
choice c;
std::string get()
{
return choice_str(c);
}
};
// test the exclusion of the following
struct ForwardDeclared; // should be excluded automatically
struct A {};
void foo();
enum bar { value };
}
#endif

View File

@@ -0,0 +1,4 @@
h = AllFromHeader('header_test.h')
exclude(h.A)
exclude(h.foo)
exclude(h.bar)

View File

@@ -1,5 +1,5 @@
import unittest
from header_test import *
from _header_test import *
class HeaderTest(unittest.TestCase):

18
pyste/tests/inherit.h Normal file
View File

@@ -0,0 +1,18 @@
template<typename T>
class A
{
public:
void set(T v) { mData = v; }
T get() const { return mData; }
private:
T mData;
};
class B : public A<int>
{
public:
int go() { return get(); }
};

View File

@@ -0,0 +1,8 @@
# Doesn't work:
A = Template('A', 'inherit.h')
A_int = A('int')
Class('B', 'inherit.h')
# Does work:
#AllFromHeader('inherit.h')

View File

@@ -1,5 +1,5 @@
import unittest
from inherit import *
from _inherit import *
class InheritExampleTest(unittest.TestCase):

4
pyste/tests/nested.cpp Normal file
View File

@@ -0,0 +1,4 @@
#include "nested.h"
int nested::X::staticXValue = 10;
int nested::X::Y::staticYValue = 20;

26
pyste/tests/nested.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef NESTED_H
#define NESTED_H
namespace nested {
struct X
{
struct Y
{
int valueY;
static int staticYValue;
struct Z
{
int valueZ;
};
};
static int staticXValue;
int valueX;
};
typedef X Root;
}
#endif

1
pyste/tests/nested.pyste Normal file
View File

@@ -0,0 +1 @@
Class('nested::Root', 'nested.h')

View File

@@ -1,5 +1,5 @@
import unittest
from nested import *
from _nested import *
class NestedTest(unittest.TestCase):

View File

@@ -1,7 +0,0 @@
@echo off
call nt_build_all.bat
runtests.py
call nt_clean.bat
call nt_build_all.bat --multiple
runtests.py
call nt_clean.bat

View File

@@ -1,16 +0,0 @@
@echo off
call nt_build_pyste.bat basic %1
call nt_build_pyste.bat enums %1
call nt_build_pyste.bat header_test %1
call nt_build_pyste.bat nested %1
call nt_build_pyste.bat operators %1
call nt_build_pyste.bat smart_ptr %1
call nt_build_pyste.bat templates %1
call nt_build_pyste.bat unions %1
call nt_build_pyste.bat virtual %1
call nt_build_pyste.bat virtual2 %1
call nt_build_pyste.bat wrappertest %1
call nt_build_pyste.bat opaque %1
call nt_build_pyste.bat inherit %1
call nt_build_pyste.bat vars %1

View File

@@ -1,8 +0,0 @@
@echo off
set BOOST_INCLUDE=D:\Programming\Libraries\boost-cvs\boost
set out=_%1.cpp
if "%2" == "--multiple" set out=_%1
rem python ../src/pyste.py %2 -I%BOOST_INCLUDE -I../example --module=%1 --out=%out ../example/%1.pyste
pyste %2 -I%BOOST_INCLUDE -I../example --module=%1 --out=%out ../example/%1.pyste
scons --quiet multiple=%2 %1.dll

View File

@@ -1,24 +0,0 @@
@echo off
rm -Rf _basic
rm -Rf _enums
rm -Rf _header_test
rm -Rf _nested
rm -Rf _operators
rm -Rf _smart_ptr
rm -Rf _templates
rm -Rf _unions
rm -Rf _virtual
rm -Rf _virtual2
rm -Rf _wrappertest
rm -Rf _opaque
rm -Rf _inherit
rm -f *.cpp
rm -f *.obj
rm -f *.exp
rm -f *.arg
rm -f *.dll
rm -f *.pyc
rm -f *.lib
rm -f ../example/*.obj

47
pyste/tests/opaque.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef OPAQUE_H
#define OPAQUE_H
#include <iostream>
namespace opaque {
struct C {
C(int v): value(v) {}
int value;
};
inline C* new_C()
{
return new C(10);
}
inline int get(C* c)
{
return c->value;
}
struct D {
D(double v): value(v) {}
double value;
};
struct A
{
D* new_handle()
{
return new D(3.0);
}
double get(D* d)
{
return d->value;
}
int f(int x=0) { return x; }
};
}
#endif

5
pyste/tests/opaque.pyste Normal file
View File

@@ -0,0 +1,5 @@
foo = Function('opaque::new_C', 'opaque.h')
set_policy(foo, return_value_policy(return_opaque_pointer))
Function('opaque::get', 'opaque.h' )
A = Class('opaque::A', 'opaque.h')
set_policy(A.new_handle, return_value_policy(return_opaque_pointer))

View File

@@ -1,5 +1,5 @@
import unittest
from opaque import *
from _opaque import *
class OpaqueTest(unittest.TestCase):

View File

@@ -0,0 +1,3 @@
#include "operators.h"
double operators::C::x = 10;

49
pyste/tests/operators.h Normal file
View File

@@ -0,0 +1,49 @@
#ifndef OPERATORS_H
#define OPERATORS_H
#include <iostream>
namespace operators {
struct C
{
static double x;
double value;
const C operator+(const C other) const
{
C c;
c.value = value + other.value;
return c;
}
operator int() const
{
return (int)value;
}
double operator()()
{
return C::x;
}
double operator()(double other)
{
return C::x + other;
}
operator const char*() { return "C"; }
};
inline const C operator*(const C& lhs, const C& rhs)
{
C c;
c.value = lhs.value * rhs.value;
return c;
}
}
#endif

View File

@@ -0,0 +1,2 @@
C = Class('operators::C', 'operators.h')
#exclude(C.operator['+'])

View File

@@ -1,5 +1,5 @@
import unittest
from operators import *
from _operators import *
class OperatorTest(unittest.TestCase):

44
pyste/tests/smart_ptr.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef SMART_PTR_H
#define SMART_PTR_H
#include <memory>
#include <boost/shared_ptr.hpp>
namespace smart_ptr {
struct C
{
int value;
};
inline boost::shared_ptr<C> NewC() { return boost::shared_ptr<C>( new C() ); }
struct D
{
boost::shared_ptr<C> Get() { return ptr; }
void Set( boost::shared_ptr<C> c ) { ptr = c; }
private:
boost::shared_ptr<C> ptr;
};
inline std::auto_ptr<D> NewD() { return std::auto_ptr<D>( new D() ); }
// test an abstract class
struct A
{
virtual int f() = 0;
};
struct B: A
{
virtual int f(){ return 1; }
};
inline boost::shared_ptr<A> NewA() { return boost::shared_ptr<A>(new B()); }
inline int GetA(boost::shared_ptr<A> a) { return a->f(); }
}
#endif

View File

@@ -0,0 +1,13 @@
C = Class('smart_ptr::C', 'smart_ptr.h')
use_shared_ptr(C)
D = Class('smart_ptr::D', 'smart_ptr.h')
use_auto_ptr(D)
A = Class('smart_ptr::A', 'smart_ptr.h')
use_shared_ptr(A)
Function('smart_ptr::NewC', 'smart_ptr.h')
Function('smart_ptr::NewD', 'smart_ptr.h')
Function('smart_ptr::NewA', 'smart_ptr.h')
Function('smart_ptr::GetA', 'smart_ptr.h')

View File

@@ -1,5 +1,5 @@
import unittest
from smart_ptr import *
from _smart_ptr import *
class BasicExampleTest(unittest.TestCase):

10
pyste/tests/templates.h Normal file
View File

@@ -0,0 +1,10 @@
namespace templates {
template <class T>
struct Point
{
T x;
T y;
};
}

View File

@@ -0,0 +1,8 @@
Point = Template('templates::Point', 'templates.h')
rename(Point.x, 'i')
rename(Point.y, 'j')
IPoint = Point('int')
FPoint = Point('double', 'FPoint')
rename(IPoint, 'IPoint')
rename(IPoint.x, 'x')
rename(IPoint.y, 'y')

View File

@@ -1,5 +1,5 @@
import unittest
from templates import *
from _templates import *
class TemplatesTest(unittest.TestCase):

179
pyste/tests/test_all.py Normal file
View File

@@ -0,0 +1,179 @@
#!/usr/bin/python
import os
import glob
import shutil
import sys
# 3 functions are needed for each plataform:
# build_pyste(multiple, module)
# compile_single(module)
# compile_multiple(module)
#
if sys.platform == 'win32':
includes = '-ID:/programming/libraries/boost-cvs/boost -IC:/Python/include'
lib_dirs = '/libpath:D:/programming/libraries/boost-cvs/lib /libpath:C:/Python/libs'
libs = 'boost_python.lib python22.lib'
def build_pyste(multiple, module):
cmd = 'python ../src/pyste.py %s %s --module=%s %s.pyste'
execute(cmd % (multiple, includes, '_' + module, module))
def compile_single(module):
start_building(module)
cmd = 'icl /nologo /GR /GX -c %s -I.' % includes
cmd += ' %s'
module_obj = ''
if os.path.isfile(module+'.cpp'):
execute(cmd % (module+'.cpp'))
module_obj = module + '.obj'
execute(cmd % '_%s.cpp' % module)
execute('link /nologo /DLL /out:_%s.dll %s %s %s %s' % \
(module, lib_dirs, '_%s.obj' % module, module_obj, libs))
end_building(module)
def compile_multiple(module):
start_building(module)
cmd = 'icl /nologo /GR /GX -c %s -I.' % includes
cmd += ' %s'
module_obj = ''
if os.path.isfile(module+'.cpp'):
execute(cmd % (module+'.cpp'))
module_obj = module + '.obj'
files = glob.glob('_%s/*.cpp' % module)
for f in files:
execute(cmd % f)
objs = [os.path.split(os.path.splitext(x)[0])[1] + '.obj' for x in files]
objs.append(module_obj)
execute('link /nologo /DLL /out:_%s.dll %s %s %s' % \
(module, lib_dirs, ' '.join(objs), libs))
end_building(module)
def start_building(module):
#print 'Building module %s...' % module,
pass
def end_building(module):
pass
#if os.path.isfile('_%s.dll' % module):
# print ' done.'
#else:
# print 'FAILED!'
#print
elif sys.platform == 'posix':
def build_pyste(multiple, module):
cmd = 'python ../src/pyste.py %s --module=%s %s.pyste'
execute(cmd % (multiple, module))
def execute(cmd):
#output = os.popen(cmd).read()
#f = file('build.log', 'a')
#f.write(output)
#f.close()
os.system(cmd)
def compile_pyste_files(multiple):
pass
#if not multiple:
# ## compile each cpp into a shared library
# #for cpp in glob.glob('*.cpp'):
# # print
# # print 'compiling', cpp
# # out = os.path.splitext(cpp)[0] + '.so'
# # cmdline = 'g++ -shared -o %s -I../example ' \
# # '-I/usr/include/python2.2 -lboost_python %s' % (out, cpp)
# # os.system(cmdline)
#
#else:
# modules = get_modules()
# # list cpp files in each module directory
# print
# for module in modules:
# # compile each
# for file in glob.glob(module+'/*.cpp'):
# print 'compiling', file
# out = os.path.splitext(file)[0] + '.obj'
# cmdline = 'g++ -shared -c -o %s -I../example ' \
# '-I/usr/include/python2.2 %s' % (out, file)
# os.system(cmdline)
# # generate a dynamic library
# print 'linking'
# objs = ' '.join([x for x in glob.glob(module+'/*.obj')])
# out = module + '.so'
# cmdline = 'g++ -shared -o %s -lboost_python %s' % (out, objs)
# os.system(cmdline)
def compile_file_posix(filename, outfilename):
cmdline = 'g++ -shared -o %s -I../example ' \
'-I/usr/include/python2.2 -lboost_python %s' % (outfilename, filename)
execute(cmdline)
def run_tests():
if os.system('python runtests.py') != 0:
raise RuntimeError, 'tests failed'
def cleanup():
modules = get_modules()
extensions = '*.dll *.pyc *.obj *.exp *.lib'
files = []
for module in modules:
files.append('_' + module + '.cpp')
for ext in extensions.split():
files += glob.glob(ext)
files.append('build.log')
for file in files:
try:
os.remove(file)
except OSError: pass
for module in modules:
try:
shutil.rmtree('_' + module)
except OSError: pass
def main(multiple, module=None):
if module is None:
modules = get_modules()
else:
modules = [module]
for module in modules:
build_pyste(multiple, module)
if multiple:
compile_multiple(module)
else:
compile_single(module)
run_tests()
cleanup()
def get_modules():
def getname(file):
return os.path.splitext(os.path.basename(file))[0]
return [getname(x) for x in glob.glob('*.pyste')]
if __name__ == '__main__':
if len(sys.argv) > 1:
module = sys.argv[1]
else:
module = None
try:
main('--multiple', module)
main('', module)
except RuntimeError, e:
print e

View File

@@ -1,88 +0,0 @@
#!/usr/bin/python
import os
import glob
import shutil
def build_pyste_files(multiple):
# list all pyste files in the example directory
examples = glob.glob('../example/*.pyste')
# generate the cpp file for each example
for example in examples:
path, filename = os.path.split(example)
module = os.path.splitext(filename)[0]
os.system('python ../src/pyste.py %s -I%s --module=%s %s' % \
(multiple, path, module, example))
def compile_pyste_files(multiple):
if not multiple:
# compile each cpp into a shared library
for cpp in glob.glob('*.cpp'):
print
print 'compiling', cpp
out = os.path.splitext(cpp)[0] + '.so'
cmdline = 'g++ -shared -o %s -I../example ' \
'-I/usr/include/python2.2 -lboost_python %s' % (out, cpp)
os.system(cmdline)
else:
modules = get_modules()
# list cpp files in each module directory
print
for module in modules:
# compile each
for file in glob.glob(module+'/*.cpp'):
print 'compiling', file
out = os.path.splitext(file)[0] + '.obj'
cmdline = 'g++ -shared -c -o %s -I../example ' \
'-I/usr/include/python2.2 %s' % (out, file)
os.system(cmdline)
# generate a dynamic library
print 'linking'
objs = ' '.join([x for x in glob.glob(module+'/*.obj')])
out = module + '.so'
cmdline = 'g++ -shared -o %s -lboost_python %s' % (out, objs)
os.system(cmdline)
def run_tests():
if os.system('python runtests.py') != 0:
raise RuntimeError, 'tests failed'
def cleanup():
extensions = '*.cpp *.so *.pyc'
files = []
for ext in extensions.split():
files += glob.glob(ext)
for file in files:
try:
os.remove(file)
except OSError: pass
modules = get_modules()
for module in modules:
try:
shutil.rmtree(module)
except OSError: pass
def main(multiple):
build_pyste_files(multiple)
compile_pyste_files(multiple)
run_tests()
cleanup()
def get_modules():
def getname(file):
return os.path.splitext(os.path.basename(file))[0]
return [getname(x) for x in glob.glob('../example/*.pyste')]
if __name__ == '__main__':
try:
main('--multiple')
main('')
except RuntimeError, e:
print e

16
pyste/tests/unions.h Normal file
View File

@@ -0,0 +1,16 @@
namespace unions {
class UnionTest
{
public:
union // unions are not supported for now
{
int i;
short s1;
short s2;
} mBad;
int mGood;
};
}

2
pyste/tests/unions.pyste Normal file
View File

@@ -0,0 +1,2 @@
UnionTest = Class('unions::UnionTest', 'unions.h')
exclude(UnionTest.mBad)

19
pyste/tests/vars.h Normal file
View File

@@ -0,0 +1,19 @@
struct Color
{
Color(int r_ = 0, int g_ = 0, int b_ = 0):
r(r_), g(g_), b(b_)
{}
Color( const Color &c):
r(c.r), g(c.g), b(c.b)
{}
int r;
int g;
int b;
};
const Color black = Color(0, 0, 0);
const Color red = Color(255, 0, 0);
const Color green = Color(0, 255, 0);
const Color blue = Color(0, 0, 255);
Color in_use = black;

1
pyste/tests/vars.pyste Normal file
View File

@@ -0,0 +1 @@
AllFromHeader('vars.h')

View File

@@ -1,5 +1,5 @@
import unittest
import vars
import _vars
class VarsTest(unittest.TestCase):
@@ -9,9 +9,9 @@ class VarsTest(unittest.TestCase):
self.assertEqual(c.r, r)
self.assertEqual(c.g, g)
self.assertEqual(c.b, b)
testColor(vars.black, 0, 0, 0)
testColor(vars.red, 255, 0, 0)
testColor(vars.green, 0, 255, 0)
testColor(vars.blue, 0, 0, 255)
testColor(_vars.black, 0, 0, 0)
testColor(_vars.red, 255, 0, 0)
testColor(_vars.green, 0, 255, 0)
testColor(_vars.blue, 0, 0, 255)

70
pyste/tests/virtual.cpp Normal file
View File

@@ -0,0 +1,70 @@
// Includes ====================================================================
#include <boost/python.hpp>
#include <virtual.h>
// Using =======================================================================
using namespace boost::python;
// Declarations ================================================================
namespace {
struct virtual_C_Wrapper: virtual_::C
{
virtual_C_Wrapper(PyObject* self_, const virtual_::C & p0):
virtual_::C(p0), self(self_) {}
virtual_C_Wrapper(PyObject* self_):
virtual_::C(), self(self_) {}
int f() {
return call_method< int >(self, "f");
}
int default_f() {
return virtual_::C::f();
}
void bar(int p0) {
call_method< void >(self, "bar", p0);
}
void default_bar(int p0) {
virtual_::C::bar(p0);
}
void bar(char * p0) {
call_method< void >(self, "bar", p0);
}
void default_bar(char * p0) {
virtual_::C::bar(p0);
}
int f_abs() {
return call_method< int >(self, "f_abs");
}
PyObject* self;
};
}// namespace
// Module ======================================================================
BOOST_PYTHON_MODULE(virtual)
{
class_< virtual_::C, boost::noncopyable, virtual_C_Wrapper >("C", init< >())
.def("get_name", &virtual_::C::get_name)
.def("f", &virtual_::C::f, &virtual_C_Wrapper::default_f)
.def("bar", (void (virtual_::C::*)(int) )&virtual_::C::bar, (void (virtual_C_Wrapper::*)(int))&virtual_C_Wrapper::default_bar)
.def("bar", (void (virtual_::C::*)(char *) )&virtual_::C::bar, (void (virtual_C_Wrapper::*)(char *))&virtual_C_Wrapper::default_bar)
;
def("call_f", &virtual_::call_f);
}

28
pyste/tests/virtual.h Normal file
View File

@@ -0,0 +1,28 @@
namespace virtual_ {
struct C
{
public:
virtual int f()
{
return f_abs();
}
virtual void bar(int) {}
virtual void bar(char*) {}
const char* get_name()
{
return name();
}
protected:
virtual int f_abs() = 0;
private:
virtual const char* name() { return "C"; }
};
inline int call_f(C& c) { return c.f(); }
}

View File

@@ -0,0 +1,2 @@
Class('virtual_::C', 'virtual.h')
Function('virtual_::call_f', 'virtual.h')

27
pyste/tests/virtual2.h Normal file
View File

@@ -0,0 +1,27 @@
namespace virtual2 {
struct A
{
virtual int f() { return 0; }
virtual int f1() { return 10; }
};
struct B: A
{
virtual int f() { return 1; }
virtual int f2() { return 20; }
};
inline int call_fs(A*a)
{
int r = a->f1();
B* b = dynamic_cast<B*>(a);
return r + b->f2();
}
inline int call_f(A* a)
{
return a->f();
}
}

View File

@@ -0,0 +1,4 @@
Class('virtual2::A', 'virtual2.h')
Class('virtual2::B', 'virtual2.h')
Function('virtual2::call_fs', 'virtual2.h')
Function('virtual2::call_f', 'virtual2.h')

View File

@@ -1,5 +1,5 @@
import unittest
from virtual2 import *
from _virtual2 import *
class Virtual2Test(unittest.TestCase):

View File

@@ -1,5 +1,5 @@
import unittest
from virtual import *
from _virtual import *
class VirtualTest(unittest.TestCase):

46
pyste/tests/wrappertest.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef WRAPPER_TEST
#define WRAPPER_TEST
#include <vector>
namespace wrappertest {
inline std::vector<int> Range(int count)
{
std::vector<int> v;
v.reserve(count);
for (int i = 0; i < count; ++i){
v.push_back(i);
}
return v;
}
struct C
{
C() {}
std::vector<int> Mul(int value)
{
std::vector<int> res;
res.reserve(value);
std::vector<int>::const_iterator it;
std::vector<int> v(Range(value));
for (it = v.begin(); it != v.end(); ++it){
res.push_back(*it * value);
}
return res;
}
};
struct A
{
virtual int f() { return 1; };
};
inline int call_foo(A* a){ return a->f(); }
}
#endif

View File

@@ -0,0 +1,21 @@
Include('wrappertest_wrappers.h')
f = Function('wrappertest::Range', 'wrappertest.h')
set_wrapper(f, 'RangeWrapper')
mul = Wrapper('MulWrapper',
'''
list MulWrapper(wrappertest::C& c, int value){
return VectorToList(c.Mul(value));
}
'''
)
C = Class('wrappertest::C', 'wrappertest.h')
set_wrapper(C.Mul, mul)
A = Class('wrappertest::A', 'wrappertest.h')
set_wrapper(A.f, 'f_wrapper')
Function('wrappertest::call_foo', 'wrappertest.h')

View File

@@ -1,5 +1,5 @@
import unittest
from wrappertest import *
from _wrappertest import *
class WrapperTest(unittest.TestCase):

View File

@@ -0,0 +1,28 @@
#ifndef WRAPPER_TEST_WRAPPERS
#define WRAPPER_TEST_WRAPPERS
#include <vector>
#include <boost/python.hpp>
#include "wrappertest.h"
using namespace boost::python;
template <class T>
list VectorToList(const std::vector<T> & v)
{
list res;
typename std::vector<T>::const_iterator it;
for(it = v.begin(); it != v.end(); ++it){
res.append(*it);
}
Py_XINCREF(res.ptr());
return res;
}
inline list RangeWrapper(int count){
return VectorToList(wrappertest::Range(count));
}
inline int f_wrapper(wrappertest::A*) { return 10; }
#endif