Browse Source

raise an exception if 'this' is null

David Rose 17 years ago
parent
commit
f7105a081b
2 changed files with 26 additions and 21 deletions
  1. 24 21
      dtool/src/interrogate/interfaceMakerPythonNative.cxx
  2. 2 0
      dtool/src/pystub/pystub.cxx

+ 24 - 21
dtool/src/interrogate/interfaceMakerPythonNative.cxx

@@ -644,9 +644,8 @@ void InterfaceMakerPythonNative::write_ClasseDetails(ostream &out, Object * obj)
             GetThis << "    "<<cClassName  << " * local_this = NULL;\n";
             GetThis << "    "<<cClassName  << " * local_this = NULL;\n";
             GetThis << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
             GetThis << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
             GetThis << "    if(local_this == NULL) {\n";
             GetThis << "    if(local_this == NULL) {\n";
-            GetThis << "      // This might happen if Python called a reverse operator like __rsub__().\n";
-            GetThis << "      Py_INCREF(Py_NotImplemented);\n";
-            GetThis << "      return Py_NotImplemented;\n";
+            GetThis << "       PyErr_SetString(PyExc_AttributeError, \"C++ object is not yet constructed, or already destructed.\");\n";
+            GetThis << "       return NULL;\n";
             GetThis << "    }\n";
             GetThis << "    }\n";
 
 
             write_function_for_top(out, func,GetThis.str());
             write_function_for_top(out, func,GetThis.str());
@@ -1348,7 +1347,7 @@ write_module_class(ostream &out,  Object *obj) {
     if(HasAGetKeyFunction(obj->_itype)) 
     if(HasAGetKeyFunction(obj->_itype)) 
       {
       {
         out << "//////////////////\n";
         out << "//////////////////\n";
-        out << "//  A LocalHash(getKey) Function for this type";
+        out << "//  A LocalHash(getKey) Function for this type\n";
         out << "//     " <<ClassName << "\n";
         out << "//     " <<ClassName << "\n";
         out << "//////////////////\n";
         out << "//////////////////\n";
         out << "static long  DTool_HashKey_"<<ClassName << "(PyObject * self)\n";
         out << "static long  DTool_HashKey_"<<ClassName << "(PyObject * self)\n";
@@ -1357,6 +1356,7 @@ write_module_class(ostream &out,  Object *obj) {
         out << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
         out << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
         out << "    if(local_this == NULL)\n";
         out << "    if(local_this == NULL)\n";
         out << "    {\n";
         out << "    {\n";
+        out << "       PyErr_SetString(PyExc_AttributeError, \"C++ object is not yet constructed, or already destructed.\");\n";
         out << "       return -1;\n";
         out << "       return -1;\n";
         out << "    };\n";
         out << "    };\n";
         out << "    return local_this->get_key();\n";
         out << "    return local_this->get_key();\n";
@@ -1368,7 +1368,7 @@ write_module_class(ostream &out,  Object *obj) {
         if(bases.size() == 0)
         if(bases.size() == 0)
           {
           {
             out << "//////////////////\n";
             out << "//////////////////\n";
-            out << "//  A LocalHash(This Pointer) Function for this type";
+            out << "//  A LocalHash(This Pointer) Function for this type\n";
             out << "//     " <<ClassName << "\n";
             out << "//     " <<ClassName << "\n";
             out << "//////////////////\n";
             out << "//////////////////\n";
             out << "static long  DTool_HashKey_"<<ClassName << "(PyObject * self)\n";
             out << "static long  DTool_HashKey_"<<ClassName << "(PyObject * self)\n";
@@ -1377,6 +1377,7 @@ write_module_class(ostream &out,  Object *obj) {
             out << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
             out << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
             out << "    if(local_this == NULL)\n";
             out << "    if(local_this == NULL)\n";
             out << "    {\n";
             out << "    {\n";
+            out << "       PyErr_SetString(PyExc_AttributeError, \"C++ object is not yet constructed, or already destructed.\");\n";
             out << "       return -1;\n";
             out << "       return -1;\n";
             out << "    };\n";
             out << "    };\n";
             out << "    return (long)local_this;\n";
             out << "    return (long)local_this;\n";
@@ -1396,19 +1397,20 @@ write_module_class(ostream &out,  Object *obj) {
         out << "{\n";
         out << "{\n";
         out << "    "<<cClassName  << " * local_this = NULL;\n";
         out << "    "<<cClassName  << " * local_this = NULL;\n";
         out << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
         out << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
-        out << "    if(local_this != NULL)\n";
+        out << "    if(local_this == NULL)\n";
         out << "    {\n";
         out << "    {\n";
-        out << "       ostringstream os;\n";
+        out << "       PyErr_SetString(PyExc_AttributeError, \"C++ object is not yet constructed, or already destructed.\");\n";
+        out << "       return NULL;\n";
+        out << "    };\n";
+        out << "     ostringstream os;\n";
         if (need_repr == 2) {
         if (need_repr == 2) {
-          out << "       local_this->output(os);\n";
+          out << "     local_this->output(os);\n";
         } else {
         } else {
-          out << "       local_this->python_repr(os, \""
+          out << "     local_this->python_repr(os, \""
               << classNameFromCppName(ClassName) << "\");\n";
               << classNameFromCppName(ClassName) << "\");\n";
         }
         }
-        out << "       std::string ss = os.str();\n";
-        out << "       return PyString_FromStringAndSize(ss.data(),ss.length());\n";
-        out << "    };\n";
-        out << "    return Py_BuildValue(\"\");\n";
+        out << "     std::string ss = os.str();\n";
+        out << "     return PyString_FromStringAndSize(ss.data(),ss.length());\n";
         out << "}\n";   
         out << "}\n";   
         has_local_repr = true;
         has_local_repr = true;
       }
       }
@@ -1424,17 +1426,18 @@ write_module_class(ostream &out,  Object *obj) {
         out << "{\n";
         out << "{\n";
         out << "    "<<cClassName  << " * local_this = NULL;\n";
         out << "    "<<cClassName  << " * local_this = NULL;\n";
         out << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
         out << "    DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<<  ClassName<<",(void **)&local_this);\n";
-        out << "    if(local_this != NULL)\n";
+        out << "    if(local_this == NULL)\n";
         out << "    {\n";
         out << "    {\n";
-        out << "       ostringstream os;\n";
+        out << "       PyErr_SetString(PyExc_AttributeError, \"C++ object is not yet constructed, or already destructed.\");\n";
+        out << "       return NULL;\n";
+        out << "    };\n";
+        out << "     ostringstream os;\n";
         if(need_str == 2)
         if(need_str == 2)
-          out << "       local_this->write(os,0);\n";
+          out << "     local_this->write(os,0);\n";
         else
         else
-          out << "       local_this->write(os);\n";
-        out << "       std::string ss = os.str();\n";
-        out << "       return PyString_FromStringAndSize(ss.data(),ss.length());\n";
-        out << "    };\n";
-        out << "    return Py_BuildValue(\"\");\n";
+          out << "     local_this->write(os);\n";
+        out << "     std::string ss = os.str();\n";
+        out << "     return PyString_FromStringAndSize(ss.data(),ss.length());\n";
         out << "}\n";   
         out << "}\n";   
         has_local_str = true;
         has_local_str = true;
       }
       }

+ 2 - 0
dtool/src/pystub/pystub.cxx

@@ -121,6 +121,7 @@ extern "C" {
   EXPCL_DTOOLCONFIG int _Py_RefTotal(...);
   EXPCL_DTOOLCONFIG int _Py_RefTotal(...);
 
 
   EXPCL_DTOOLCONFIG extern void *PyExc_AssertionError;
   EXPCL_DTOOLCONFIG extern void *PyExc_AssertionError;
+  EXPCL_DTOOLCONFIG extern void *PyExc_AttributeError;
   EXPCL_DTOOLCONFIG extern void *PyExc_RuntimeError;
   EXPCL_DTOOLCONFIG extern void *PyExc_RuntimeError;
   EXPCL_DTOOLCONFIG extern void *PyExc_StopIteration;
   EXPCL_DTOOLCONFIG extern void *PyExc_StopIteration;
   EXPCL_DTOOLCONFIG extern void *PyExc_TypeError;
   EXPCL_DTOOLCONFIG extern void *PyExc_TypeError;
@@ -237,6 +238,7 @@ int _Py_RefTotal(...) { return 0; };
 
 
 
 
 void *PyExc_AssertionError = (void *)NULL;
 void *PyExc_AssertionError = (void *)NULL;
+void *PyExc_AttributeError = (void *)NULL;
 void *PyExc_RuntimeError = (void *)NULL;
 void *PyExc_RuntimeError = (void *)NULL;
 void *PyExc_StopIteration = (void *)NULL;
 void *PyExc_StopIteration = (void *)NULL;
 void *PyExc_TypeError = (void *)NULL;
 void *PyExc_TypeError = (void *)NULL;