Author: Wim Lavrijsen <wlavrij...@lbl.gov>
Branch: reflex-support
Changeset: r44824:4c5ed516665c
Date: 2011-06-07 12:37 -0700
http://bitbucket.org/pypy/pypy/changeset/4c5ed516665c/

Log:    refactoring

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
@@ -25,6 +25,7 @@
 class State(object):
     def __init__(self, space):
         self.cpptype_cache = { "void" : W_CPPType(space, "void", NULL_VOIDP) }
+        self.cpptemplatetype_cache = {}
 
 def type_byname(space, name):
     state = space.fromcache(State)
@@ -45,13 +46,13 @@
         cpptype._find_data_members()
         return cpptype
 
-    raise OperationError(space.w_TypeError, space.wrap(str("no such C++ class 
%s" % name)))
+    return space.w_None
 type_byname.unwrap_spec = [ObjSpace, str]
 
 def template_byname(space, name):
     state = space.fromcache(State)
     try:
-        return state.cpptype_cache[name]
+        return state.cpptemplatetype_cache[name]
     except KeyError:
         pass
 
@@ -61,7 +62,7 @@
         state.cpptype_cache[name] = template
         return template
 
-    raise OperationError(space.w_TypeError, space.wrap(str("no such C++ 
template %s" % name)))
+    return space.w_None
 template_byname.unwrap_spec = [ObjSpace, str]
 
 
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
@@ -6,10 +6,19 @@
 # with info from multiple dictionaries and do not need to bother with meta
 # classes for inheritance. Both are python classes, though, and refactoring
 # may be in order at some point.
-class CppyyNamespace(type):
+class CppyyScopeMeta(type):
+    def __getattr__(self, attr):
+        try:
+            cppclass = get_cppitem(attr, self)
+            self.__dict__[attr] = cppclass
+            return cppclass
+        except TypeError:
+            raise AttributeError("%s object has no attribute '%s'" % (self, 
attr))
+
+class CppyyNamespaceMeta(CppyyScopeMeta):
     pass
 
-class CppyyClass(type):
+class CppyyClass(CppyyScopeMeta):
     pass
 
 
@@ -64,15 +73,6 @@
     return method
 
 
-def __innercpp_getattr__(self, attr):
-    try:
-        cppclass = get_cppitem(attr, self)
-        self.__dict__[attr] = cppclass
-        return cppclass
-    except TypeError:
-        raise AttributeError("%s object has no attribute '%s'" % (self,attr))
-
-
 def make_cppnamespace(namespace_name, cppns):
     d = {}
 
@@ -82,8 +82,7 @@
         d[func_name] = make_static_function(cppns, func_name, cppol)
 
     # create a meta class to allow properties (for static data write access)
-    metans = type(CppyyNamespace)(namespace_name+'_meta', (type(type),),
-                                  {"__getattr__" : __innercpp_getattr__})
+    metans = type(CppyyNamespaceMeta)(namespace_name+'_meta', 
(CppyyNamespaceMeta,), {})
 
     # add all data members to the dictionary of the class to be created, and
     # static ones also to the meta class (needed for property setters)
@@ -93,7 +92,7 @@
         setattr(metans, dm, cppdm)
 
     # create the python-side C++ namespace representation
-    pycppns = metans(namespace_name, (type,), d)
+    pycppns = metans(namespace_name, (object,), d)
 
     # cache result and return
     _existing_cppitems[namespace_name] = pycppns
@@ -108,8 +107,7 @@
 
     # create a meta class to allow properties (for static data write access)
     metabases = tuple([type(base) for base in bases])
-    metacpp = type(CppyyClass)(class_name+'_meta', metabases,
-                               {"__getattr__" : __innercpp_getattr__})
+    metacpp = type(CppyyClass)(class_name+'_meta', metabases, {})
 
     # create the python-side C++ class representation
     d = {"_cppyyclass" : cpptype}
@@ -138,7 +136,7 @@
     return pycpptype
 
 def make_cpptemplatetype(template_name, scope):
-    return  CppyyTemplateType(scope, template_name)
+    return CppyyTemplateType(scope, template_name)
 
 
 _existing_cppitems = {}               # to merge with gbl.__dict__ (?)
@@ -156,15 +154,17 @@
 
     # ... if lookup failed, create
     pycppitem = None
-    try:
-        cppitem = cppyy._type_byname(fullname)
+    cppitem = cppyy._type_byname(fullname)
+    if cppitem:
         if cppitem.is_namespace():
             pycppitem = make_cppnamespace(fullname, cppitem)
         else:
             pycppitem = make_cppclass(fullname, cppitem)
-    except TypeError:
+    else:
         cppitem = cppyy._template_byname(fullname)
-        pycppitem = make_cpptemplatetype(name, scope)
+        if cppitem:
+            pycppitem = make_cpptemplatetype(name, scope)
+            _existing_cppitems[fullname] = pycppitem
 
     if pycppitem:
         _existing_cppitems[fullname] = pycppitem
@@ -172,7 +172,6 @@
 
     raise AttributeError("'%s' has no attribute '%s'", (str(scope), name))
 
-
 get_cppclass = get_cppitem         # TODO: restrict to classes only (?)
 
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to