Mercurial > cpython-lru_cache-weakref
annotate Mac/Modules/cf/pycfbridge.c @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 52294af4b663 |
| children | fd25030223b0 |
| rev | line source |
|---|---|
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
1 /* |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
2 ** Convert objects from Python to CoreFoundation and vice-versa. |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
3 */ |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
4 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
5 #include <CoreServices/CoreServices.h> |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
6 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
7 #include "Python.h" |
|
30982
e543b3f0d906
Got rid of macglue.h, replacing it by pymactoolbox.h where relevant.
Jack Jansen <jack.jansen@cwi.nl>
parents:
30973
diff
changeset
|
8 #include "pymactoolbox.h" |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
9 #include "pycfbridge.h" |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
10 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
11 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
12 /* ---------------------------------------- */ |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
13 /* CoreFoundation objects to Python objects */ |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
14 /* ---------------------------------------- */ |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
15 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
16 PyObject * |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
17 PyCF_CF2Python(CFTypeRef src) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
18 CFTypeID typeid; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
19 |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
20 if( src == NULL ) { |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
21 Py_INCREF(Py_None); |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
22 return Py_None; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
23 } |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
24 typeid = CFGetTypeID(src); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
25 if (typeid == CFArrayGetTypeID()) |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
26 return PyCF_CF2Python_sequence((CFArrayRef)src); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
27 if (typeid == CFDictionaryGetTypeID()) |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
28 return PyCF_CF2Python_mapping((CFDictionaryRef)src); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
29 return PyCF_CF2Python_simple(src); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
30 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
31 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
32 PyObject * |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
33 PyCF_CF2Python_sequence(CFArrayRef src) { |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
34 int size = CFArrayGetCount(src); |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
35 PyObject *rv; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
36 CFTypeRef item_cf; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
37 PyObject *item_py = NULL; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
38 int i; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
39 |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
40 if ( (rv=PyList_New(size)) == NULL ) |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
41 return NULL; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
42 for(i=0; i<size; i++) { |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
43 item_cf = CFArrayGetValueAtIndex(src, i); |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
44 if (item_cf == NULL ) goto err; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
45 item_py = PyCF_CF2Python(item_cf); |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
46 if (item_py == NULL ) goto err; |
|
23404
b2aa2648df27
Partial fix for string handling. Null byte TBD.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23395
diff
changeset
|
47 if (PyList_SetItem(rv, i, item_py) < 0) goto err; |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
48 item_py = NULL; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
49 } |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
50 return rv; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
51 err: |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
52 Py_XDECREF(item_py); |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
53 Py_DECREF(rv); |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
54 return NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
55 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
56 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
57 PyObject * |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
58 PyCF_CF2Python_mapping(CFTypeRef src) { |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
59 int size = CFDictionaryGetCount(src); |
|
23405
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
60 PyObject *rv = NULL; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
61 CFTypeRef *allkeys = NULL, *allvalues = NULL; |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
62 CFTypeRef key_cf, value_cf; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
63 PyObject *key_py = NULL, *value_py = NULL; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
64 int i; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
65 |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
66 allkeys = malloc(size*sizeof(CFTypeRef *)); |
|
23405
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
67 if (allkeys == NULL) { |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
68 PyErr_NoMemory(); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
69 goto err; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
70 } |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
71 allvalues = malloc(size*sizeof(CFTypeRef *)); |
|
23405
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
72 if (allvalues == NULL) { |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
73 PyErr_NoMemory(); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
74 goto err; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
75 } |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
76 if ( (rv=PyDict_New()) == NULL ) goto err; |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
77 CFDictionaryGetKeysAndValues(src, allkeys, allvalues); |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
78 for(i=0; i<size; i++) { |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
79 key_cf = allkeys[i]; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
80 value_cf = allvalues[i]; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
81 key_py = PyCF_CF2Python(key_cf); |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
82 if (key_py == NULL ) goto err; |
|
23405
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
83 value_py = PyCF_CF2Python(value_cf); |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
84 if (value_py == NULL ) goto err; |
|
23404
b2aa2648df27
Partial fix for string handling. Null byte TBD.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23395
diff
changeset
|
85 if (PyDict_SetItem(rv, key_py, value_py) < 0) goto err; |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
86 key_py = NULL; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
87 value_py = NULL; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
88 } |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
89 return rv; |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
90 err: |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
91 Py_XDECREF(key_py); |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
92 Py_XDECREF(value_py); |
|
23405
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
93 Py_XDECREF(rv); |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
94 free(allkeys); |
|
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
95 free(allvalues); |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
96 return NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
97 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
98 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
99 PyObject * |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
100 PyCF_CF2Python_simple(CFTypeRef src) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
101 CFTypeID typeid; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
102 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
103 typeid = CFGetTypeID(src); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
104 if (typeid == CFStringGetTypeID()) |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
105 return PyCF_CF2Python_string((CFStringRef)src); |
|
23405
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
106 if (typeid == CFBooleanGetTypeID()) |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
107 return PyBool_FromLong((long)CFBooleanGetValue(src)); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
108 if (typeid == CFNumberGetTypeID()) { |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
109 if (CFNumberIsFloatType(src)) { |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
110 double d; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
111 CFNumberGetValue(src, kCFNumberDoubleType, &d); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
112 return PyFloat_FromDouble(d); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
113 } else { |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
114 long l; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
115 if (!CFNumberGetValue(src, kCFNumberLongType, &l)) |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
116 /* XXXX Out of range! */; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
117 return PyInt_FromLong(l); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
118 } |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
119 } |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
120 /* XXXX Should return as CFTypeRef, really... */ |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
121 PyMac_Error(resNotFound); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
122 return NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
123 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
124 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
125 /* Unsure - Return unicode or 8 bit strings? */ |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
126 PyObject * |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
127 PyCF_CF2Python_string(CFStringRef src) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
128 int size = CFStringGetLength(src)+1; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
129 Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE)); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
130 CFRange range; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
131 PyObject *rv; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
132 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
133 range.location = 0; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
134 range.length = size; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
135 if( data == NULL ) return PyErr_NoMemory(); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
136 CFStringGetCharacters(src, range, data); |
|
23405
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
137 rv = (PyObject *)PyUnicode_FromUnicode(data, size-1); |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
138 free(data); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
139 return rv; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
140 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
141 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
142 /* ---------------------------------------- */ |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
143 /* Python objects to CoreFoundation objects */ |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
144 /* ---------------------------------------- */ |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
145 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
146 int |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
147 PyCF_Python2CF(PyObject *src, CFTypeRef *dst) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
148 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
149 if (PyString_Check(src) || PyUnicode_Check(src)) |
|
23404
b2aa2648df27
Partial fix for string handling. Null byte TBD.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23395
diff
changeset
|
150 return PyCF_Python2CF_simple(src, dst); |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
151 if (PySequence_Check(src)) |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
152 return PyCF_Python2CF_sequence(src, (CFArrayRef *)dst); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
153 if (PyMapping_Check(src)) |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
154 return PyCF_Python2CF_mapping(src, (CFDictionaryRef *)dst); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
155 return PyCF_Python2CF_simple(src, dst); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
156 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
157 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
158 int |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
159 PyCF_Python2CF_sequence(PyObject *src, CFArrayRef *dst) { |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
160 CFMutableArrayRef rv = NULL; |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
161 CFTypeRef item_cf = NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
162 PyObject *item_py = NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
163 int size, i; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
164 |
|
23439
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
165 if( !PySequence_Check(src) ) { |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
166 PyErr_Format(PyExc_TypeError, |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
167 "Cannot convert %.500s objects to CFArray", |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
168 src->ob_type->tp_name); |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
169 return 0; |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
170 } |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
171 size = PySequence_Size(src); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
172 rv = CFArrayCreateMutable((CFAllocatorRef)NULL, size, &kCFTypeArrayCallBacks); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
173 if (rv == NULL) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
174 PyMac_Error(resNotFound); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
175 goto err; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
176 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
177 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
178 for( i=0; i<size; i++) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
179 item_py = PySequence_GetItem(src, i); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
180 if (item_py == NULL) goto err; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
181 if ( !PyCF_Python2CF(item_py, &item_cf)) goto err; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
182 Py_DECREF(item_py); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
183 CFArraySetValueAtIndex(rv, i, item_cf); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
184 CFRelease(item_cf); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
185 item_cf = NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
186 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
187 *dst = rv; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
188 return 1; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
189 err: |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
190 Py_XDECREF(item_py); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
191 if (rv) CFRelease(rv); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
192 if (item_cf) CFRelease(item_cf); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
193 return 0; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
194 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
195 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
196 int |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
197 PyCF_Python2CF_mapping(PyObject *src, CFDictionaryRef *dst) { |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
198 CFMutableDictionaryRef rv = NULL; |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
199 PyObject *aslist = NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
200 CFTypeRef key_cf = NULL, value_cf = NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
201 PyObject *item_py = NULL, *key_py = NULL, *value_py = NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
202 int size, i; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
203 |
|
23439
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
204 if( !PyMapping_Check(src) ) { |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
205 PyErr_Format(PyExc_TypeError, |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
206 "Cannot convert %.500s objects to CFDictionary", |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
207 src->ob_type->tp_name); |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
208 return 0; |
|
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
209 } |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
210 size = PyMapping_Size(src); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
211 rv = CFDictionaryCreateMutable((CFAllocatorRef)NULL, size, |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
212 &kCFTypeDictionaryKeyCallBacks, |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
213 &kCFTypeDictionaryValueCallBacks); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
214 if (rv == NULL) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
215 PyMac_Error(resNotFound); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
216 goto err; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
217 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
218 if ( (aslist = PyMapping_Items(src)) == NULL ) goto err; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
219 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
220 for( i=0; i<size; i++) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
221 item_py = PySequence_GetItem(aslist, i); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
222 if (item_py == NULL) goto err; |
|
23405
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
223 if (!PyArg_ParseTuple(item_py, "OO", &key_py, &value_py)) goto err; |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
224 if ( !PyCF_Python2CF(key_py, &key_cf) ) goto err; |
|
23395
bfe91e1e31b2
More support for bridging between Python and CoreFoundation objects. Still untested.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23381
diff
changeset
|
225 if ( !PyCF_Python2CF(value_py, &value_cf) ) goto err; |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
226 CFDictionaryAddValue(rv, key_cf, value_cf); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
227 CFRelease(key_cf); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
228 key_cf = NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
229 CFRelease(value_cf); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
230 value_cf = NULL; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
231 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
232 *dst = rv; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
233 return 1; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
234 err: |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
235 Py_XDECREF(item_py); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
236 Py_XDECREF(aslist); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
237 if (rv) CFRelease(rv); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
238 if (key_cf) CFRelease(key_cf); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
239 if (value_cf) CFRelease(value_cf); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
240 return 0; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
241 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
242 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
243 int |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
244 PyCF_Python2CF_simple(PyObject *src, CFTypeRef *dst) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
245 |
|
23439
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
246 #if 0 |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
247 if (PyObject_HasAttrString(src, "CFType")) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
248 *dst = PyObject_CallMethod(src, "CFType", ""); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
249 return (*dst != NULL); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
250 } |
|
23439
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
251 #endif |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
252 if (PyString_Check(src) || PyUnicode_Check(src)) |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
253 return PyCF_Python2CF_string(src, (CFStringRef *)dst); |
|
23405
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
254 if (PyBool_Check(src)) { |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
255 if (src == Py_True) |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
256 *dst = kCFBooleanTrue; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
257 else |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
258 *dst = kCFBooleanFalse; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
259 return 1; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
260 } |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
261 if (PyInt_Check(src)) { |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
262 long v = PyInt_AsLong(src); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
263 *dst = CFNumberCreate(NULL, kCFNumberLongType, &v); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
264 return 1; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
265 } |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
266 if (PyFloat_Check(src)) { |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
267 double d = PyFloat_AsDouble(src); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
268 *dst = CFNumberCreate(NULL, kCFNumberDoubleType, &d); |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
269 return 1; |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
270 } |
|
977e534fc6c7
Fixed string and dict conversion, and implemented booleans and numbers (int and float). I think we now have enough CFType support to start on plists and CFpreferences!
Jack Jansen <jack.jansen@cwi.nl>
parents:
23404
diff
changeset
|
271 |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
272 PyErr_Format(PyExc_TypeError, |
|
23439
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
273 "Cannot convert %.500s objects to CFType", |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
274 src->ob_type->tp_name); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
275 return 0; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
276 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
277 |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
278 int |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
279 PyCF_Python2CF_string(PyObject *src, CFStringRef *dst) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
280 char *chars; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
281 CFIndex size; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
282 UniChar *unichars; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
283 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
284 if (PyString_Check(src)) { |
|
28147
2815c1a9f735
Mod to previous checkin: we must require ascii, not system defautl encoding,
Jack Jansen <jack.jansen@cwi.nl>
parents:
28146
diff
changeset
|
285 if (!PyArg_Parse(src, "es", "ascii", &chars)) |
|
36650
5d828703c6a1
Fix a couple of warnings on Mac OS X.
Neal Norwitz <nnorwitz@gmail.com>
parents:
30982
diff
changeset
|
286 return 0; /* This error is more descriptive than the general one below */ |
|
28146
f9b702c52dae
Accept only the system default encoding when converting Python
Jack Jansen <jack.jansen@cwi.nl>
parents:
23439
diff
changeset
|
287 *dst = CFStringCreateWithCString((CFAllocatorRef)NULL, chars, kCFStringEncodingASCII); |
|
43344
840ff58d1e9e
bug #1154: release memory allocated by "es" PyArg_ParseTuple format specifier.
Georg Brandl <georg@python.org>
parents:
36650
diff
changeset
|
288 PyMem_Free(chars); |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
289 return 1; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
290 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
291 if (PyUnicode_Check(src)) { |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
292 /* We use the CF types here, if Python was configured differently that will give an error */ |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
293 size = PyUnicode_GetSize(src); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
294 if ((unichars = PyUnicode_AsUnicode(src)) == NULL ) goto err; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
295 *dst = CFStringCreateWithCharacters((CFAllocatorRef)NULL, unichars, size); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
296 return 1; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
297 } |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
298 err: |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
299 PyErr_Format(PyExc_TypeError, |
|
23439
de0ac03e5e72
Added typechecking to the individual python->CF converters, so we can use them in the CF object initializers safely.
Jack Jansen <jack.jansen@cwi.nl>
parents:
23405
diff
changeset
|
300 "Cannot convert %.500s objects to CFString", |
|
23381
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
301 src->ob_type->tp_name); |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
302 return 0; |
|
82a850db7ec5
First steps towards a generalized converter of Python object
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff
changeset
|
303 } |
