]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
- Use weaklink generators so we can support OSX-only calls without crashing on OS9.
authorJack Jansen <jack.jansen@cwi.nl>
Sun, 1 Jul 2001 22:04:02 +0000 (22:04 +0000)
committerJack Jansen <jack.jansen@cwi.nl>
Sun, 1 Jul 2001 22:04:02 +0000 (22:04 +0000)
- Convert CFString to/from Python strings. Currently always MacRoman, to be fixed later (as is unicode support). Python->CFString conversion is automatic.

Mac/Modules/cf/cfscan.py
Mac/Modules/cf/cfsupport.py

index 2d11867c4e31e22fe1ab4beae3d22b7e1e501734..79418755b28b785d5bd6df712d29e86f0978fdc7 100644 (file)
@@ -83,9 +83,11 @@ class MyScanner(Scanner_OSX):
                        "CFStringGetPascalStringPtr", # TBD automatically
                        "CFStringGetCStringPtr", 
                        "CFStringGetCharactersPtr",
+                       "CFStringGetCString", 
+                       "CFStringGetCharacters",
                        # OSX only, to be done
-                       "CFURLCreateWithFileSystemPath",
-                       "CFURLCreateStringWithFileSystemPath",
+##                     "CFURLCreateWithFileSystemPath",
+##                     "CFURLCreateStringWithFileSystemPath",
                        ]
 
        def makegreylist(self):
index bdc20f8c6e33b760119dce4b923bf322ac56308a..170d4db06ae14f6eba46f820c76e0c00033de193 100644 (file)
@@ -119,7 +119,6 @@ OptionalCFURLRef  = OpaqueByValueType("CFURLRef", "OptionalCFURLRefObj")
 class MyGlobalObjectDefinition(GlobalObjectDefinition):
        def outputCheckNewArg(self):
                Output("if (itself == NULL) return PyMac_Error(resNotFound);")
-               Output("CFRetain(itself);")
        def outputStructMembers(self):
                GlobalObjectDefinition.outputStructMembers(self)
                Output("void (*ob_freeit)(CFTypeRef ptr);")
@@ -243,6 +242,16 @@ class CFMutableDataRefObjectDefinition(MyGlobalObjectDefinition):
 class CFStringRefObjectDefinition(MyGlobalObjectDefinition):
        basechain = "&CFTypeRefObj_chain"
        
+       def outputCheckConvertArg(self):
+               Out("""
+               if (v == Py_None) { *p_itself = NULL; return 1; }
+               if (PyString_Check(v)) {
+                   char *cStr = PyString_AsString(v);
+                       *p_itself = CFStringCreateWithCString((CFAllocatorRef)NULL, cStr, 0);
+                       return 1;
+               }
+               """)
+
        def outputRepr(self):
                Output()
                Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
@@ -255,6 +264,10 @@ class CFStringRefObjectDefinition(MyGlobalObjectDefinition):
 class CFMutableStringRefObjectDefinition(CFStringRefObjectDefinition):
        basechain = "&CFStringRefObj_chain"
        
+       def outputCheckConvertArg(self):
+               # Mutable, don't allow Python strings
+               return MyGlobalObjectDefinition.outputCheckConvertArg(self)
+               
        def outputRepr(self):
                Output()
                Output("static PyObject * %s_repr(%s *self)", self.prefix, self.objecttype)
@@ -309,8 +322,8 @@ module.addobject(CFURLRef_object)
 # ADD addobject call here
 
 # Create the generator classes used to populate the lists
-Function = OSErrFunctionGenerator
-Method = OSErrMethodGenerator
+Function = OSErrWeakLinkFunctionGenerator
+Method = OSErrWeakLinkMethodGenerator
 
 # Create and populate the lists
 functions = []
@@ -343,6 +356,27 @@ for f in CFStringRef_methods: CFStringRef_object.add(f)
 for f in CFMutableStringRef_methods: CFMutableStringRef_object.add(f)
 for f in CFURLRef_methods: CFURLRef_object.add(f)
 
+# Manual generators for getting data out of strings
+
+getasstring_body = """
+int size = CFStringGetLength(_self->ob_itself)+1;
+char *data = malloc(size);
+
+if( data == NULL ) return PyErr_NoMemory();
+if ( CFStringGetCString(_self->ob_itself, data, size, 0) ) {
+       _res = (PyObject *)PyString_FromString(data);
+} else {
+       PyErr_SetString(PyExc_RuntimeError, "CFStringGetCString could not fit the string");
+       _res = NULL;
+}
+free(data);
+return _res;
+"""
+
+f = ManualGenerator("CFStringGetString", getasstring_body);
+f.docstring = lambda: "() -> (string _rv)"
+CFStringRef_object.add(f)
+
 # ADD add forloop here
 
 # generate output (open the output file as late as possible)