diff options
author | Jack Jansen <jack.jansen@cwi.nl> | 2003-05-27 21:39:58 (GMT) |
---|---|---|
committer | Jack Jansen <jack.jansen@cwi.nl> | 2003-05-27 21:39:58 (GMT) |
commit | 4eb45e7804e312b19860b15dcdac904afe407c9b (patch) | |
tree | 8fe61b15da23a23d17bf894f8ab08fd4c2ee91c2 /Mac/Modules/cf | |
parent | 893801efb6add1145f620ae70d5bef51c440821f (diff) | |
download | cpython-4eb45e7804e312b19860b15dcdac904afe407c9b.zip cpython-4eb45e7804e312b19860b15dcdac904afe407c9b.tar.gz cpython-4eb45e7804e312b19860b15dcdac904afe407c9b.tar.bz2 |
Added functions CFObj_New and CFObj_Convert, general functions to convert
between CF objects and their Python representation. Fixes 734695.
Diffstat (limited to 'Mac/Modules/cf')
-rw-r--r-- | Mac/Modules/cf/_CFmodule.c | 52 | ||||
-rw-r--r-- | Mac/Modules/cf/cfsupport.py | 48 |
2 files changed, 98 insertions, 2 deletions
diff --git a/Mac/Modules/cf/_CFmodule.c b/Mac/Modules/cf/_CFmodule.c index 8473eb5..b6e434a 100644 --- a/Mac/Modules/cf/_CFmodule.c +++ b/Mac/Modules/cf/_CFmodule.c @@ -36,6 +36,11 @@ #include "pycfbridge.h" #ifdef USE_TOOLBOX_OBJECT_GLUE +extern PyObject *_CFObj_New(CFTypeRef); +extern int _CFObj_Convert(PyObject *, CFTypeRef *); +#define CFObj_New _CFObj_New +#define CFObj_Convert _CFObj_Convert + extern PyObject *_CFTypeRefObj_New(CFTypeRef); extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *); #define CFTypeRefObj_New _CFTypeRefObj_New @@ -121,7 +126,6 @@ OptionalCFURLRefObj_Convert(PyObject *v, CFURLRef *p_itself) return CFURLRefObj_Convert(v, p_itself); } - static PyObject *CF_Error; /* --------------------- Object type CFTypeRef ---------------------- */ @@ -1457,7 +1461,9 @@ int CFStringRefObj_Convert(PyObject *v, CFStringRef *p_itself) if (v == Py_None) { *p_itself = NULL; return 1; } if (PyString_Check(v)) { - char *cStr = PyString_AsString(v); + char *cStr; + if (!PyArg_Parse(v, "es", "ascii", &cStr)) + return NULL; *p_itself = CFStringCreateWithCString((CFAllocatorRef)NULL, cStr, kCFStringEncodingASCII); return 1; } @@ -4292,6 +4298,48 @@ static PyMethodDef CF_methods[] = { +/* Routines to convert any CF type to/from the corresponding CFxxxObj */ +PyObject *CFObj_New(CFTypeRef itself) +{ + if (itself == NULL) + { + PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL"); + return NULL; + } + if (CFGetTypeID(itself) == CFArrayGetTypeID()) return CFArrayRefObj_New((CFArrayRef)itself); + if (CFGetTypeID(itself) == CFDictionaryGetTypeID()) return CFDictionaryRefObj_New((CFDictionaryRef)itself); + if (CFGetTypeID(itself) == CFDataGetTypeID()) return CFDataRefObj_New((CFDataRef)itself); + if (CFGetTypeID(itself) == CFStringGetTypeID()) return CFStringRefObj_New((CFStringRef)itself); + if (CFGetTypeID(itself) == CFURLGetTypeID()) return CFURLRefObj_New((CFURLRef)itself); + /* XXXX Or should we use PyCF_CF2Python here?? */ + return CFTypeRefObj_New(itself); +} +int CFObj_Convert(PyObject *v, CFTypeRef *p_itself) +{ + + if (v == Py_None) { *p_itself = NULL; return 1; } + /* Check for other CF objects here */ + + if (!CFTypeRefObj_Check(v) && + !CFArrayRefObj_Check(v) && + !CFMutableArrayRefObj_Check(v) && + !CFDictionaryRefObj_Check(v) && + !CFMutableDictionaryRefObj_Check(v) && + !CFDataRefObj_Check(v) && + !CFMutableDataRefObj_Check(v) && + !CFStringRefObj_Check(v) && + !CFMutableStringRefObj_Check(v) && + !CFURLRefObj_Check(v) ) + { + /* XXXX Or should we use PyCF_Python2CF here?? */ + PyErr_SetString(PyExc_TypeError, "CF object required"); + return 0; + } + *p_itself = ((CFTypeRefObject *)v)->ob_itself; + return 1; +} + + void init_CF(void) { PyObject *m; diff --git a/Mac/Modules/cf/cfsupport.py b/Mac/Modules/cf/cfsupport.py index e8fb710..7e0a5d9 100644 --- a/Mac/Modules/cf/cfsupport.py +++ b/Mac/Modules/cf/cfsupport.py @@ -58,6 +58,11 @@ includestuff = includestuff + """ #include "pycfbridge.h" #ifdef USE_TOOLBOX_OBJECT_GLUE +extern PyObject *_CFObj_New(CFTypeRef); +extern int _CFObj_Convert(PyObject *, CFTypeRef *); +#define CFObj_New _CFObj_New +#define CFObj_Convert _CFObj_Convert + extern PyObject *_CFTypeRefObj_New(CFTypeRef); extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *); #define CFTypeRefObj_New _CFTypeRefObj_New @@ -142,7 +147,50 @@ OptionalCFURLRefObj_Convert(PyObject *v, CFURLRef *p_itself) } return CFURLRefObj_Convert(v, p_itself); } +""" + +finalstuff = finalstuff + """ + +/* Routines to convert any CF type to/from the corresponding CFxxxObj */ +PyObject *CFObj_New(CFTypeRef itself) +{ + if (itself == NULL) + { + PyErr_SetString(PyExc_RuntimeError, "cannot wrap NULL"); + return NULL; + } + if (CFGetTypeID(itself) == CFArrayGetTypeID()) return CFArrayRefObj_New((CFArrayRef)itself); + if (CFGetTypeID(itself) == CFDictionaryGetTypeID()) return CFDictionaryRefObj_New((CFDictionaryRef)itself); + if (CFGetTypeID(itself) == CFDataGetTypeID()) return CFDataRefObj_New((CFDataRef)itself); + if (CFGetTypeID(itself) == CFStringGetTypeID()) return CFStringRefObj_New((CFStringRef)itself); + if (CFGetTypeID(itself) == CFURLGetTypeID()) return CFURLRefObj_New((CFURLRef)itself); + /* XXXX Or should we use PyCF_CF2Python here?? */ + return CFTypeRefObj_New(itself); +} +int CFObj_Convert(PyObject *v, CFTypeRef *p_itself) +{ + if (v == Py_None) { *p_itself = NULL; return 1; } + /* Check for other CF objects here */ + + if (!CFTypeRefObj_Check(v) && + !CFArrayRefObj_Check(v) && + !CFMutableArrayRefObj_Check(v) && + !CFDictionaryRefObj_Check(v) && + !CFMutableDictionaryRefObj_Check(v) && + !CFDataRefObj_Check(v) && + !CFMutableDataRefObj_Check(v) && + !CFStringRefObj_Check(v) && + !CFMutableStringRefObj_Check(v) && + !CFURLRefObj_Check(v) ) + { + /* XXXX Or should we use PyCF_Python2CF here?? */ + PyErr_SetString(PyExc_TypeError, "CF object required"); + return 0; + } + *p_itself = ((CFTypeRefObject *)v)->ob_itself; + return 1; +} """ initstuff = initstuff + """ |