From 5ad6f7a3a9190df03583ff9c0426a97e15a63ada Mon Sep 17 00:00:00 2001 From: Jack Jansen Date: Tue, 7 May 2002 23:00:03 +0000 Subject: More support for bridging between Python and CoreFoundation objects. Still untested. --- Mac/Modules/cf/_CFmodule.c | 38 ++++++++++++++++++++++++++ Mac/Modules/cf/cfsupport.py | 33 +++++++++++++++++++++++ Mac/Modules/cf/pycfbridge.c | 66 ++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 131 insertions(+), 6 deletions(-) diff --git a/Mac/Modules/cf/_CFmodule.c b/Mac/Modules/cf/_CFmodule.c index 373976d..9dbe825 100644 --- a/Mac/Modules/cf/_CFmodule.c +++ b/Mac/Modules/cf/_CFmodule.c @@ -31,6 +31,8 @@ #include #endif +#include "pycfbridge.h" + #ifdef USE_TOOLBOX_OBJECT_GLUE extern PyObject *_CFTypeRefObj_New(CFTypeRef); extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *); @@ -287,6 +289,14 @@ static PyObject *CFTypeRefObj_CFShow(CFTypeRefObject *_self, PyObject *_args) return _res; } +static PyObject *CFTypeRefObj_toPython(CFTypeRefObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + + return PyCF_CF2Python(_self->ob_itself); + +} + static PyMethodDef CFTypeRefObj_methods[] = { {"CFGetTypeID", (PyCFunction)CFTypeRefObj_CFGetTypeID, 1, "() -> (CFTypeID _rv)"}, @@ -304,6 +314,8 @@ static PyMethodDef CFTypeRefObj_methods[] = { "() -> (CFStringRef _rv)"}, {"CFShow", (PyCFunction)CFTypeRefObj_CFShow, 1, "() -> None"}, + {"toPython", (PyCFunction)CFTypeRefObj_toPython, 1, + "() -> (python_object)"}, {NULL, NULL, 0} }; @@ -3695,6 +3707,30 @@ static PyObject *CF_CFURLCreateFromFSRef(PyObject *_self, PyObject *_args) return _res; } +static PyObject *CF_toCF(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + + CFTypeRef rv; + CFTypeID typeid; + + if (!PyArg_ParseTuple(_args, "O&", PyCF_Python2CF, &rv)) + return NULL; + typeid = CFGetTypeID(rv); + + if (typeid == CFStringGetTypeID()) + return Py_BuildValue("O&", CFStringRefObj_New, rv); + if (typeid == CFArrayGetTypeID()) + return Py_BuildValue("O&", CFArrayRefObj_New, rv); + if (typeid == CFDictionaryGetTypeID()) + return Py_BuildValue("O&", CFDictionaryRefObj_New, rv); + if (typeid == CFURLGetTypeID()) + return Py_BuildValue("O&", CFURLRefObj_New, rv); + + return Py_BuildValue("O&", CFTypeRefObj_New, rv); + +} + static PyMethodDef CF_methods[] = { {"__CFRangeMake", (PyCFunction)CF___CFRangeMake, 1, "(CFIndex loc, CFIndex len) -> (CFRange _rv)"}, @@ -3778,6 +3814,8 @@ static PyMethodDef CF_methods[] = { "(Buffer buffer, Boolean isDirectory, CFURLRef baseURL) -> (CFURLRef _rv)"}, {"CFURLCreateFromFSRef", (PyCFunction)CF_CFURLCreateFromFSRef, 1, "(FSRef fsRef) -> (CFURLRef _rv)"}, + {"toCF", (PyCFunction)CF_toCF, 1, + "(python_object) -> (CF_object)"}, {NULL, NULL, 0} }; diff --git a/Mac/Modules/cf/cfsupport.py b/Mac/Modules/cf/cfsupport.py index fb275c3..d690441 100644 --- a/Mac/Modules/cf/cfsupport.py +++ b/Mac/Modules/cf/cfsupport.py @@ -53,6 +53,8 @@ includestuff = includestuff + """ #include #endif +#include "pycfbridge.h" + #ifdef USE_TOOLBOX_OBJECT_GLUE extern PyObject *_CFTypeRefObj_New(CFTypeRef); extern int _CFTypeRefObj_Convert(PyObject *, CFTypeRef *); @@ -485,6 +487,37 @@ f = ManualGenerator("CFStringGetUnicode", getasunicode_body); f.docstring = lambda: "() -> (unicode _rv)" CFStringRef_object.add(f) +toPython_body = """ +return PyCF_CF2Python(_self->ob_itself); +""" + +f = ManualGenerator("toPython", toPython_body); +f.docstring = lambda: "() -> (python_object)" +CFTypeRef_object.add(f) + +toCF_body = """ +CFTypeRef rv; +CFTypeID typeid; + +if (!PyArg_ParseTuple(_args, "O&", PyCF_Python2CF, &rv)) + return NULL; +typeid = CFGetTypeID(rv); + +if (typeid == CFStringGetTypeID()) + return Py_BuildValue("O&", CFStringRefObj_New, rv); +if (typeid == CFArrayGetTypeID()) + return Py_BuildValue("O&", CFArrayRefObj_New, rv); +if (typeid == CFDictionaryGetTypeID()) + return Py_BuildValue("O&", CFDictionaryRefObj_New, rv); +if (typeid == CFURLGetTypeID()) + return Py_BuildValue("O&", CFURLRefObj_New, rv); + +return Py_BuildValue("O&", CFTypeRefObj_New, rv); +""" +f = ManualGenerator("toCF", toCF_body); +f.docstring = lambda: "(python_object) -> (CF_object)" +module.add(f) + # ADD add forloop here # generate output (open the output file as late as possible) diff --git a/Mac/Modules/cf/pycfbridge.c b/Mac/Modules/cf/pycfbridge.c index 76c5ad4..9afc96c 100644 --- a/Mac/Modules/cf/pycfbridge.c +++ b/Mac/Modules/cf/pycfbridge.c @@ -26,6 +26,10 @@ PyObject * PyCF_CF2Python(CFTypeRef src) { CFTypeID typeid; + if( src == NULL ) { + Py_INCREF(Py_None); + return Py_None; + } typeid = CFGetTypeID(src); if (typeid == CFArrayGetTypeID()) return PyCF_CF2Python_sequence((CFArrayRef)src); @@ -36,13 +40,63 @@ PyCF_CF2Python(CFTypeRef src) { PyObject * PyCF_CF2Python_sequence(CFArrayRef src) { - PyErr_SetString(PyExc_SystemError, "Not yet implemented"); + int size = CFArrayGetCount(src); + PyObject *rv; + CFTypeRef item_cf; + PyObject *item_py = NULL; + int i; + + if ( (rv=PyList_New(size)) == NULL ) + return NULL; + for(i=0; i