[Python-checkins] r46371 - in python/branches/sreifschneider-newnewexcept: Lib/ctypes/test/test_structures.py Lib/test/test_codeccallbacks.py Modules/cPickle.c Objects/exceptions.c Python/errors.c Python/pythonrun.c

georg.brandl python-checkins at python.org
Fri May 26 20:00:26 CEST 2006


Author: georg.brandl
Date: Fri May 26 20:00:24 2006
New Revision: 46371

Modified:
   python/branches/sreifschneider-newnewexcept/Lib/ctypes/test/test_structures.py
   python/branches/sreifschneider-newnewexcept/Lib/test/test_codeccallbacks.py
   python/branches/sreifschneider-newnewexcept/Modules/cPickle.c
   python/branches/sreifschneider-newnewexcept/Objects/exceptions.c
   python/branches/sreifschneider-newnewexcept/Python/errors.c
   python/branches/sreifschneider-newnewexcept/Python/pythonrun.c
Log:
Fix naming issues and make the rest of the tests pass.



Modified: python/branches/sreifschneider-newnewexcept/Lib/ctypes/test/test_structures.py
==============================================================================
--- python/branches/sreifschneider-newnewexcept/Lib/ctypes/test/test_structures.py	(original)
+++ python/branches/sreifschneider-newnewexcept/Lib/ctypes/test/test_structures.py	Fri May 26 20:00:24 2006
@@ -294,20 +294,20 @@
         # In Python 2.5, Exception is a new-style class, and the repr changed
         if issubclass(Exception, object):
             self.failUnlessEqual(msg,
-                                 "(Phone) <class 'exceptions.TypeError'>: "
+                                 "(Phone) <type 'exceptions.TypeError'>: "
                                  "expected string or Unicode object, int found")
         else:
             self.failUnlessEqual(msg,
-                                 "(Phone) exceptions.TypeError: "
+                                 "(Phone) TypeError: "
                                  "expected string or Unicode object, int found")
 
         cls, msg = self.get_except(Person, "Someone", ("a", "b", "c"))
         self.failUnlessEqual(cls, RuntimeError)
         if issubclass(Exception, object):
             self.failUnlessEqual(msg,
-                                 "(Phone) <class 'exceptions.ValueError'>: too many initializers")
+                                 "(Phone) <type 'exceptions.ValueError'>: too many initializers")
         else:
-            self.failUnlessEqual(msg, "(Phone) exceptions.ValueError: too many initializers")
+            self.failUnlessEqual(msg, "(Phone) ValueError: too many initializers")
 
 
     def get_except(self, func, *args):

Modified: python/branches/sreifschneider-newnewexcept/Lib/test/test_codeccallbacks.py
==============================================================================
--- python/branches/sreifschneider-newnewexcept/Lib/test/test_codeccallbacks.py	(original)
+++ python/branches/sreifschneider-newnewexcept/Lib/test/test_codeccallbacks.py	Fri May 26 20:00:24 2006
@@ -18,30 +18,12 @@
             self.pos = len(exc.object)
         return (u"<?>", oldpos)
 
-# A UnicodeEncodeError object without a start attribute
-class NoStartUnicodeEncodeError(UnicodeEncodeError):
-    def __init__(self):
-        UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
-        del self.start
-
 # A UnicodeEncodeError object with a bad start attribute
 class BadStartUnicodeEncodeError(UnicodeEncodeError):
     def __init__(self):
         UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
         self.start = []
 
-# A UnicodeEncodeError object without an end attribute
-class NoEndUnicodeEncodeError(UnicodeEncodeError):
-    def __init__(self):
-        UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
-        del self.end
-
-# A UnicodeEncodeError object without an object attribute
-class NoObjectUnicodeEncodeError(UnicodeEncodeError):
-    def __init__(self):
-        UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
-        del self.object
-
 # A UnicodeEncodeError object with a bad object attribute
 class BadObjectUnicodeEncodeError(UnicodeEncodeError):
     def __init__(self):
@@ -478,55 +460,15 @@
            UnicodeError("ouch")
         )
         self.assertRaises(
-            AttributeError,
-            codecs.replace_errors,
-            NoStartUnicodeEncodeError()
-        )
-        self.assertRaises(
-            TypeError,
-            codecs.replace_errors,
-            BadStartUnicodeEncodeError()
-        )
-        self.assertRaises(
-            AttributeError,
-            codecs.replace_errors,
-            NoEndUnicodeEncodeError()
-        )
-        self.assertRaises(
-            AttributeError,
-            codecs.replace_errors,
-            NoObjectUnicodeEncodeError()
-        )
-        self.assertRaises(
             TypeError,
             codecs.replace_errors,
             BadObjectUnicodeEncodeError()
         )
         self.assertRaises(
-            AttributeError,
-            codecs.replace_errors,
-            NoEndUnicodeDecodeError()
-        )
-        self.assertRaises(
             TypeError,
             codecs.replace_errors,
             BadObjectUnicodeDecodeError()
         )
-        self.assertRaises(
-            AttributeError,
-            codecs.replace_errors,
-            NoStartUnicodeTranslateError()
-        )
-        self.assertRaises(
-            AttributeError,
-            codecs.replace_errors,
-            NoEndUnicodeTranslateError()
-        )
-        self.assertRaises(
-            AttributeError,
-            codecs.replace_errors,
-            NoObjectUnicodeTranslateError()
-        )
         # With the correct exception, "replace" returns an "?" or u"\ufffd" replacement
         self.assertEquals(
             codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
@@ -565,21 +507,6 @@
             codecs.xmlcharrefreplace_errors,
             UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
         )
-        self.assertRaises(
-            AttributeError,
-            codecs.xmlcharrefreplace_errors,
-            NoStartUnicodeEncodeError()
-        )
-        self.assertRaises(
-            AttributeError,
-            codecs.xmlcharrefreplace_errors,
-            NoEndUnicodeEncodeError()
-        )
-        self.assertRaises(
-            AttributeError,
-            codecs.xmlcharrefreplace_errors,
-            NoObjectUnicodeEncodeError()
-        )
         # Use the correct exception
         cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042)
         s = "".join(unichr(c) for c in cs)

Modified: python/branches/sreifschneider-newnewexcept/Modules/cPickle.c
==============================================================================
--- python/branches/sreifschneider-newnewexcept/Modules/cPickle.c	(original)
+++ python/branches/sreifschneider-newnewexcept/Modules/cPickle.c	Fri May 26 20:00:24 2006
@@ -5625,7 +5625,6 @@
 
 	if (!( t=PyDict_New()))  return -1;
 	if (!( r=PyRun_String(
-		       "def __init__(self, *args): self.args=args\n\n"
 		       "def __str__(self):\n"
 		       "  return self.args and ('%s' % self.args[0]) or '(what)'\n",
 		       Py_file_input,
@@ -5645,7 +5644,6 @@
 
 	if (!( t=PyDict_New()))  return -1;
 	if (!( r=PyRun_String(
-		       "def __init__(self, *args): self.args=args\n\n"
 		       "def __str__(self):\n"
 		       "  a=self.args\n"
 		       "  a=a and type(a[0]) or '(what)'\n"

Modified: python/branches/sreifschneider-newnewexcept/Objects/exceptions.c
==============================================================================
--- python/branches/sreifschneider-newnewexcept/Objects/exceptions.c	(original)
+++ python/branches/sreifschneider-newnewexcept/Objects/exceptions.c	Fri May 26 20:00:24 2006
@@ -4,6 +4,7 @@
 #include "osdefs.h"
 
 #define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None);
+#define EXC_MODULE_NAME "exceptions."
 
 /*
  *    BaseException
@@ -127,6 +128,8 @@
     Py_ssize_t args_len;
     PyObject *repr_suffix;
     PyObject *repr;
+    char *name;
+    char *dot;
 
     args_len = PySequence_Length(self->args);
     if (args_len < 0) {
@@ -145,7 +148,11 @@
         repr_suffix = args_repr;
     }
 
-    repr = PyString_FromString(self->ob_type->tp_name);
+    name = (char *)self->ob_type->tp_name;
+    dot = strrchr(name, '.');
+    if (dot != NULL) name = dot+1;
+
+    repr = PyString_FromString(name);
     if (!repr) {
         Py_DECREF(repr_suffix);
         return NULL;
@@ -216,8 +223,6 @@
 };
 
 static PyMemberDef BaseException_members[] = {
-    {"args", T_OBJECT, offsetof(BaseExceptionObject, args), RO,
-        PyDoc_STR("exception arguments")},
     {"message", T_OBJECT, offsetof(BaseExceptionObject, message), 0,
         PyDoc_STR("exception message")},
     {NULL}  /* Sentinel */
@@ -236,8 +241,51 @@
     return self->dict;
 }
 
+static int
+BaseException_set_dict(BaseExceptionObject *self, PyObject *val)
+{
+    if (val == NULL) {
+        PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
+        return -1;
+    }
+    if (!PyDict_Check(val)) {
+        PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
+        return -1;
+    }
+    Py_CLEAR(self->dict);
+    Py_INCREF(val);
+    self->dict = val;
+    return 0;
+}
+
+static PyObject *
+BaseException_get_args(BaseExceptionObject *self)
+{
+    if (self->args == NULL) {
+        Py_INCREF(Py_None);
+        return Py_None;
+    }
+    Py_INCREF(self->args);
+    return self->args;
+}
+
+static int
+BaseException_set_args(BaseExceptionObject *self, PyObject *val)
+{
+    PyObject *seq;
+    if (val == NULL) {
+        PyErr_SetString(PyExc_TypeError, "args may not be deleted");
+        return -1;
+    }
+    seq = PySequence_Tuple(val);
+    if (!seq) return -1;
+    self->args = seq;
+    return 0;
+}
+
 static PyGetSetDef BaseException_getset[] = {
-    {"__dict__", (getter)BaseException_get_dict, 0},
+    {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
+    {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
     {NULL},
 };
 
@@ -245,7 +293,7 @@
 static PyTypeObject _PyExc_BaseException = {
     PyObject_HEAD_INIT(NULL)
     0,                          /*ob_size*/
-    "BaseException",            /*tp_name*/
+    EXC_MODULE_NAME "BaseException", /*tp_name*/
     sizeof(BaseExceptionObject), /*tp_basicsize*/
     0,                          /*tp_itemsize*/
     (destructor)BaseException_dealloc, /*tp_dealloc*/
@@ -292,7 +340,7 @@
 static PyTypeObject _PyExc_ ## EXCNAME = { \
     PyObject_HEAD_INIT(NULL) \
     0, \
-    # EXCNAME, \
+    EXC_MODULE_NAME # EXCNAME, \
     sizeof(BaseExceptionObject), \
     0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
     0, 0, 0, 0, 0, 0, 0, \
@@ -308,7 +356,7 @@
 static PyTypeObject _PyExc_ ## EXCNAME = { \
     PyObject_HEAD_INIT(NULL) \
     0, \
-    # EXCNAME, \
+    EXC_MODULE_NAME # EXCNAME, \
     sizeof(EXCSTORE ## Object), \
     0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
     0, 0, 0, 0, 0, \
@@ -324,7 +372,7 @@
 static PyTypeObject _PyExc_ ## EXCNAME = { \
     PyObject_HEAD_INIT(NULL) \
     0, \
-    # EXCNAME, \
+    EXC_MODULE_NAME # EXCNAME, \
     sizeof(EXCSTORE ## Object), 0, \
     (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
     (reprfunc)EXCSTR, 0, 0, 0, \
@@ -438,8 +486,6 @@
 }
 
 static PyMemberDef SystemExit_members[] = {
-    {"args", T_OBJECT, offsetof(SystemExitObject, args), RO,
-        PyDoc_STR("exception arguments")},
     {"message", T_OBJECT, offsetof(SystemExitObject, message), 0,
         PyDoc_STR("exception message")},
     {"code", T_OBJECT, offsetof(SystemExitObject, code), 0,
@@ -627,8 +673,6 @@
 }
 
 static PyMemberDef EnvironmentError_members[] = {
-    {"args", T_OBJECT, offsetof(EnvironmentErrorObject, args), RO,
-        PyDoc_STR("exception arguments")},
     {"message", T_OBJECT, offsetof(EnvironmentErrorObject, message), 0,
         PyDoc_STR("exception message")},
     {"errno", T_OBJECT, offsetof(EnvironmentErrorObject, myerrno), 0,
@@ -859,8 +903,6 @@
 }
 
 static PyMemberDef WindowsError_members[] = {
-    {"args", T_OBJECT, offsetof(WindowsErrorObject, args), RO,
-        PyDoc_STR("exception arguments")},
     {"message", T_OBJECT, offsetof(WindowsErrorObject, message), 0,
         PyDoc_STR("exception message")},
     {"errno", T_OBJECT, offsetof(WindowsErrorObject, myerrno), 0,
@@ -1106,8 +1148,6 @@
 }
 
 static PyMemberDef SyntaxError_members[] = {
-    {"args", T_OBJECT, offsetof(SyntaxErrorObject, args), RO,
-        PyDoc_STR("exception arguments")},
     {"message", T_OBJECT, offsetof(SyntaxErrorObject, message), 0,
         PyDoc_STR("exception message")},
     {"msg", T_OBJECT, offsetof(SyntaxErrorObject, msg), 0,
@@ -1308,7 +1348,10 @@
 {
     if (!get_int(((UnicodeErrorObject *)exc)->start, start, "start")) {
         Py_ssize_t size;
-        size = PyUnicode_GET_SIZE(((UnicodeErrorObject *)exc)->object);
+        PyObject *obj = get_unicode(((UnicodeErrorObject *)exc)->object,
+                                    "object");
+        if (!obj) return -1;
+        size = PyUnicode_GET_SIZE(obj);
         if (*start<0)
             *start = 0; /*XXX check for values <0*/
         if (*start>=size)
@@ -1323,7 +1366,10 @@
 {
     if (!get_int(((UnicodeErrorObject *)exc)->start, start, "start")) {
         Py_ssize_t size;
-        size = PyString_GET_SIZE(((UnicodeErrorObject *)exc)->object);
+        PyObject *obj = get_string(((UnicodeErrorObject *)exc)->object,
+                                   "object");
+        if (!obj) return -1;
+        size = PyString_GET_SIZE(obj);
         if (*start<0)
             *start = 0;
         if (*start>=size)
@@ -1362,7 +1408,10 @@
 {
     if (!get_int(((UnicodeErrorObject *)exc)->end, end, "end")) {
         Py_ssize_t size;
-        size = PyUnicode_GET_SIZE(((UnicodeErrorObject *)exc)->object);
+        PyObject *obj = get_unicode(((UnicodeErrorObject *)exc)->object,
+                                    "object");
+        if (!obj) return -1;
+        size = PyUnicode_GET_SIZE(obj);
         if (*end<1)
             *end = 1;
         if (*end>size)
@@ -1377,7 +1426,10 @@
 {
     if (!get_int(((UnicodeErrorObject *)exc)->end, end, "end")) {
         Py_ssize_t size;
-        size = PyString_GET_SIZE(((UnicodeErrorObject *)exc)->object);
+        PyObject *obj = get_string(((UnicodeErrorObject *)exc)->object,
+                                   "object");
+        if (!obj) return -1;
+        size = PyString_GET_SIZE(obj);
         if (*end<1)
             *end = 1;
         if (*end>size)
@@ -1518,8 +1570,6 @@
 }
 
 static PyMemberDef UnicodeError_members[] = {
-    {"args", T_OBJECT, offsetof(UnicodeErrorObject, args), RO,
-        PyDoc_STR("exception arguments")},
     {"message", T_OBJECT, offsetof(UnicodeErrorObject, message), 0,
         PyDoc_STR("exception message")},
     {"encoding", T_OBJECT, offsetof(UnicodeErrorObject, encoding), 0,
@@ -1669,7 +1719,7 @@
 static PyTypeObject _PyExc_UnicodeDecodeError = {
     PyObject_HEAD_INIT(NULL)
     0,
-    "UnicodeDecodeError",
+    EXC_MODULE_NAME "UnicodeDecodeError",
     sizeof(UnicodeErrorObject), 0,
     (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
@@ -1782,7 +1832,7 @@
 static PyTypeObject _PyExc_UnicodeTranslateError = {
     PyObject_HEAD_INIT(NULL)
     0,
-    "UnicodeTranslateError",
+    EXC_MODULE_NAME "UnicodeTranslateError",
     sizeof(UnicodeErrorObject), 0,
     (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     (reprfunc)UnicodeTranslateError_str, 0, 0, 0,

Modified: python/branches/sreifschneider-newnewexcept/Python/errors.c
==============================================================================
--- python/branches/sreifschneider-newnewexcept/Python/errors.c	(original)
+++ python/branches/sreifschneider-newnewexcept/Python/errors.c	Fri May 26 20:00:24 2006
@@ -557,9 +557,6 @@
 		if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
 			goto failure;
 	}
-	classname = PyString_FromString(dot+1);
-	if (classname == NULL)
-		goto failure;
 	if (PyTuple_Check(base)) {
 		bases = base;
 		/* INCREF as we create a new ref in the else branch */
@@ -569,7 +566,9 @@
 		if (bases == NULL)
 			goto failure;
 	}
-	result = PyClass_New(bases, dict, classname);
+	/*result = PyClass_New(bases, dict, classname);*/
+	result = PyObject_CallFunction(&PyType_Type, "sOO",
+				       dot+1, bases, dict);
   failure:
 	Py_XDECREF(bases);
 	Py_XDECREF(mydict);
@@ -590,6 +589,9 @@
 		PyFile_WriteString("Exception ", f);
 		if (t) {
 			char* className = PyExceptionClass_Name(t);
+			char *dot = strrchr(className, '.');
+			if (dot != NULL)
+				className = dot+1;
 			PyObject* moduleName =
 			      PyObject_GetAttrString(t, "__module__");
 

Modified: python/branches/sreifschneider-newnewexcept/Python/pythonrun.c
==============================================================================
--- python/branches/sreifschneider-newnewexcept/Python/pythonrun.c	(original)
+++ python/branches/sreifschneider-newnewexcept/Python/pythonrun.c	Fri May 26 20:00:24 2006
@@ -1084,7 +1084,8 @@
 	Py_XDECREF(tb);
 }
 
-void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
+void
+PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
 {
 	int err = 0;
 	PyObject *f = PySys_GetObject("stderr");
@@ -1132,6 +1133,9 @@
 		}
 		else if (PyExceptionClass_Check(exception)) {
 			char* className = PyExceptionClass_Name(exception);
+			char *dot = strrchr(className, '.');
+			if (dot != NULL)
+				className = dot+1;
 			PyObject* moduleName =
 			      PyObject_GetAttrString(exception, "__module__");
 
@@ -1139,7 +1143,7 @@
 				err = PyFile_WriteString("<unknown>", f);
 			else {
 				char* modstr = PyString_AsString(moduleName);
-				if (modstr && strcmp(modstr, "__builtin__"))
+				if (modstr && strcmp(modstr, "exceptions"))
 				{
 					err = PyFile_WriteString(modstr, f);
 					err += PyFile_WriteString(".", f);


More information about the Python-checkins mailing list