diff options
author | Jack Jansen <jack.jansen@cwi.nl> | 2002-05-12 22:04:14 (GMT) |
---|---|---|
committer | Jack Jansen <jack.jansen@cwi.nl> | 2002-05-12 22:04:14 (GMT) |
commit | 7906634f28b6c730912bcf51644ae4a8361fb028 (patch) | |
tree | 3a4f42381698aad54698edce8d9736b2590d35ed /Mac/Modules | |
parent | 69c9266f45fa56070b09cd2507fff595caa2760e (diff) | |
download | cpython-7906634f28b6c730912bcf51644ae4a8361fb028.zip cpython-7906634f28b6c730912bcf51644ae4a8361fb028.tar.gz cpython-7906634f28b6c730912bcf51644ae4a8361fb028.tar.bz2 |
- Better exception when a NULL CF object is encountered.
- Manually generate a routine with funny error semantics.
Diffstat (limited to 'Mac/Modules')
-rw-r--r-- | Mac/Modules/cf/_CFmodule.c | 144 | ||||
-rw-r--r-- | Mac/Modules/cf/cfscan.py | 1 | ||||
-rw-r--r-- | Mac/Modules/cf/cfsupport.py | 39 |
3 files changed, 168 insertions, 16 deletions
diff --git a/Mac/Modules/cf/_CFmodule.c b/Mac/Modules/cf/_CFmodule.c index 9dbe825..552511dd 100644 --- a/Mac/Modules/cf/_CFmodule.c +++ b/Mac/Modules/cf/_CFmodule.c @@ -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; diff --git a/Mac/Modules/cf/cfscan.py b/Mac/Modules/cf/cfscan.py index d6613ae..ebaeed8 100644 --- a/Mac/Modules/cf/cfscan.py +++ b/Mac/Modules/cf/cfscan.py @@ -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): diff --git a/Mac/Modules/cf/cfsupport.py b/Mac/Modules/cf/cfsupport.py index b2ff3e1..23dbbac 100644 --- a/Mac/Modules/cf/cfsupport.py +++ b/Mac/Modules/cf/cfsupport.py @@ -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)" |