--- Mac/Modules/macosmodule.c	2004-11-05 08:02:59.000000000 +0100
+++ Mac/Modules/macosmodule.c	2009-09-05 02:07:14.000000000 +0200
@@ -40,7 +40,7 @@
 
 typedef struct {
 	PyObject_HEAD
-	short fRefNum;
+	FSIORefNum fRefNum;
 	int isclosed;
 } rfobject;
 
@@ -54,7 +54,7 @@
 do_close(rfobject *self)
 {
 	if (self->isclosed ) return;
-	(void)FSClose(self->fRefNum);
+	(void)FSCloseFork(self->fRefNum);
 	self->isclosed = 1;
 }
 
@@ -68,6 +68,7 @@
 	long n;
 	PyObject *v;
 	OSErr err;
+	ByteCount n2;
 	
 	if (self->isclosed) {
 		PyErr_SetString(PyExc_ValueError, "Operation on closed file");
@@ -81,13 +82,13 @@
 	if (v == NULL)
 		return NULL;
 		
-	err = FSRead(self->fRefNum, &n, PyString_AsString(v));
+	err = FSReadFork(self->fRefNum, fsAtMark, 0, n, PyString_AsString(v), &n2);
 	if (err && err != eofErr) {
 		PyMac_Error(err);
 		Py_DECREF(v);
 		return NULL;
 	}
-	_PyString_Resize(&v, n);
+	_PyString_Resize(&v, n2);
 	return v;
 }
 
@@ -109,7 +110,7 @@
 	}
 	if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
 		return NULL;
-	err = FSWrite(self->fRefNum, &size, buffer);
+	err = FSWriteFork(self->fRefNum, fsAtMark, 0, size, buffer, NULL);
 	if (err) {
 		PyMac_Error(err);
 		return NULL;
@@ -126,47 +127,36 @@
 static PyObject *
 rf_seek(rfobject *self, PyObject *args)
 {
-	long amount, pos;
+	long amount;
 	int whence = SEEK_SET;
-	long eof;
+	int mode;
 	OSErr err;
 	
 	if (self->isclosed) {
 		PyErr_SetString(PyExc_ValueError, "Operation on closed file");
 		return NULL;
 	}
-	if (!PyArg_ParseTuple(args, "l|i", &amount, &whence))
+	if (!PyArg_ParseTuple(args, "l|i", &amount, &whence)) {
 		return NULL;
-	
-	if ((err = GetEOF(self->fRefNum, &eof)))
-		goto ioerr;
+	}
 	
 	switch (whence) {
 	case SEEK_CUR:
-		if ((err = GetFPos(self->fRefNum, &pos)))
-			goto ioerr; 
+		mode = fsFromMark;
 		break;
 	case SEEK_END:
-		pos = eof;
+		mode = fsFromLEOF;
 		break;
 	case SEEK_SET:
-		pos = 0;
+		mode = fsFromStart;
 		break;
 	default:
 		PyErr_BadArgument();
 		return NULL;
 	}
-	
-	pos += amount;
-	
-	/* Don't bother implementing seek past EOF */
-	if (pos > eof || pos < 0) {
-		PyErr_BadArgument();
-		return NULL;
-	}
-	
-	if ((err = SetFPos(self->fRefNum, fsFromStart, pos)) ) {
-ioerr:
+
+	err = FSSetForkPosition(self->fRefNum, mode, amount);
+	if (err != noErr) {
 		PyMac_Error(err);
 		return NULL;
 	}
@@ -182,7 +172,7 @@
 static PyObject *
 rf_tell(rfobject *self, PyObject *args)
 {
-	long where;
+	long long where;
 	OSErr err;
 	
 	if (self->isclosed) {
@@ -191,11 +181,13 @@
 	}
 	if (!PyArg_ParseTuple(args, ""))
 		return NULL;
-	if ((err = GetFPos(self->fRefNum, &where)) ) {
+
+	err = FSGetForkPosition(self->fRefNum, &where);
+	if (err != noErr) {
 		PyMac_Error(err);
 		return NULL;
 	}
-	return PyInt_FromLong(where);
+	return PyLong_FromLongLong(where);
 }
 
 static char rf_close__doc__[] = 
@@ -281,6 +273,7 @@
 	Rftype__doc__ /* Documentation string */
 };
 
+
 /* End of code for Resource fork objects */
 /* -------------------------------------------------------- */
 
@@ -292,17 +285,61 @@
 static PyObject *
 MacOS_GetCreatorAndType(PyObject *self, PyObject *args)
 {
-	FSSpec fss;
-	FInfo info;
 	PyObject *creator, *type, *res;
 	OSErr err;
-	
-	if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
+	FSRef ref;
+	FSCatalogInfo	cataloginfo;
+	FileInfo* finfo;
+
+	if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &ref)) {
+#ifndef __LP64__
+		/* This function is documented to take an FSSpec as well,
+		 * which only works in 32-bit mode.
+		 */
+		PyErr_Clear();
+		FSSpec fss;
+		FInfo info;
+
+		if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
+			return NULL;
+
+		if ((err = FSpGetFInfo(&fss, &info)) != noErr) {
+			return PyErr_Mac(MacOS_Error, err);
+		}
+		creator = PyString_FromStringAndSize(
+				(char *)&info.fdCreator, 4);
+		type = PyString_FromStringAndSize((char *)&info.fdType, 4);
+		res = Py_BuildValue("OO", creator, type);
+		Py_DECREF(creator);
+		Py_DECREF(type);
+		return res;
+#else	/* __LP64__ */
+		return NULL;
+#endif	/* __LP64__ */
+	}
+
+	err = FSGetCatalogInfo(&ref, 
+			kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo, 
+			NULL, NULL, NULL);
+	if (err != noErr) {
+		PyErr_Mac(MacOS_Error, err);
 		return NULL;
-	if ((err = FSpGetFInfo(&fss, &info)) != noErr)
-		return PyErr_Mac(MacOS_Error, err);
-	creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4);
-	type = PyString_FromStringAndSize((char *)&info.fdType, 4);
+	}
+
+	if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) {
+		/* Directory: doesn't have type/creator info.
+		 *
+		 * The specific error code is for backward compatibility with
+		 * earlier versions.
+		 */
+		PyErr_Mac(MacOS_Error, fnfErr);
+		return NULL;
+
+	} 
+	finfo = (FileInfo*)&(cataloginfo.finderInfo);
+	creator = PyString_FromStringAndSize((char*)&(finfo->fileCreator), 4);
+	type = PyString_FromStringAndSize((char*)&(finfo->fileType), 4);
+
 	res = Py_BuildValue("OO", creator, type);
 	Py_DECREF(creator);
 	Py_DECREF(type);
@@ -314,20 +351,66 @@
 static PyObject *
 MacOS_SetCreatorAndType(PyObject *self, PyObject *args)
 {
-	FSSpec fss;
 	ResType creator, type;
-	FInfo info;
+	FSRef ref;
+	FileInfo* finfo;
 	OSErr err;
-	
+	FSCatalogInfo	cataloginfo;
+
 	if (!PyArg_ParseTuple(args, "O&O&O&",
+			PyMac_GetFSRef, &ref, PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) {
+#ifndef __LP64__
+		/* Try to handle FSSpec arguments, for backward compatibility */
+		FSSpec fss;
+		FInfo info;
+
+		if (!PyArg_ParseTuple(args, "O&O&O&",
 			PyMac_GetFSSpec, &fss, PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
+			return NULL;
+
+		if ((err = FSpGetFInfo(&fss, &info)) != noErr)
+			return PyErr_Mac(MacOS_Error, err);
+
+		info.fdCreator = creator;
+		info.fdType = type;
+
+		if ((err = FSpSetFInfo(&fss, &info)) != noErr)
+			return PyErr_Mac(MacOS_Error, err);
+		Py_INCREF(Py_None);
+		return Py_None;
+#else /* __LP64__ */
 		return NULL;
-	if ((err = FSpGetFInfo(&fss, &info)) != noErr)
-		return PyErr_Mac(MacOS_Error, err);
-	info.fdCreator = creator;
-	info.fdType = type;
-	if ((err = FSpSetFInfo(&fss, &info)) != noErr)
-		return PyErr_Mac(MacOS_Error, err);
+#endif /* __LP64__ */
+	}
+	
+	err = FSGetCatalogInfo(&ref, 
+			kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo, 
+			NULL, NULL, NULL);
+	if (err != noErr) {
+		PyErr_Mac(MacOS_Error, err);
+		return NULL;
+	}
+
+	if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) {
+		/* Directory: doesn't have type/creator info.
+		 *
+		 * The specific error code is for backward compatibility with
+		 * earlier versions.
+		 */
+		PyErr_Mac(MacOS_Error, fnfErr);
+		return NULL;
+
+	} 
+	finfo = (FileInfo*)&(cataloginfo.finderInfo);
+	finfo->fileCreator = creator;
+	finfo->fileType = type;
+
+	err = FSSetCatalogInfo(&ref, kFSCatInfoFinderInfo, &cataloginfo);
+	if (err != noErr) {
+		PyErr_Mac(MacOS_Error, fnfErr);
+		return NULL;
+	}
+
 	Py_INCREF(Py_None);
 	return Py_None;
 }
@@ -375,6 +458,7 @@
 				/* And try again... */
 				h = GetResource('Estr', err);
 			}
+			Py_DECREF(m);
 		}
 	}
 	/*
@@ -398,6 +482,9 @@
 	return Py_BuildValue("s", buf);
 }
 
+
+#ifndef __LP64__
+
 static char splash_doc[] = "Open a splash-screen dialog by resource-id (0=close)";
 
 static PyObject *
@@ -416,7 +503,7 @@
 		return NULL;
 	olddialog = curdialog;
 	curdialog = NULL;
-		
+
 	if ( resid != -1 ) {
 		curdialog = GetNewDialog(resid, NULL, (WindowPtr)-1);
 		if ( curdialog ) {
@@ -451,11 +538,13 @@
 	
 	if (!PyArg_ParseTuple(args, "O&|O", PyMac_GetStr255, message, &object))
 		return NULL;
+	
 	DebugStr(message);
 	Py_INCREF(Py_None);
 	return Py_None;
 }
 
+
 static char SysBeep_doc[] = "BEEEEEP!!!";
 
 static PyObject *
@@ -470,6 +559,8 @@
 	return Py_None;
 }
 
+#endif /* __LP64__ */
+
 static char WMAvailable_doc[] = 
 	"True if this process can interact with the display."
 	"Will foreground the application on the first call as a side-effect."
@@ -529,51 +620,37 @@
 {
 	OSErr err;
 	char *mode = "r";
-	FSSpec fss;
-	SignedByte permission = 1;
+	FSRef ref;
+	SInt8 permission = fsRdPerm;
 	rfobject *fp;
+	HFSUniStr255 name;
 		
-	if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSSpec, &fss, &mode))
+	if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSRef, &ref, &mode))
 		return NULL;
 	while (*mode) {
 		switch (*mode++) {
 		case '*': break;
-		case 'r': permission = 1; break;
-		case 'w': permission = 2; break;
+		case 'r': permission = fsRdPerm; break;
+		case 'w': permission = fsWrPerm; break;
 		case 'b': break;
 		default:
 			PyErr_BadArgument();
 			return NULL;
 		}
 	}
+
+	err = FSGetResourceForkName(&name);
+	if (err != noErr) {
+		PyMac_Error(err);
+		return NULL;
+	}
 	
 	if ( (fp = newrfobject()) == NULL )
 		return NULL;
-		
-	err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
+
 	
-	if ( err == fnfErr ) {
-		/* In stead of doing complicated things here to get creator/type
-		** correct we let the standard i/o library handle it
-		*/
-		FILE *tfp;
-		char pathname[PATHNAMELEN];
-		
-		if ( (err=PyMac_GetFullPathname(&fss, pathname, PATHNAMELEN)) ) {
-			PyMac_Error(err);
-			Py_DECREF(fp);
-			return NULL;
-		}
-		
-		if ( (tfp = fopen(pathname, "w")) == NULL ) {
-			PyMac_Error(fnfErr); /* What else... */
-			Py_DECREF(fp);
-			return NULL;
-		}
-		fclose(tfp);
-		err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
-	}
-	if ( err ) {
+	err = FSOpenFork(&ref, name.length, name.unicode, permission, &fp->fRefNum);
+	if (err != noErr) {
 		Py_DECREF(fp);
 		PyMac_Error(err);
 		return NULL;
@@ -583,15 +660,18 @@
 }
 
 
+
 static PyMethodDef MacOS_Methods[] = {
 	{"GetCreatorAndType",		MacOS_GetCreatorAndType, 1,	getcrtp_doc},
 	{"SetCreatorAndType",		MacOS_SetCreatorAndType, 1,	setcrtp_doc},
 	{"GetErrorString",		MacOS_GetErrorString,	1,	geterr_doc},
 	{"openrf",			MacOS_openrf, 		1, 	openrf_doc},
+#ifndef __LP64__
 	{"splash",			MacOS_splash,		1, 	splash_doc},
 	{"DebugStr",			MacOS_DebugStr,		1,	DebugStr_doc},
-	{"GetTicks",			MacOS_GetTicks,		1,	GetTicks_doc},
 	{"SysBeep",			MacOS_SysBeep,		1,	SysBeep_doc},
+#endif /* __LP64__ */
+	{"GetTicks",			MacOS_GetTicks,		1,	GetTicks_doc},
 	{"WMAvailable",			MacOS_WMAvailable,		1,	WMAvailable_doc},
 	{NULL,				NULL}		 /* Sentinel */
 };
