]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
- Better exception when a NULL CF object is encountered.
authorJack Jansen <jack.jansen@cwi.nl>
Sun, 12 May 2002 22:04:14 +0000 (22:04 +0000)
committerJack Jansen <jack.jansen@cwi.nl>
Sun, 12 May 2002 22:04:14 +0000 (22:04 +0000)
- Manually generate a routine with funny error semantics.

Mac/Modules/cf/_CFmodule.c
Mac/Modules/cf/cfscan.py
Mac/Modules/cf/cfsupport.py

index 9dbe8256ebcfc797363895239349333516c04849..552511ddb2991b0fea0a32489e8a83b20ee119cf 100644 (file)
@@ -27,6 +27,7 @@
 #include <CFDictionary.h>
 #include <CFString.h>
 #include <CFURL.h>
+#include <CFPropertyList.h>
 #else
 #include <CoreServices/CoreServices.h>
 #endif
@@ -137,7 +138,11 @@ typedef struct CFTypeRefObject {
 PyObject *CFTypeRefObj_New(CFTypeRef itself)
 {
        CFTypeRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFTypeRefObject, &CFTypeRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
@@ -275,6 +280,35 @@ static PyObject *CFTypeRefObj_CFCopyDescription(CFTypeRefObject *_self, PyObject
        return _res;
 }
 
+static PyObject *CFTypeRefObj_CFPropertyListCreateXMLData(CFTypeRefObject *_self, PyObject *_args)
+{
+       PyObject *_res = NULL;
+       CFDataRef _rv;
+       if (!PyArg_ParseTuple(_args, ""))
+               return NULL;
+       _rv = CFPropertyListCreateXMLData((CFAllocatorRef)NULL,
+                                         _self->ob_itself);
+       _res = Py_BuildValue("O&",
+                            CFDataRefObj_New, _rv);
+       return _res;
+}
+
+static PyObject *CFTypeRefObj_CFPropertyListCreateDeepCopy(CFTypeRefObject *_self, PyObject *_args)
+{
+       PyObject *_res = NULL;
+       CFTypeRef _rv;
+       CFOptionFlags mutabilityOption;
+       if (!PyArg_ParseTuple(_args, "l",
+                             &mutabilityOption))
+               return NULL;
+       _rv = CFPropertyListCreateDeepCopy((CFAllocatorRef)NULL,
+                                          _self->ob_itself,
+                                          mutabilityOption);
+       _res = Py_BuildValue("O&",
+                            CFTypeRefObj_New, _rv);
+       return _res;
+}
+
 static PyObject *CFTypeRefObj_CFShow(CFTypeRefObject *_self, PyObject *_args)
 {
        PyObject *_res = NULL;
@@ -289,6 +323,32 @@ static PyObject *CFTypeRefObj_CFShow(CFTypeRefObject *_self, PyObject *_args)
        return _res;
 }
 
+static PyObject *CFTypeRefObj_CFPropertyListCreateFromXMLData(CFTypeRefObject *_self, PyObject *_args)
+{
+       PyObject *_res = NULL;
+
+       CFTypeRef _rv;
+       CFOptionFlags mutabilityOption;
+       CFStringRef errorString;
+       if (!PyArg_ParseTuple(_args, "l",
+                             &mutabilityOption))
+               return NULL;
+       _rv = CFPropertyListCreateFromXMLData((CFAllocatorRef)NULL,
+                                             _self->ob_itself,
+                                             mutabilityOption,
+                                             &errorString);
+       if (errorString)
+               CFRelease(errorString);
+       if (_rv == NULL) {
+               PyErr_SetString(PyExc_RuntimeError, "Parse error in XML data");
+               return NULL;
+       }
+       _res = Py_BuildValue("O&",
+                            CFTypeRefObj_New, _rv);
+       return _res;
+
+}
+
 static PyObject *CFTypeRefObj_toPython(CFTypeRefObject *_self, PyObject *_args)
 {
        PyObject *_res = NULL;
@@ -312,8 +372,14 @@ static PyMethodDef CFTypeRefObj_methods[] = {
         "() -> (CFHashCode _rv)"},
        {"CFCopyDescription", (PyCFunction)CFTypeRefObj_CFCopyDescription, 1,
         "() -> (CFStringRef _rv)"},
+       {"CFPropertyListCreateXMLData", (PyCFunction)CFTypeRefObj_CFPropertyListCreateXMLData, 1,
+        "() -> (CFDataRef _rv)"},
+       {"CFPropertyListCreateDeepCopy", (PyCFunction)CFTypeRefObj_CFPropertyListCreateDeepCopy, 1,
+        "(CFOptionFlags mutabilityOption) -> (CFTypeRef _rv)"},
        {"CFShow", (PyCFunction)CFTypeRefObj_CFShow, 1,
         "() -> None"},
+       {"CFPropertyListCreateFromXMLData", (PyCFunction)CFTypeRefObj_CFPropertyListCreateFromXMLData, 1,
+        "(CFOptionFlags mutabilityOption) -> (CFTypeRefObj)"},
        {"toPython", (PyCFunction)CFTypeRefObj_toPython, 1,
         "() -> (python_object)"},
        {NULL, NULL, 0}
@@ -386,7 +452,11 @@ typedef struct CFArrayRefObject {
 PyObject *CFArrayRefObj_New(CFArrayRef itself)
 {
        CFArrayRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFArrayRefObject, &CFArrayRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
@@ -538,7 +608,11 @@ typedef struct CFMutableArrayRefObject {
 PyObject *CFMutableArrayRefObj_New(CFMutableArrayRef itself)
 {
        CFMutableArrayRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFMutableArrayRefObject, &CFMutableArrayRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
@@ -719,7 +793,11 @@ typedef struct CFDictionaryRefObject {
 PyObject *CFDictionaryRefObj_New(CFDictionaryRef itself)
 {
        CFDictionaryRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFDictionaryRefObject, &CFDictionaryRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
@@ -853,7 +931,11 @@ typedef struct CFMutableDictionaryRefObject {
 PyObject *CFMutableDictionaryRefObj_New(CFMutableDictionaryRef itself)
 {
        CFMutableDictionaryRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFMutableDictionaryRefObject, &CFMutableDictionaryRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
@@ -971,7 +1053,11 @@ typedef struct CFDataRefObject {
 PyObject *CFDataRefObj_New(CFDataRef itself)
 {
        CFDataRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFDataRefObject, &CFDataRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
@@ -982,7 +1068,13 @@ int CFDataRefObj_Convert(PyObject *v, CFDataRef *p_itself)
 {
 
        if (v == Py_None) { *p_itself = NULL; return 1; }
-       /* Check for other CF objects here */
+       if (PyString_Check(v)) {
+           char *cStr;
+           int cLen;
+           if( PyString_AsStringAndSize(v, &cStr, &cLen) < 0 ) return 0;
+           *p_itself = CFDataCreate((CFAllocatorRef)NULL, (unsigned char *)cStr, cLen);
+           return 1;
+       }
 
        if (!CFDataRefObj_Check(v))
        {
@@ -1046,6 +1138,18 @@ static PyObject *CFDataRefObj_CFStringCreateFromExternalRepresentation(CFDataRef
        return _res;
 }
 
+static PyObject *CFDataRefObj_CFDataGetData(CFDataRefObject *_self, PyObject *_args)
+{
+       PyObject *_res = NULL;
+
+       int size = CFDataGetLength(_self->ob_itself);
+       char *data = (char *)CFDataGetBytePtr(_self->ob_itself);
+
+       _res = (PyObject *)PyString_FromStringAndSize(data, size);
+       return _res;
+
+}
+
 static PyMethodDef CFDataRefObj_methods[] = {
        {"CFDataCreateCopy", (PyCFunction)CFDataRefObj_CFDataCreateCopy, 1,
         "() -> (CFDataRef _rv)"},
@@ -1053,6 +1157,8 @@ static PyMethodDef CFDataRefObj_methods[] = {
         "() -> (CFIndex _rv)"},
        {"CFStringCreateFromExternalRepresentation", (PyCFunction)CFDataRefObj_CFStringCreateFromExternalRepresentation, 1,
         "(CFStringEncoding encoding) -> (CFStringRef _rv)"},
+       {"CFDataGetData", (PyCFunction)CFDataRefObj_CFDataGetData, 1,
+        "() -> (string _rv)"},
        {NULL, NULL, 0}
 };
 
@@ -1123,7 +1229,11 @@ typedef struct CFMutableDataRefObject {
 PyObject *CFMutableDataRefObj_New(CFMutableDataRef itself)
 {
        CFMutableDataRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFMutableDataRefObject, &CFMutableDataRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
@@ -1329,7 +1439,11 @@ typedef struct CFStringRefObject {
 PyObject *CFStringRefObj_New(CFStringRef itself)
 {
        CFStringRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFStringRefObject, &CFStringRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
@@ -2010,7 +2124,11 @@ typedef struct CFMutableStringRefObject {
 PyObject *CFMutableStringRefObj_New(CFMutableStringRef itself)
 {
        CFMutableStringRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFMutableStringRefObject, &CFMutableStringRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
@@ -2339,7 +2457,11 @@ typedef struct CFURLRefObject {
 PyObject *CFURLRefObj_New(CFURLRef itself)
 {
        CFURLRefObject *it;
-       if (itself == NULL) return PyMac_Error(resNotFound);
+       if (itself == NULL)
+       {
+               PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");
+               return NULL;
+       }
        it = PyObject_NEW(CFURLRefObject, &CFURLRef_Type);
        if (it == NULL) return NULL;
        it->ob_itself = itself;
index d6613ae6a3b4e03c0088580bac854761f097a573..ebaeed886ba73e9e12b5cfeb5ab569cb302e5cb4 100644 (file)
@@ -97,6 +97,7 @@ class MyScanner(Scanner_OSX):
                        "CFStringSetExternalCharactersNoCopy",
                        "CFStringGetCharacterAtIndex", # No format for single unichars yet.
                        "kCFStringEncodingInvalidId", # incompatible constant declaration
+                       "CFPropertyListCreateFromXMLData", # Manually generated
                        ]
 
        def makegreylist(self):
index b2ff3e1502ffe2f22e056275e7f37d2bd1f2934d..23dbbac86d86c509b9a227541d1a4aed0bb95ab0 100644 (file)
@@ -203,7 +203,11 @@ OptionalCFURLRef  = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
 
 class MyGlobalObjectDefinition(GlobalObjectDefinition):
        def outputCheckNewArg(self):
-               Output("if (itself == NULL) return PyMac_Error(resNotFound);")
+               Output('if (itself == NULL)')
+               OutLbrace()
+               Output('PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL");')
+               Output('return NULL;')
+               OutRbrace()
        def outputStructMembers(self):
                GlobalObjectDefinition.outputStructMembers(self)
                Output("void (*ob_freeit)(CFTypeRef ptr);")
@@ -501,10 +505,6 @@ f = ManualGenerator("CFStringGetUnicode", getasunicode_body);
 f.docstring = lambda: "() -> (unicode _rv)"
 CFStringRef_object.add(f)
 
-toPython_body = """
-return PyCF_CF2Python(_self->ob_itself);
-"""
-
 # Get data from CFDataRef
 getasdata_body = """
 int size = CFDataGetLength(_self->ob_itself);
@@ -518,7 +518,36 @@ f = ManualGenerator("CFDataGetData", getasdata_body);
 f.docstring = lambda: "() -> (string _rv)"
 CFDataRef_object.add(f)
 
+# Manual generator for CFPropertyListCreateFromXMLData because of funny error return
+fromxml_body = """
+CFTypeRef _rv;
+CFOptionFlags mutabilityOption;
+CFStringRef errorString;
+if (!PyArg_ParseTuple(_args, "l",
+                      &mutabilityOption))
+       return NULL;
+_rv = CFPropertyListCreateFromXMLData((CFAllocatorRef)NULL,
+                                      _self->ob_itself,
+                                      mutabilityOption,
+                                      &errorString);
+if (errorString)
+       CFRelease(errorString);
+if (_rv == NULL) {
+       PyErr_SetString(PyExc_RuntimeError, "Parse error in XML data");
+       return NULL;
+}
+_res = Py_BuildValue("O&",
+                     CFTypeRefObj_New, _rv);
+return _res;
+"""
+f = ManualGenerator("CFPropertyListCreateFromXMLData", fromxml_body)
+f.docstring = lambda: "(CFOptionFlags mutabilityOption) -> (CFTypeRefObj)"
+CFTypeRef_object.add(f)
 
+# Convert CF objects to Python objects
+toPython_body = """
+return PyCF_CF2Python(_self->ob_itself);
+"""
 
 f = ManualGenerator("toPython", toPython_body);
 f.docstring = lambda: "() -> (python_object)"