2002-05-05 18:48:12 -03:00
|
|
|
/*
|
|
|
|
** Convert objects from Python to CoreFoundation and vice-versa.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <CoreServices/CoreServices.h>
|
|
|
|
|
|
|
|
#include "Python.h"
|
2003-11-20 09:31:00 -04:00
|
|
|
#include "pymactoolbox.h"
|
2002-05-05 18:48:12 -03:00
|
|
|
#include "pycfbridge.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------- */
|
|
|
|
/* CoreFoundation objects to Python objects */
|
|
|
|
/* ---------------------------------------- */
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyCF_CF2Python(CFTypeRef src) {
|
|
|
|
CFTypeID typeid;
|
|
|
|
|
2002-05-07 20:00:03 -03:00
|
|
|
if( src == NULL ) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
2002-05-05 18:48:12 -03:00
|
|
|
typeid = CFGetTypeID(src);
|
|
|
|
if (typeid == CFArrayGetTypeID())
|
|
|
|
return PyCF_CF2Python_sequence((CFArrayRef)src);
|
|
|
|
if (typeid == CFDictionaryGetTypeID())
|
|
|
|
return PyCF_CF2Python_mapping((CFDictionaryRef)src);
|
|
|
|
return PyCF_CF2Python_simple(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyCF_CF2Python_sequence(CFArrayRef src) {
|
2002-05-07 20:00:03 -03:00
|
|
|
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<size; i++) {
|
|
|
|
item_cf = CFArrayGetValueAtIndex(src, i);
|
|
|
|
if (item_cf == NULL ) goto err;
|
|
|
|
item_py = PyCF_CF2Python(item_cf);
|
|
|
|
if (item_py == NULL ) goto err;
|
2002-05-08 12:29:33 -03:00
|
|
|
if (PyList_SetItem(rv, i, item_py) < 0) goto err;
|
2002-05-07 20:00:03 -03:00
|
|
|
item_py = NULL;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
err:
|
|
|
|
Py_XDECREF(item_py);
|
|
|
|
Py_DECREF(rv);
|
2002-05-05 18:48:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyCF_CF2Python_mapping(CFTypeRef src) {
|
2002-05-07 20:00:03 -03:00
|
|
|
int size = CFDictionaryGetCount(src);
|
2002-05-08 19:13:51 -03:00
|
|
|
PyObject *rv = NULL;
|
|
|
|
CFTypeRef *allkeys = NULL, *allvalues = NULL;
|
2002-05-07 20:00:03 -03:00
|
|
|
CFTypeRef key_cf, value_cf;
|
|
|
|
PyObject *key_py = NULL, *value_py = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
allkeys = malloc(size*sizeof(CFTypeRef *));
|
2002-05-08 19:13:51 -03:00
|
|
|
if (allkeys == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto err;
|
|
|
|
}
|
2002-05-07 20:00:03 -03:00
|
|
|
allvalues = malloc(size*sizeof(CFTypeRef *));
|
2002-05-08 19:13:51 -03:00
|
|
|
if (allvalues == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ( (rv=PyDict_New()) == NULL ) goto err;
|
2002-05-07 20:00:03 -03:00
|
|
|
CFDictionaryGetKeysAndValues(src, allkeys, allvalues);
|
|
|
|
for(i=0; i<size; i++) {
|
|
|
|
key_cf = allkeys[i];
|
|
|
|
value_cf = allvalues[i];
|
|
|
|
key_py = PyCF_CF2Python(key_cf);
|
|
|
|
if (key_py == NULL ) goto err;
|
2002-05-08 19:13:51 -03:00
|
|
|
value_py = PyCF_CF2Python(value_cf);
|
2002-05-07 20:00:03 -03:00
|
|
|
if (value_py == NULL ) goto err;
|
2002-05-08 12:29:33 -03:00
|
|
|
if (PyDict_SetItem(rv, key_py, value_py) < 0) goto err;
|
2002-05-07 20:00:03 -03:00
|
|
|
key_py = NULL;
|
|
|
|
value_py = NULL;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
err:
|
|
|
|
Py_XDECREF(key_py);
|
|
|
|
Py_XDECREF(value_py);
|
2002-05-08 19:13:51 -03:00
|
|
|
Py_XDECREF(rv);
|
2002-05-07 20:00:03 -03:00
|
|
|
free(allkeys);
|
|
|
|
free(allvalues);
|
2002-05-05 18:48:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyCF_CF2Python_simple(CFTypeRef src) {
|
|
|
|
CFTypeID typeid;
|
|
|
|
|
|
|
|
typeid = CFGetTypeID(src);
|
|
|
|
if (typeid == CFStringGetTypeID())
|
|
|
|
return PyCF_CF2Python_string((CFStringRef)src);
|
2002-05-08 19:13:51 -03:00
|
|
|
if (typeid == CFBooleanGetTypeID())
|
|
|
|
return PyBool_FromLong((long)CFBooleanGetValue(src));
|
|
|
|
if (typeid == CFNumberGetTypeID()) {
|
|
|
|
if (CFNumberIsFloatType(src)) {
|
|
|
|
double d;
|
|
|
|
CFNumberGetValue(src, kCFNumberDoubleType, &d);
|
|
|
|
return PyFloat_FromDouble(d);
|
|
|
|
} else {
|
|
|
|
long l;
|
|
|
|
if (!CFNumberGetValue(src, kCFNumberLongType, &l))
|
|
|
|
/* XXXX Out of range! */;
|
|
|
|
return PyInt_FromLong(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* XXXX Should return as CFTypeRef, really... */
|
2002-05-05 18:48:12 -03:00
|
|
|
PyMac_Error(resNotFound);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unsure - Return unicode or 8 bit strings? */
|
|
|
|
PyObject *
|
|
|
|
PyCF_CF2Python_string(CFStringRef src) {
|
|
|
|
int size = CFStringGetLength(src)+1;
|
|
|
|
Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE));
|
|
|
|
CFRange range;
|
|
|
|
PyObject *rv;
|
|
|
|
|
|
|
|
range.location = 0;
|
|
|
|
range.length = size;
|
|
|
|
if( data == NULL ) return PyErr_NoMemory();
|
|
|
|
CFStringGetCharacters(src, range, data);
|
2002-05-08 19:13:51 -03:00
|
|
|
rv = (PyObject *)PyUnicode_FromUnicode(data, size-1);
|
2002-05-05 18:48:12 -03:00
|
|
|
free(data);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------- */
|
|
|
|
/* Python objects to CoreFoundation objects */
|
|
|
|
/* ---------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
PyCF_Python2CF(PyObject *src, CFTypeRef *dst) {
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
if (PyString_Check(src) || PyUnicode_Check(src))
|
2002-05-08 12:29:33 -03:00
|
|
|
return PyCF_Python2CF_simple(src, dst);
|
2002-05-05 18:48:12 -03:00
|
|
|
if (PySequence_Check(src))
|
|
|
|
return PyCF_Python2CF_sequence(src, (CFArrayRef *)dst);
|
|
|
|
if (PyMapping_Check(src))
|
|
|
|
return PyCF_Python2CF_mapping(src, (CFDictionaryRef *)dst);
|
|
|
|
return PyCF_Python2CF_simple(src, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyCF_Python2CF_sequence(PyObject *src, CFArrayRef *dst) {
|
2002-05-07 20:00:03 -03:00
|
|
|
CFMutableArrayRef rv = NULL;
|
2002-05-05 18:48:12 -03:00
|
|
|
CFTypeRef item_cf = NULL;
|
|
|
|
PyObject *item_py = NULL;
|
|
|
|
int size, i;
|
|
|
|
|
2002-05-13 18:23:10 -03:00
|
|
|
if( !PySequence_Check(src) ) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"Cannot convert %.500s objects to CFArray",
|
|
|
|
src->ob_type->tp_name);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-05-05 18:48:12 -03:00
|
|
|
size = PySequence_Size(src);
|
|
|
|
rv = CFArrayCreateMutable((CFAllocatorRef)NULL, size, &kCFTypeArrayCallBacks);
|
|
|
|
if (rv == NULL) {
|
|
|
|
PyMac_Error(resNotFound);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i=0; i<size; i++) {
|
|
|
|
item_py = PySequence_GetItem(src, i);
|
|
|
|
if (item_py == NULL) goto err;
|
|
|
|
if ( !PyCF_Python2CF(item_py, &item_cf)) goto err;
|
|
|
|
Py_DECREF(item_py);
|
|
|
|
CFArraySetValueAtIndex(rv, i, item_cf);
|
|
|
|
CFRelease(item_cf);
|
|
|
|
item_cf = NULL;
|
|
|
|
}
|
|
|
|
*dst = rv;
|
|
|
|
return 1;
|
|
|
|
err:
|
|
|
|
Py_XDECREF(item_py);
|
|
|
|
if (rv) CFRelease(rv);
|
|
|
|
if (item_cf) CFRelease(item_cf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyCF_Python2CF_mapping(PyObject *src, CFDictionaryRef *dst) {
|
2002-05-07 20:00:03 -03:00
|
|
|
CFMutableDictionaryRef rv = NULL;
|
2002-05-05 18:48:12 -03:00
|
|
|
PyObject *aslist = NULL;
|
|
|
|
CFTypeRef key_cf = NULL, value_cf = NULL;
|
|
|
|
PyObject *item_py = NULL, *key_py = NULL, *value_py = NULL;
|
|
|
|
int size, i;
|
|
|
|
|
2002-05-13 18:23:10 -03:00
|
|
|
if( !PyMapping_Check(src) ) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"Cannot convert %.500s objects to CFDictionary",
|
|
|
|
src->ob_type->tp_name);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-05-05 18:48:12 -03:00
|
|
|
size = PyMapping_Size(src);
|
|
|
|
rv = CFDictionaryCreateMutable((CFAllocatorRef)NULL, size,
|
2002-05-07 20:00:03 -03:00
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
2002-05-05 18:48:12 -03:00
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
if (rv == NULL) {
|
|
|
|
PyMac_Error(resNotFound);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ( (aslist = PyMapping_Items(src)) == NULL ) goto err;
|
|
|
|
|
|
|
|
for( i=0; i<size; i++) {
|
|
|
|
item_py = PySequence_GetItem(aslist, i);
|
|
|
|
if (item_py == NULL) goto err;
|
2002-05-08 19:13:51 -03:00
|
|
|
if (!PyArg_ParseTuple(item_py, "OO", &key_py, &value_py)) goto err;
|
2002-05-05 18:48:12 -03:00
|
|
|
if ( !PyCF_Python2CF(key_py, &key_cf) ) goto err;
|
2002-05-07 20:00:03 -03:00
|
|
|
if ( !PyCF_Python2CF(value_py, &value_cf) ) goto err;
|
2002-05-05 18:48:12 -03:00
|
|
|
CFDictionaryAddValue(rv, key_cf, value_cf);
|
|
|
|
CFRelease(key_cf);
|
|
|
|
key_cf = NULL;
|
|
|
|
CFRelease(value_cf);
|
|
|
|
value_cf = NULL;
|
|
|
|
}
|
|
|
|
*dst = rv;
|
|
|
|
return 1;
|
|
|
|
err:
|
|
|
|
Py_XDECREF(item_py);
|
|
|
|
Py_XDECREF(aslist);
|
|
|
|
if (rv) CFRelease(rv);
|
|
|
|
if (key_cf) CFRelease(key_cf);
|
|
|
|
if (value_cf) CFRelease(value_cf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyCF_Python2CF_simple(PyObject *src, CFTypeRef *dst) {
|
|
|
|
|
2002-05-13 18:23:10 -03:00
|
|
|
#if 0
|
2002-05-05 18:48:12 -03:00
|
|
|
if (PyObject_HasAttrString(src, "CFType")) {
|
|
|
|
*dst = PyObject_CallMethod(src, "CFType", "");
|
|
|
|
return (*dst != NULL);
|
|
|
|
}
|
2002-05-13 18:23:10 -03:00
|
|
|
#endif
|
2008-06-09 01:58:54 -03:00
|
|
|
if (PyString_Check(src) || PyUnicode_Check(src))
|
2002-05-05 18:48:12 -03:00
|
|
|
return PyCF_Python2CF_string(src, (CFStringRef *)dst);
|
2002-05-08 19:13:51 -03:00
|
|
|
if (PyBool_Check(src)) {
|
|
|
|
if (src == Py_True)
|
|
|
|
*dst = kCFBooleanTrue;
|
|
|
|
else
|
|
|
|
*dst = kCFBooleanFalse;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (PyInt_Check(src)) {
|
|
|
|
long v = PyInt_AsLong(src);
|
|
|
|
*dst = CFNumberCreate(NULL, kCFNumberLongType, &v);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (PyFloat_Check(src)) {
|
|
|
|
double d = PyFloat_AsDouble(src);
|
|
|
|
*dst = CFNumberCreate(NULL, kCFNumberDoubleType, &d);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-05-05 18:48:12 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2002-05-13 18:23:10 -03:00
|
|
|
"Cannot convert %.500s objects to CFType",
|
2002-05-05 18:48:12 -03:00
|
|
|
src->ob_type->tp_name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyCF_Python2CF_string(PyObject *src, CFStringRef *dst) {
|
|
|
|
char *chars;
|
|
|
|
CFIndex size;
|
|
|
|
UniChar *unichars;
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
if (PyString_Check(src)) {
|
2003-03-03 09:19:44 -04:00
|
|
|
if (!PyArg_Parse(src, "es", "ascii", &chars))
|
2006-02-24 11:39:29 -04:00
|
|
|
return 0; /* This error is more descriptive than the general one below */
|
2003-03-03 09:12:59 -04:00
|
|
|
*dst = CFStringCreateWithCString((CFAllocatorRef)NULL, chars, kCFStringEncodingASCII);
|
2007-09-12 15:29:18 -03:00
|
|
|
PyMem_Free(chars);
|
2002-05-05 18:48:12 -03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (PyUnicode_Check(src)) {
|
|
|
|
/* We use the CF types here, if Python was configured differently that will give an error */
|
|
|
|
size = PyUnicode_GetSize(src);
|
|
|
|
if ((unichars = PyUnicode_AsUnicode(src)) == NULL ) goto err;
|
|
|
|
*dst = CFStringCreateWithCharacters((CFAllocatorRef)NULL, unichars, size);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2002-05-13 18:23:10 -03:00
|
|
|
"Cannot convert %.500s objects to CFString",
|
2002-05-05 18:48:12 -03:00
|
|
|
src->ob_type->tp_name);
|
|
|
|
return 0;
|
|
|
|
}
|