| 1 | n/a | /* |
|---|
| 2 | n/a | ** Convert objects from Python to CoreFoundation and vice-versa. |
|---|
| 3 | n/a | */ |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | #include <CoreServices/CoreServices.h> |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | #include "Python.h" |
|---|
| 8 | n/a | #include "pymactoolbox.h" |
|---|
| 9 | n/a | #include "pycfbridge.h" |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | /* ---------------------------------------- */ |
|---|
| 13 | n/a | /* CoreFoundation objects to Python objects */ |
|---|
| 14 | n/a | /* ---------------------------------------- */ |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | PyObject * |
|---|
| 17 | n/a | PyCF_CF2Python(CFTypeRef src) { |
|---|
| 18 | n/a | CFTypeID typeid; |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | if( src == NULL ) { |
|---|
| 21 | n/a | Py_INCREF(Py_None); |
|---|
| 22 | n/a | return Py_None; |
|---|
| 23 | n/a | } |
|---|
| 24 | n/a | typeid = CFGetTypeID(src); |
|---|
| 25 | n/a | if (typeid == CFArrayGetTypeID()) |
|---|
| 26 | n/a | return PyCF_CF2Python_sequence((CFArrayRef)src); |
|---|
| 27 | n/a | if (typeid == CFDictionaryGetTypeID()) |
|---|
| 28 | n/a | return PyCF_CF2Python_mapping((CFDictionaryRef)src); |
|---|
| 29 | n/a | return PyCF_CF2Python_simple(src); |
|---|
| 30 | n/a | } |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | PyObject * |
|---|
| 33 | n/a | PyCF_CF2Python_sequence(CFArrayRef src) { |
|---|
| 34 | n/a | int size = CFArrayGetCount(src); |
|---|
| 35 | n/a | PyObject *rv; |
|---|
| 36 | n/a | CFTypeRef item_cf; |
|---|
| 37 | n/a | PyObject *item_py = NULL; |
|---|
| 38 | n/a | int i; |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | if ( (rv=PyList_New(size)) == NULL ) |
|---|
| 41 | n/a | return NULL; |
|---|
| 42 | n/a | for(i=0; i<size; i++) { |
|---|
| 43 | n/a | item_cf = CFArrayGetValueAtIndex(src, i); |
|---|
| 44 | n/a | if (item_cf == NULL ) goto err; |
|---|
| 45 | n/a | item_py = PyCF_CF2Python(item_cf); |
|---|
| 46 | n/a | if (item_py == NULL ) goto err; |
|---|
| 47 | n/a | if (PyList_SetItem(rv, i, item_py) < 0) goto err; |
|---|
| 48 | n/a | item_py = NULL; |
|---|
| 49 | n/a | } |
|---|
| 50 | n/a | return rv; |
|---|
| 51 | n/a | err: |
|---|
| 52 | n/a | Py_XDECREF(item_py); |
|---|
| 53 | n/a | Py_DECREF(rv); |
|---|
| 54 | n/a | return NULL; |
|---|
| 55 | n/a | } |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | PyObject * |
|---|
| 58 | n/a | PyCF_CF2Python_mapping(CFTypeRef src) { |
|---|
| 59 | n/a | int size = CFDictionaryGetCount(src); |
|---|
| 60 | n/a | PyObject *rv = NULL; |
|---|
| 61 | n/a | CFTypeRef *allkeys = NULL, *allvalues = NULL; |
|---|
| 62 | n/a | CFTypeRef key_cf, value_cf; |
|---|
| 63 | n/a | PyObject *key_py = NULL, *value_py = NULL; |
|---|
| 64 | n/a | int i; |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | allkeys = malloc(size*sizeof(CFTypeRef *)); |
|---|
| 67 | n/a | if (allkeys == NULL) { |
|---|
| 68 | n/a | PyErr_NoMemory(); |
|---|
| 69 | n/a | goto err; |
|---|
| 70 | n/a | } |
|---|
| 71 | n/a | allvalues = malloc(size*sizeof(CFTypeRef *)); |
|---|
| 72 | n/a | if (allvalues == NULL) { |
|---|
| 73 | n/a | PyErr_NoMemory(); |
|---|
| 74 | n/a | goto err; |
|---|
| 75 | n/a | } |
|---|
| 76 | n/a | if ( (rv=PyDict_New()) == NULL ) goto err; |
|---|
| 77 | n/a | CFDictionaryGetKeysAndValues(src, allkeys, allvalues); |
|---|
| 78 | n/a | for(i=0; i<size; i++) { |
|---|
| 79 | n/a | key_cf = allkeys[i]; |
|---|
| 80 | n/a | value_cf = allvalues[i]; |
|---|
| 81 | n/a | key_py = PyCF_CF2Python(key_cf); |
|---|
| 82 | n/a | if (key_py == NULL ) goto err; |
|---|
| 83 | n/a | value_py = PyCF_CF2Python(value_cf); |
|---|
| 84 | n/a | if (value_py == NULL ) goto err; |
|---|
| 85 | n/a | if (PyDict_SetItem(rv, key_py, value_py) < 0) goto err; |
|---|
| 86 | n/a | key_py = NULL; |
|---|
| 87 | n/a | value_py = NULL; |
|---|
| 88 | n/a | } |
|---|
| 89 | n/a | return rv; |
|---|
| 90 | n/a | err: |
|---|
| 91 | n/a | Py_XDECREF(key_py); |
|---|
| 92 | n/a | Py_XDECREF(value_py); |
|---|
| 93 | n/a | Py_XDECREF(rv); |
|---|
| 94 | n/a | free(allkeys); |
|---|
| 95 | n/a | free(allvalues); |
|---|
| 96 | n/a | return NULL; |
|---|
| 97 | n/a | } |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | PyObject * |
|---|
| 100 | n/a | PyCF_CF2Python_simple(CFTypeRef src) { |
|---|
| 101 | n/a | CFTypeID typeid; |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | typeid = CFGetTypeID(src); |
|---|
| 104 | n/a | if (typeid == CFStringGetTypeID()) |
|---|
| 105 | n/a | return PyCF_CF2Python_string((CFStringRef)src); |
|---|
| 106 | n/a | if (typeid == CFBooleanGetTypeID()) |
|---|
| 107 | n/a | return PyBool_FromLong((long)CFBooleanGetValue(src)); |
|---|
| 108 | n/a | if (typeid == CFNumberGetTypeID()) { |
|---|
| 109 | n/a | if (CFNumberIsFloatType(src)) { |
|---|
| 110 | n/a | double d; |
|---|
| 111 | n/a | CFNumberGetValue(src, kCFNumberDoubleType, &d); |
|---|
| 112 | n/a | return PyFloat_FromDouble(d); |
|---|
| 113 | n/a | } else { |
|---|
| 114 | n/a | long l; |
|---|
| 115 | n/a | if (!CFNumberGetValue(src, kCFNumberLongType, &l)) |
|---|
| 116 | n/a | /* XXXX Out of range! */; |
|---|
| 117 | n/a | return PyInt_FromLong(l); |
|---|
| 118 | n/a | } |
|---|
| 119 | n/a | } |
|---|
| 120 | n/a | /* XXXX Should return as CFTypeRef, really... */ |
|---|
| 121 | n/a | PyMac_Error(resNotFound); |
|---|
| 122 | n/a | return NULL; |
|---|
| 123 | n/a | } |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | /* Unsure - Return unicode or 8 bit strings? */ |
|---|
| 126 | n/a | PyObject * |
|---|
| 127 | n/a | PyCF_CF2Python_string(CFStringRef src) { |
|---|
| 128 | n/a | int size = CFStringGetLength(src)+1; |
|---|
| 129 | n/a | Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE)); |
|---|
| 130 | n/a | CFRange range; |
|---|
| 131 | n/a | PyObject *rv; |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | range.location = 0; |
|---|
| 134 | n/a | range.length = size; |
|---|
| 135 | n/a | if( data == NULL ) return PyErr_NoMemory(); |
|---|
| 136 | n/a | CFStringGetCharacters(src, range, data); |
|---|
| 137 | n/a | rv = (PyObject *)PyUnicode_FromUnicode(data, size-1); |
|---|
| 138 | n/a | free(data); |
|---|
| 139 | n/a | return rv; |
|---|
| 140 | n/a | } |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | /* ---------------------------------------- */ |
|---|
| 143 | n/a | /* Python objects to CoreFoundation objects */ |
|---|
| 144 | n/a | /* ---------------------------------------- */ |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | int |
|---|
| 147 | n/a | PyCF_Python2CF(PyObject *src, CFTypeRef *dst) { |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | if (PyString_Check(src) || PyUnicode_Check(src)) |
|---|
| 150 | n/a | return PyCF_Python2CF_simple(src, dst); |
|---|
| 151 | n/a | if (PySequence_Check(src)) |
|---|
| 152 | n/a | return PyCF_Python2CF_sequence(src, (CFArrayRef *)dst); |
|---|
| 153 | n/a | if (PyMapping_Check(src)) |
|---|
| 154 | n/a | return PyCF_Python2CF_mapping(src, (CFDictionaryRef *)dst); |
|---|
| 155 | n/a | return PyCF_Python2CF_simple(src, dst); |
|---|
| 156 | n/a | } |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | int |
|---|
| 159 | n/a | PyCF_Python2CF_sequence(PyObject *src, CFArrayRef *dst) { |
|---|
| 160 | n/a | CFMutableArrayRef rv = NULL; |
|---|
| 161 | n/a | CFTypeRef item_cf = NULL; |
|---|
| 162 | n/a | PyObject *item_py = NULL; |
|---|
| 163 | n/a | int size, i; |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | if( !PySequence_Check(src) ) { |
|---|
| 166 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 167 | n/a | "Cannot convert %.500s objects to CFArray", |
|---|
| 168 | n/a | src->ob_type->tp_name); |
|---|
| 169 | n/a | return 0; |
|---|
| 170 | n/a | } |
|---|
| 171 | n/a | size = PySequence_Size(src); |
|---|
| 172 | n/a | rv = CFArrayCreateMutable((CFAllocatorRef)NULL, size, &kCFTypeArrayCallBacks); |
|---|
| 173 | n/a | if (rv == NULL) { |
|---|
| 174 | n/a | PyMac_Error(resNotFound); |
|---|
| 175 | n/a | goto err; |
|---|
| 176 | n/a | } |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | for( i=0; i<size; i++) { |
|---|
| 179 | n/a | item_py = PySequence_GetItem(src, i); |
|---|
| 180 | n/a | if (item_py == NULL) goto err; |
|---|
| 181 | n/a | if ( !PyCF_Python2CF(item_py, &item_cf)) goto err; |
|---|
| 182 | n/a | Py_DECREF(item_py); |
|---|
| 183 | n/a | CFArraySetValueAtIndex(rv, i, item_cf); |
|---|
| 184 | n/a | CFRelease(item_cf); |
|---|
| 185 | n/a | item_cf = NULL; |
|---|
| 186 | n/a | } |
|---|
| 187 | n/a | *dst = rv; |
|---|
| 188 | n/a | return 1; |
|---|
| 189 | n/a | err: |
|---|
| 190 | n/a | Py_XDECREF(item_py); |
|---|
| 191 | n/a | if (rv) CFRelease(rv); |
|---|
| 192 | n/a | if (item_cf) CFRelease(item_cf); |
|---|
| 193 | n/a | return 0; |
|---|
| 194 | n/a | } |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | int |
|---|
| 197 | n/a | PyCF_Python2CF_mapping(PyObject *src, CFDictionaryRef *dst) { |
|---|
| 198 | n/a | CFMutableDictionaryRef rv = NULL; |
|---|
| 199 | n/a | PyObject *aslist = NULL; |
|---|
| 200 | n/a | CFTypeRef key_cf = NULL, value_cf = NULL; |
|---|
| 201 | n/a | PyObject *item_py = NULL, *key_py = NULL, *value_py = NULL; |
|---|
| 202 | n/a | int size, i; |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | if( !PyMapping_Check(src) ) { |
|---|
| 205 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 206 | n/a | "Cannot convert %.500s objects to CFDictionary", |
|---|
| 207 | n/a | src->ob_type->tp_name); |
|---|
| 208 | n/a | return 0; |
|---|
| 209 | n/a | } |
|---|
| 210 | n/a | size = PyMapping_Size(src); |
|---|
| 211 | n/a | rv = CFDictionaryCreateMutable((CFAllocatorRef)NULL, size, |
|---|
| 212 | n/a | &kCFTypeDictionaryKeyCallBacks, |
|---|
| 213 | n/a | &kCFTypeDictionaryValueCallBacks); |
|---|
| 214 | n/a | if (rv == NULL) { |
|---|
| 215 | n/a | PyMac_Error(resNotFound); |
|---|
| 216 | n/a | goto err; |
|---|
| 217 | n/a | } |
|---|
| 218 | n/a | if ( (aslist = PyMapping_Items(src)) == NULL ) goto err; |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | for( i=0; i<size; i++) { |
|---|
| 221 | n/a | item_py = PySequence_GetItem(aslist, i); |
|---|
| 222 | n/a | if (item_py == NULL) goto err; |
|---|
| 223 | n/a | if (!PyArg_ParseTuple(item_py, "OO", &key_py, &value_py)) goto err; |
|---|
| 224 | n/a | if ( !PyCF_Python2CF(key_py, &key_cf) ) goto err; |
|---|
| 225 | n/a | if ( !PyCF_Python2CF(value_py, &value_cf) ) goto err; |
|---|
| 226 | n/a | CFDictionaryAddValue(rv, key_cf, value_cf); |
|---|
| 227 | n/a | CFRelease(key_cf); |
|---|
| 228 | n/a | key_cf = NULL; |
|---|
| 229 | n/a | CFRelease(value_cf); |
|---|
| 230 | n/a | value_cf = NULL; |
|---|
| 231 | n/a | } |
|---|
| 232 | n/a | *dst = rv; |
|---|
| 233 | n/a | return 1; |
|---|
| 234 | n/a | err: |
|---|
| 235 | n/a | Py_XDECREF(item_py); |
|---|
| 236 | n/a | Py_XDECREF(aslist); |
|---|
| 237 | n/a | if (rv) CFRelease(rv); |
|---|
| 238 | n/a | if (key_cf) CFRelease(key_cf); |
|---|
| 239 | n/a | if (value_cf) CFRelease(value_cf); |
|---|
| 240 | n/a | return 0; |
|---|
| 241 | n/a | } |
|---|
| 242 | n/a | |
|---|
| 243 | n/a | int |
|---|
| 244 | n/a | PyCF_Python2CF_simple(PyObject *src, CFTypeRef *dst) { |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | #if 0 |
|---|
| 247 | n/a | if (PyObject_HasAttrString(src, "CFType")) { |
|---|
| 248 | n/a | *dst = PyObject_CallMethod(src, "CFType", ""); |
|---|
| 249 | n/a | return (*dst != NULL); |
|---|
| 250 | n/a | } |
|---|
| 251 | n/a | #endif |
|---|
| 252 | n/a | if (PyString_Check(src) || PyUnicode_Check(src)) |
|---|
| 253 | n/a | return PyCF_Python2CF_string(src, (CFStringRef *)dst); |
|---|
| 254 | n/a | if (PyBool_Check(src)) { |
|---|
| 255 | n/a | if (src == Py_True) |
|---|
| 256 | n/a | *dst = kCFBooleanTrue; |
|---|
| 257 | n/a | else |
|---|
| 258 | n/a | *dst = kCFBooleanFalse; |
|---|
| 259 | n/a | return 1; |
|---|
| 260 | n/a | } |
|---|
| 261 | n/a | if (PyInt_Check(src)) { |
|---|
| 262 | n/a | long v = PyInt_AsLong(src); |
|---|
| 263 | n/a | *dst = CFNumberCreate(NULL, kCFNumberLongType, &v); |
|---|
| 264 | n/a | return 1; |
|---|
| 265 | n/a | } |
|---|
| 266 | n/a | if (PyFloat_Check(src)) { |
|---|
| 267 | n/a | double d = PyFloat_AsDouble(src); |
|---|
| 268 | n/a | *dst = CFNumberCreate(NULL, kCFNumberDoubleType, &d); |
|---|
| 269 | n/a | return 1; |
|---|
| 270 | n/a | } |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 273 | n/a | "Cannot convert %.500s objects to CFType", |
|---|
| 274 | n/a | src->ob_type->tp_name); |
|---|
| 275 | n/a | return 0; |
|---|
| 276 | n/a | } |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | int |
|---|
| 279 | n/a | PyCF_Python2CF_string(PyObject *src, CFStringRef *dst) { |
|---|
| 280 | n/a | char *chars; |
|---|
| 281 | n/a | CFIndex size; |
|---|
| 282 | n/a | UniChar *unichars; |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | if (PyString_Check(src)) { |
|---|
| 285 | n/a | if (!PyArg_Parse(src, "es", "ascii", &chars)) |
|---|
| 286 | n/a | return 0; /* This error is more descriptive than the general one below */ |
|---|
| 287 | n/a | *dst = CFStringCreateWithCString((CFAllocatorRef)NULL, chars, kCFStringEncodingASCII); |
|---|
| 288 | n/a | PyMem_Free(chars); |
|---|
| 289 | n/a | return 1; |
|---|
| 290 | n/a | } |
|---|
| 291 | n/a | if (PyUnicode_Check(src)) { |
|---|
| 292 | n/a | /* We use the CF types here, if Python was configured differently that will give an error */ |
|---|
| 293 | n/a | size = PyUnicode_GetSize(src); |
|---|
| 294 | n/a | if ((unichars = PyUnicode_AsUnicode(src)) == NULL ) goto err; |
|---|
| 295 | n/a | *dst = CFStringCreateWithCharacters((CFAllocatorRef)NULL, unichars, size); |
|---|
| 296 | n/a | return 1; |
|---|
| 297 | n/a | } |
|---|
| 298 | n/a | err: |
|---|
| 299 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 300 | n/a | "Cannot convert %.500s objects to CFString", |
|---|
| 301 | n/a | src->ob_type->tp_name); |
|---|
| 302 | n/a | return 0; |
|---|
| 303 | n/a | } |
|---|