Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e55beef

Browse files
committed
- Added as_pathname and as_tuple methods
- Added access to the "data" attribute - Fixed the FSRef tp_init routine to accept pathnames on OSX - Changed the FSSpec tp_repr to return something resembling what macfs returns.
1 parent ff38505 commit e55beef

2 files changed

Lines changed: 205 additions & 19 deletions

File tree

Mac/Modules/file/_Filemodule.c

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,25 @@ static PyMethodDef Alias_methods[] = {
291291
{NULL, NULL, 0}
292292
};
293293

294-
#define Alias_getsetlist NULL
294+
static PyObject *Alias_get_data(AliasObject *self, void *closure)
295+
{
296+
int size;
297+
PyObject *rv;
298+
299+
size = GetHandleSize((Handle)self->ob_itself);
300+
HLock((Handle)self->ob_itself);
301+
rv = PyString_FromStringAndSize(*(Handle)self->ob_itself, size);
302+
HUnlock((Handle)self->ob_itself);
303+
return rv;
304+
305+
}
306+
307+
#define Alias_set_data NULL
308+
309+
static PyGetSetDef Alias_getsetlist[] = {
310+
{"data", (getter)Alias_get_data, (setter)Alias_set_data, "Raw data of the alias object"},
311+
{NULL, NULL, NULL, NULL},
312+
};
295313

296314

297315
#define Alias_compare NULL
@@ -329,7 +347,7 @@ static PyObject *Alias_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds
329347
PyTypeObject Alias_Type = {
330348
PyObject_HEAD_INIT(NULL)
331349
0, /*ob_size*/
332-
"_File.Alias", /*tp_name*/
350+
"Carbon.File.Alias", /*tp_name*/
333351
sizeof(AliasObject), /*tp_basicsize*/
334352
0, /*tp_itemsize*/
335353
/* methods */
@@ -691,6 +709,37 @@ static PyObject *FSSpec_UpdateAlias(FSSpecObject *_self, PyObject *_args)
691709
return _res;
692710
}
693711

712+
static PyObject *FSSpec_as_pathname(FSSpecObject *_self, PyObject *_args)
713+
{
714+
PyObject *_res = NULL;
715+
716+
char strbuf[1024];
717+
OSErr err;
718+
719+
if (!PyArg_ParseTuple(_args, ""))
720+
return NULL;
721+
err = PyMac_GetFullPathname(&_self->ob_itself, strbuf, sizeof(strbuf));
722+
if ( err ) {
723+
PyMac_Error(err);
724+
return NULL;
725+
}
726+
_res = PyString_FromString(strbuf);
727+
return _res;
728+
729+
}
730+
731+
static PyObject *FSSpec_as_tuple(FSSpecObject *_self, PyObject *_args)
732+
{
733+
PyObject *_res = NULL;
734+
735+
if (!PyArg_ParseTuple(_args, ""))
736+
return NULL;
737+
_res = Py_BuildValue("(iis#)", _self->ob_itself.vRefNum, _self->ob_itself.parID,
738+
&_self->ob_itself.name[1], _self->ob_itself.name[0]);
739+
return _res;
740+
741+
}
742+
694743
static PyMethodDef FSSpec_methods[] = {
695744
{"FSpOpenDF", (PyCFunction)FSSpec_FSpOpenDF, 1,
696745
PyDoc_STR("(SInt8 permission) -> (short refNum)")},
@@ -726,15 +775,38 @@ static PyMethodDef FSSpec_methods[] = {
726775
PyDoc_STR("() -> (Boolean aliasFileFlag, Boolean folderFlag)")},
727776
{"UpdateAlias", (PyCFunction)FSSpec_UpdateAlias, 1,
728777
PyDoc_STR("(FSSpec target, AliasHandle alias) -> (Boolean wasChanged)")},
778+
{"as_pathname", (PyCFunction)FSSpec_as_pathname, 1,
779+
PyDoc_STR("() -> string")},
780+
{"as_tuple", (PyCFunction)FSSpec_as_tuple, 1,
781+
PyDoc_STR("() -> (vRefNum, dirID, name)")},
729782
{NULL, NULL, 0}
730783
};
731784

732-
#define FSSpec_getsetlist NULL
785+
static PyObject *FSSpec_get_data(FSSpecObject *self, void *closure)
786+
{
787+
return PyString_FromStringAndSize((char *)&self->ob_itself, sizeof(self->ob_itself));
788+
}
789+
790+
#define FSSpec_set_data NULL
791+
792+
static PyGetSetDef FSSpec_getsetlist[] = {
793+
{"data", (getter)FSSpec_get_data, (setter)FSSpec_set_data, "Raw data of the FSSpec object"},
794+
{NULL, NULL, NULL, NULL},
795+
};
733796

734797

735798
#define FSSpec_compare NULL
736799

737-
#define FSSpec_repr NULL
800+
static PyObject * FSSpec_repr(FSSpecObject *self)
801+
{
802+
char buf[512];
803+
PyOS_snprintf(buf, sizeof(buf), "%s((%d, %ld, '%.*s'))",
804+
self->ob_type->tp_name,
805+
self->ob_itself.vRefNum,
806+
self->ob_itself.parID,
807+
self->ob_itself.name[0], self->ob_itself.name+1);
808+
return PyString_FromString(buf);
809+
}
738810

739811
#define FSSpec_hash NULL
740812
static int FSSpec_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
@@ -765,7 +837,7 @@ static PyObject *FSSpec_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwd
765837
PyTypeObject FSSpec_Type = {
766838
PyObject_HEAD_INIT(NULL)
767839
0, /*ob_size*/
768-
"_File.FSSpec", /*tp_name*/
840+
"Carbon.File.FSSpec", /*tp_name*/
769841
sizeof(FSSpecObject), /*tp_basicsize*/
770842
0, /*tp_itemsize*/
771843
/* methods */
@@ -1122,6 +1194,8 @@ static PyObject *FSRef_FSRefMakePath(FSRefObject *_self, PyObject *_args)
11221194
UInt8 path[MAXPATHNAME];
11231195
UInt32 maxPathSize = MAXPATHNAME;
11241196

1197+
if (!PyArg_ParseTuple(_args, ""))
1198+
return NULL;
11251199
_err = FSRefMakePath(&_self->ob_itself,
11261200
path,
11271201
maxPathSize);
@@ -1131,6 +1205,15 @@ static PyObject *FSRef_FSRefMakePath(FSRefObject *_self, PyObject *_args)
11311205

11321206
}
11331207

1208+
static PyObject *FSRef_as_pathname(FSRefObject *_self, PyObject *_args)
1209+
{
1210+
PyObject *_res = NULL;
1211+
1212+
_res = FSRef_FSRefMakePath(_self, _args);
1213+
return _res;
1214+
1215+
}
1216+
11341217
static PyMethodDef FSRef_methods[] = {
11351218
{"FSMakeFSRefUnicode", (PyCFunction)FSRef_FSMakeFSRefUnicode, 1,
11361219
PyDoc_STR("(Buffer nameLength, TextEncoding textEncodingHint) -> (FSRef newRef)")},
@@ -1165,10 +1248,22 @@ static PyMethodDef FSRef_methods[] = {
11651248
PyDoc_STR("(FSRef target, AliasHandle alias) -> (Boolean wasChanged)")},
11661249
{"FSRefMakePath", (PyCFunction)FSRef_FSRefMakePath, 1,
11671250
PyDoc_STR("() -> string")},
1251+
{"as_pathname", (PyCFunction)FSRef_as_pathname, 1,
1252+
PyDoc_STR("() -> string")},
11681253
{NULL, NULL, 0}
11691254
};
11701255

1171-
#define FSRef_getsetlist NULL
1256+
static PyObject *FSRef_get_data(FSRefObject *self, void *closure)
1257+
{
1258+
return PyString_FromStringAndSize((char *)&self->ob_itself, sizeof(self->ob_itself));
1259+
}
1260+
1261+
#define FSRef_set_data NULL
1262+
1263+
static PyGetSetDef FSRef_getsetlist[] = {
1264+
{"data", (getter)FSRef_get_data, (setter)FSRef_set_data, "Raw data of the FSRef object"},
1265+
{NULL, NULL, NULL, NULL},
1266+
};
11721267

11731268

11741269
#define FSRef_compare NULL
@@ -1204,7 +1299,7 @@ static PyObject *FSRef_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds
12041299
PyTypeObject FSRef_Type = {
12051300
PyObject_HEAD_INIT(NULL)
12061301
0, /*ob_size*/
1207-
"_File.FSRef", /*tp_name*/
1302+
"Carbon.File.FSRef", /*tp_name*/
12081303
sizeof(FSRefObject), /*tp_basicsize*/
12091304
0, /*tp_itemsize*/
12101305
/* methods */
@@ -2318,17 +2413,18 @@ myPyMac_GetFSSpec(PyObject *v, FSSpec *spec)
23182413
static int
23192414
myPyMac_GetFSRef(PyObject *v, FSRef *fsr)
23202415
{
2416+
OSStatus err;
2417+
23212418
if (FSRef_Check(v)) {
23222419
*fsr = ((FSRefObject *)v)->ob_itself;
23232420
return 1;
23242421
}
23252422

2326-
#if !TARGET_API_MAC_OSX
2423+
#if TARGET_API_MAC_OSX
23272424
/* On OSX we now try a pathname */
2328-
if ( PyString_Check(args) ) {
2329-
OSStatus err;
2425+
if ( PyString_Check(v) ) {
23302426
if ( (err=FSPathMakeRef(PyString_AsString(v), fsr, NULL)) ) {
2331-
PyErr_Mac(ErrorObject, err);
2427+
PyMac_Error(err);
23322428
return 0;
23332429
}
23342430
return 1;
@@ -2337,15 +2433,17 @@ myPyMac_GetFSRef(PyObject *v, FSRef *fsr)
23372433
#endif
23382434
/* Otherwise we try to go via an FSSpec */
23392435
if (FSSpec_Check(v)) {
2340-
if (FSpMakeFSRef(&((FSSpecObject *)v)->ob_itself, fsr))
2436+
if ((err=FSpMakeFSRef(&((FSSpecObject *)v)->ob_itself, fsr)) == 0)
23412437
return 1;
2438+
PyMac_Error(err);
23422439
return 0;
23432440
}
23442441
PyErr_SetString(PyExc_TypeError, "FSRef, FSSpec or pathname required");
23452442
return 0;
23462443
}
23472444

23482445

2446+
23492447
void init_File(void)
23502448
{
23512449
PyObject *m;

0 commit comments

Comments
 (0)