summaryrefslogtreecommitdiffstats
path: root/Mac/Modules/cf/cfsupport.py
diff options
context:
space:
mode:
authorunknown <tools@python.org>2001-07-04 22:38:52 (GMT)
committerunknown <tools@python.org>2001-07-04 22:38:52 (GMT)
commitc90acb95997de32cc6014814e686804914a9c0c3 (patch)
tree7e18f2c79ea13342007827b446b55d88b1fd02c6 /Mac/Modules/cf/cfsupport.py
parentd1054ef31a816e8a07de8caae14b379039d462ee (diff)
downloadcpython-c90acb95997de32cc6014814e686804914a9c0c3.zip
cpython-c90acb95997de32cc6014814e686804914a9c0c3.tar.gz
cpython-c90acb95997de32cc6014814e686804914a9c0c3.tar.bz2
Do conversion of CFStrings to/from unicode.
Diffstat (limited to 'Mac/Modules/cf/cfsupport.py')
-rw-r--r--Mac/Modules/cf/cfsupport.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Mac/Modules/cf/cfsupport.py b/Mac/Modules/cf/cfsupport.py
index 170d4db..ebfe717 100644
--- a/Mac/Modules/cf/cfsupport.py
+++ b/Mac/Modules/cf/cfsupport.py
@@ -250,6 +250,15 @@ class CFStringRefObjectDefinition(MyGlobalObjectDefinition):
*p_itself = CFStringCreateWithCString((CFAllocatorRef)NULL, cStr, 0);
return 1;
}
+ if (PyUnicode_Check(v)) {
+ /* We use the CF types here, if Python was configured differently that will give an error */
+ CFIndex size = PyUnicode_GetSize(v);
+ UniChar *unichars = PyUnicode_AsUnicode(v);
+ if (!unichars) return 0;
+ *p_itself = CFStringCreateWithCharacters((CFAllocatorRef)NULL, unichars, size);
+ return 1;
+ }
+
""")
def outputRepr(self):
@@ -377,6 +386,24 @@ f = ManualGenerator("CFStringGetString", getasstring_body);
f.docstring = lambda: "() -> (string _rv)"
CFStringRef_object.add(f)
+getasunicode_body = """
+int size = CFStringGetLength(_self->ob_itself)+1;
+Py_UNICODE *data = malloc(size*sizeof(Py_UNICODE));
+CFRange range;
+
+range.location = 0;
+range.length = size;
+if( data == NULL ) return PyErr_NoMemory();
+CFStringGetCharacters(_self->ob_itself, range, data);
+_res = (PyObject *)PyUnicode_FromUnicode(data, size);
+free(data);
+return _res;
+"""
+
+f = ManualGenerator("CFStringGetUnicode", getasunicode_body);
+f.docstring = lambda: "() -> (unicode _rv)"
+CFStringRef_object.add(f)
+
# ADD add forloop here
# generate output (open the output file as late as possible)