Author: Wim Lavrijsen <[email protected]>
Branch: reflex-support
Changeset: r44841:231bc6acee4f
Date: 2011-06-08 15:33 -0700
http://bitbucket.org/pypy/pypy/changeset/231bc6acee4f/
Log: advanced template support and made gbl a true namespace
diff --git a/pypy/module/cppyy/interp_cppyy.py
b/pypy/module/cppyy/interp_cppyy.py
--- a/pypy/module/cppyy/interp_cppyy.py
+++ b/pypy/module/cppyy/interp_cppyy.py
@@ -485,6 +485,7 @@
self.handle = handle
def __call__(self, args_w):
+ # TODO: this is broken but unused (see pythonify.py)
fullname = "".join([self.name, '<', self.space.str_w(args_w[0]), '>'])
return type_byname(self.space, fullname)
diff --git a/pypy/module/cppyy/pythonify.py b/pypy/module/cppyy/pythonify.py
--- a/pypy/module/cppyy/pythonify.py
+++ b/pypy/module/cppyy/pythonify.py
@@ -27,8 +27,18 @@
self._scope = scope
self._name = name
+ def _arg_to_str(self, arg):
+ if type(arg) != str:
+ arg = arg.__name__
+ return arg
+
def __call__(self, *args):
- fullname = "".join([self._name, '<', str(args[0]), '>'])
+ fullname = ''.join(
+ [self._name, '<', ','.join(map(self._arg_to_str, args))])
+ if fullname[-1] == '>':
+ fullname += ' >'
+ else:
+ fullname += '>'
return getattr(self._scope, fullname)
class CppyyObject(object):
@@ -141,7 +151,7 @@
_existing_cppitems = {} # to merge with gbl.__dict__ (?)
def get_cppitem(name, scope=None):
- if scope:
+ if scope and not scope is gbl:
fullname = scope.__name__+"::"+name
else:
fullname = name
@@ -175,18 +185,6 @@
get_cppclass = get_cppitem # TODO: restrict to classes only (?)
-class _gbl(object): # TODO: make a CppyyNamespace object
- """Global C++ namespace, i.e. ::."""
-
- def __getattr__(self, attr):
- try:
- cppitem = get_cppitem(attr)
- self.__dict__[attr] = cppitem
- return cppitem
- except TypeError:
- raise AttributeError("'gbl' object has no attribute '%s'" % attr)
-
-
_loaded_shared_libs = {}
def load_lib(name):
try:
@@ -198,4 +196,4 @@
# user interface objects
-gbl = _gbl()
+gbl = make_cppnamespace("::", cppyy._type_byname("")) # global C++ namespace
diff --git a/pypy/module/cppyy/test/Makefile b/pypy/module/cppyy/test/Makefile
--- a/pypy/module/cppyy/test/Makefile
+++ b/pypy/module/cppyy/test/Makefile
@@ -26,8 +26,8 @@
$(genreflex) datatypes.h $(genreflexflags)
g++ -o $@ datatypes_rflx.cpp datatypes.cxx -shared -lReflex $(cppflags)
$(cppflags2)
-advancedcppDict.so: advancedcpp.cxx advancedcpp.h
- $(genreflex) advancedcpp.h $(genreflexflags)
+advancedcppDict.so: advancedcpp.cxx advancedcpp.h advancedcpp.xml
+ $(genreflex) advancedcpp.h $(genreflexflags) --selection=advancedcpp.xml
g++ -o $@ advancedcpp_rflx.cpp advancedcpp.cxx -shared -lReflex
$(cppflags) $(cppflags2)
stltypesDict.so: stltypes.cxx stltypes.h stltypes.xml
diff --git a/pypy/module/cppyy/test/advancedcpp.h
b/pypy/module/cppyy/test/advancedcpp.h
--- a/pypy/module/cppyy/test/advancedcpp.h
+++ b/pypy/module/cppyy/test/advancedcpp.h
@@ -120,28 +120,62 @@
//===========================================================================
-template< typename T > // for template testing
+template<typename T> // for template testing
class T1 {
public:
- T1( T t = T(0) ) : m_t1( t ) {}
+ T1(T t = T(1)) : m_t1(t) {}
T value() { return m_t1; }
public:
T m_t1;
};
-template< typename T >
+template<typename T>
class T2 {
public:
+ T2(T t = T(2)) : m_t2(t) {}
+ T value() { return m_t2; }
+
+public:
T m_t2;
};
-namespace {
- T1< int > tt1;
- T2< T1< int > > tt2;
-}
+template<typename T, typename U>
+class T3 {
+public:
+ T3(T t = T(3), U u = U(33)) : m_t3(t), m_u3(u) {}
+ T value_t() { return m_t3; }
+ U value_u() { return m_u3; }
-// helpers for checking pass-by-ref
+public:
+ T m_t3;
+ U m_u3;
+};
+
+namespace a_ns {
+
+ template<typename T>
+ class T4 {
+ public:
+ T4(T t = T(4)) : m_t4(t) {}
+ T value() { return m_t4; }
+
+ public:
+ T m_t4;
+ };
+
+} // namespace a_ns
+
+template class T1<int>;
+template class T2<T1<int> >;
+template class T3<int, double>;
+template class T3<T1<int>, T2<T1<int> > >;
+template class a_ns::T4<int>;
+template class a_ns::T4<a_ns::T4<T3<int, double> > >;
+
+
+//===========================================================================
+// for checking pass-by-reference of builtin types
void set_int_through_ref(int& i, int val);
int pass_int_through_const_ref(const int& i);
void set_long_through_ref(long& l, long val);
diff --git a/pypy/module/cppyy/test/test_advancedcpp.py
b/pypy/module/cppyy/test/test_advancedcpp.py
--- a/pypy/module/cppyy/test/test_advancedcpp.py
+++ b/pypy/module/cppyy/test/test_advancedcpp.py
@@ -54,22 +54,60 @@
"""Test access to namespaces and inner classes"""
import cppyy
+ gbl = cppyy.gbl
-# TODO: have Reflex add the globals to the dictionary ...
-# assert cppyy.gbl.a_ns.g_a == 11
- assert cppyy.gbl.a_ns.b_class.s_b == 22
- assert cppyy.gbl.a_ns.b_class().m_b == -2
- assert cppyy.gbl.a_ns.b_class.c_class.s_c == 33
- assert cppyy.gbl.a_ns.b_class.c_class().m_c == -3
-# assert cppyy.gbl.a_ns.d_ns.g_d == 44
- assert cppyy.gbl.a_ns.d_ns.e_class.s_e == 55
- assert cppyy.gbl.a_ns.d_ns.e_class().m_e == -5
- assert cppyy.gbl.a_ns.d_ns.e_class.f_class.s_f == 66
- assert cppyy.gbl.a_ns.d_ns.e_class.f_class().m_f == -6
+ assert gbl.a_ns.g_a == 11
+ assert gbl.a_ns.b_class.s_b == 22
+ assert gbl.a_ns.b_class().m_b == -2
+ assert gbl.a_ns.b_class.c_class.s_c == 33
+ assert gbl.a_ns.b_class.c_class().m_c == -3
+ assert gbl.a_ns.d_ns.g_d == 44
+ assert gbl.a_ns.d_ns.e_class.s_e == 55
+ assert gbl.a_ns.d_ns.e_class().m_e == -5
+ assert gbl.a_ns.d_ns.e_class.f_class.s_f == 66
+ assert gbl.a_ns.d_ns.e_class.f_class().m_f == -6
- assert cppyy.gbl.a_ns is cppyy.gbl.a_ns
- assert cppyy.gbl.a_ns.d_ns is cppyy.gbl.a_ns.d_ns
+ assert gbl.a_ns is gbl.a_ns
+ assert gbl.a_ns.d_ns is gbl.a_ns.d_ns
- assert cppyy.gbl.a_ns.b_class is cppyy.gbl.a_ns.b_class
- assert cppyy.gbl.a_ns.d_ns.e_class is
cppyy.gbl.a_ns.d_ns.e_class
- assert cppyy.gbl.a_ns.d_ns.e_class.f_class is
cppyy.gbl.a_ns.d_ns.e_class.f_class
+ assert gbl.a_ns.b_class is gbl.a_ns.b_class
+ assert gbl.a_ns.d_ns.e_class is gbl.a_ns.d_ns.e_class
+ assert gbl.a_ns.d_ns.e_class.f_class is gbl.a_ns.d_ns.e_class.f_class
+
+ def test03_template_types(self):
+ """Test bindings of templated types"""
+
+ import cppyy
+ gbl = cppyy.gbl
+
+ assert gbl.T1 is gbl.T1
+ assert gbl.T2 is gbl.T2
+ assert gbl.T3 is gbl.T3
+ assert not gbl.T1 is gbl.T2
+ assert not gbl.T2 is gbl.T3
+
+ assert gbl.T1('int') is gbl.T1('int')
+ assert gbl.T1(int) is gbl.T1('int')
+ assert gbl.T2('T1<int>') is gbl.T2('T1<int>')
+ assert gbl.T2(gbl.T1('int')) is gbl.T2('T1<int>')
+ assert gbl.T3('int,double') is gbl.T3('int,double')
+ assert gbl.T3('int', 'double') is gbl.T3('int,double')
+ assert gbl.T3(int, 'double') is gbl.T3('int,double')
+ assert gbl.T3('T1<int>,T2<T1<int> >') is gbl.T3('T1<int>,T2<T1<int> >')
+ assert gbl.T3('T1<int>', gbl.T2(gbl.T1(int))) is
gbl.T3('T1<int>,T2<T1<int> >')
+
+ assert gbl.a_ns.T4(int) is gbl.a_ns.T4('int')
+ assert gbl.a_ns.T4('a_ns::T4<T3<int,double> >')\
+ is gbl.a_ns.T4(gbl.a_ns.T4(gbl.T3(int, 'double')))
+
+ t1 = gbl.T1(int)()
+ assert t1.m_t1 == 1
+ assert t1.value() == 1
+ t1.destruct()
+
+ t1 = gbl.T1(int)(11)
+ assert t1.m_t1 == 11
+ assert t1.value() == 11
+ t1.m_t1 = 111
+ assert t1.value() == 111
+ t1.destruct()
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit