[Python-checkins] python/dist/src/Python mactoolboxglue.c, 1.23, 1.24

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Fri Nov 5 08:03:02 CET 2004


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8060/Python
Modified Files:
	mactoolboxglue.c 
Log Message:
SF patch #1035255: Remove CoreServices / CoreFoundation dependencies in core
(Contributed by Bob Ippolito.)
This patch trims down the Python core on Darwin by making it
independent of CoreFoundation and CoreServices. It does this by:
Changed linker flags in configure/configure.in
Removed the unused PyMac_GetAppletScriptFile
Moved the implementation of PyMac_StrError to the MacOS module
Moved the implementation of PyMac_GetFullPathname to the
Carbon.File module
Index: mactoolboxglue.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/mactoolboxglue.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -d -r1.23 -r1.24
--- mactoolboxglue.c	15 Jul 2004 13:31:39 -0000	1.23
+++ mactoolboxglue.c	5 Nov 2004 07:02:59 -0000	1.24
@@ -28,57 +28,39 @@
 
 
 /* Like strerror() but for Mac OS error numbers */
-char *PyMac_StrError(int err)
+char *
+PyMac_StrError(int err)
 {
 	static char buf[256];
-	Handle h;
-	char *str;
-	static int errors_loaded;
-	
-	h = GetResource('Estr', err);
-	if (!h && !errors_loaded) {
-		/*
-		** Attempt to open the resource file containing the
-		** Estr resources. We ignore all errors. We also try
-		** this only once.
-		*/
-		PyObject *m, *rv;
-		errors_loaded = 1;
-		
-		m = PyImport_ImportModule("macresource");
-		if (!m) {
-			if (Py_VerboseFlag)
-				PyErr_Print();
+	PyObject *m;
+	PyObject *rv;
+
+	m = PyImport_ImportModule("MacOS");
+	if (!m) {
+		if (Py_VerboseFlag)
+			PyErr_Print();
+		PyErr_Clear();
+		rv = NULL;
+	}
+	else {
+		rv = PyObject_CallMethod(m, "GetErrorString", "i", err);
+		if (!rv)
 			PyErr_Clear();
+	}
+	if (!rv) {
+		buf[0] = '0円';
+	}
+	else {
+		char *input = PyString_AsString(rv);
+		if (!input) {
+			PyErr_Clear();
+			buf[0] = '0円';
 		} else {
-			rv = PyObject_CallMethod(m, "open_error_resource", "");
-			if (!rv) {
-				if (Py_VerboseFlag)
-					PyErr_Print();
-				PyErr_Clear();
-			} else {
-				Py_DECREF(rv);
-				/* And try again... */
-				h = GetResource('Estr', err);
-			}
+			strncpy(buf, input, sizeof(buf) - 1);
+			buf[sizeof(buf) - 1] = '0円';
 		}
 	}
-	/*
-	** Whether the code above succeeded or not, we won't try
-	** again.
-	*/
-	errors_loaded = 1;
-		
-	if ( h ) {
-		HLock(h);
-		str = (char *)*h;
-		memcpy(buf, str+1, (unsigned char)str[0]);
-		buf[(unsigned char)str[0]] = '0円';
-		HUnlock(h);
-		ReleaseResource(h);
-	} else {
-		PyOS_snprintf(buf, sizeof(buf), "Mac OS error code %d", err);
-	}
+	
 	return buf;
 }
 
@@ -125,124 +107,51 @@
 OSErr
 PyMac_GetFullPathname(FSSpec *fss, char *path, int len)
 {
-	FSRef fsr;
-	OSErr err;
-	
-	*path = '0円';
-	err = FSpMakeFSRef(fss, &fsr);
-	if ( err == fnfErr ) {
-		/* FSSpecs can point to non-existing files, fsrefs can't. */
-		FSSpec fss2;
-		int tocopy;
-		
-		err = FSMakeFSSpec(fss->vRefNum, fss->parID, "", &fss2);
-		if ( err ) return err;
-		err = FSpMakeFSRef(&fss2, &fsr);
-		if ( err ) return err;
-		err = (OSErr)FSRefMakePath(&fsr, path, len-1);
-		if ( err ) return err;
-		/* This part is not 100% safe: we append the filename part, but
-		** I'm not sure that we don't run afoul of the various 8bit
-		** encodings here. Will have to look this up at some point...
-		*/
-		strcat(path, "/");
-		tocopy = fss->name[0];
-		if ( strlen(path) + tocopy >= len )
-			tocopy = len - strlen(path) - 1;
-		if ( tocopy > 0 )
-			strncat(path, fss->name+1, tocopy);
-	} else {
-		if ( err ) return err;
-		err = (OSErr)FSRefMakePath(&fsr, path, len);
-		if ( err ) return err;
-	}
-	return 0;
-}
-
-
-#ifdef WITH_NEXT_FRAMEWORK
-/*
-** In a bundle, find a file "resourceName" of type "resourceType". Return the
-** full pathname in "resourceURLCstr".
-*/
-static int
-locateResourcePy(CFStringRef resourceType, CFStringRef resourceName, char *resourceURLCStr, int length)
-{
- CFBundleRef mainBundle = NULL;
- CFURLRef URL, absoluteURL;
- CFStringRef filenameString, filepathString;
- CFIndex size, i;
- CFArrayRef arrayRef = NULL;
- int success = 0;
- 
-	CFURLPathStyle thePathStyle = kCFURLPOSIXPathStyle;
-
- /* Get a reference to our main bundle */
- mainBundle = CFBundleGetMainBundle();
+	PyObject *fs, *exc;
+	PyObject *rv = NULL;
+	char *input;
+	OSErr err = noErr;
 
-	/* If we are running inside a bundle, look through it. Otherwise, do nothing. */
-	if (mainBundle) {
+	*path = '0円';
 
-	 /* Look for py files in the main bundle by type */
-	 arrayRef = CFBundleCopyResourceURLsOfType( mainBundle, 
-	 resourceType, 
-	 NULL );
+	fs = PyMac_BuildFSSpec(fss);
+	if (!fs)
+		goto error;
 
-	 /* See if there are any filename matches */
-	 size = CFArrayGetCount(arrayRef);
-	 for (i = 0; i < size; i++) {
-	 URL = CFArrayGetValueAtIndex(arrayRef, i);
-	 filenameString = CFURLCopyLastPathComponent(URL);
-	 if (CFStringCompare(filenameString, resourceName, 0) == kCFCompareEqualTo) {
-	 /* We found a match, get the file's full path */
-	 absoluteURL = CFURLCopyAbsoluteURL(URL);
-	 filepathString = CFURLCopyFileSystemPath(absoluteURL, thePathStyle);
-	 CFRelease(absoluteURL);
+	rv = PyObject_CallMethod(fs, "as_pathname", "");
+	if (!rv)
+		goto error;
 
-	 /* Copy the full path into the caller's character buffer */
-	 success = CFStringGetCString(filepathString, resourceURLCStr, length,
-	 kCFStringEncodingMacRoman);
+	input = PyString_AsString(rv);
+	if (!input)
+		goto error;
 
-	 CFRelease(filepathString);
-	 }
-	 CFRelease(filenameString);
-	 }
-		CFRelease(arrayRef);
-	}
- return success;
-}
+	strncpy(path, input, len - 1);
+	path[len - 1] = '0円';
 
-/*
-** iff we are running in a .app framework then we could be
-** the main program for an applet. In that case, return the
-** script filename for the applet.
-** Otherwise return NULL.
-*/
-char *
-PyMac_GetAppletScriptFile(void)
-{
- static char scriptpath[1024];
+	Py_XDECREF(rv);
+	Py_XDECREF(fs);
+	return err;
 
-	/* First we see whether we have __rawmain__.py and run that if it
-	** is there. This is used for applets that want sys.argv to be
-	** unix-like: __rawmain__ will construct it (from the initial appleevent)
-	** and then call __main__.py.
-	*/
-	if (locateResourcePy(CFSTR("py"), CFSTR("__rawmain__.py"), scriptpath, 1024)) {
-		return scriptpath;
-	} else if (locateResourcePy(CFSTR("pyc"), CFSTR("__rawmain__.pyc"), scriptpath, 1024)) {
-		return scriptpath;
-	} else if (locateResourcePy(CFSTR("py"), CFSTR("__main__.py"), scriptpath, 1024)) {
-		return scriptpath;
-	} else if (locateResourcePy(CFSTR("pyc"), CFSTR("__main__.pyc"), scriptpath, 1024)) {
-		return scriptpath;
+ error:
+	exc = PyErr_Occurred();
+	if (exc && PyErr_GivenExceptionMatches(exc,
+						PyMac_GetOSErrException())) {
+		PyObject *args = PyObject_GetAttrString(exc, "args");
+		if (args) {
+			char *ignore;
+			PyArg_ParseTuple(args, "is", &err, &ignore);
+			Py_XDECREF(args);
+		}
 	}
-	return NULL;
+	if (err == noErr)
+		err = -1;
+	PyErr_Clear();
+	Py_XDECREF(rv);
+	Py_XDECREF(fs);
+	return err;
 }
 
-#endif
-
-
 /* Convert a 4-char string object argument to an OSType value */
 int
 PyMac_GetOSType(PyObject *v, OSType *pr)


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /