Skip to content

Commit e791c2e

Browse files
committed
Used new getargs interface; added {Get,Set}FileInfo.
1 parent 9954699 commit e791c2e

File tree

1 file changed

+77
-44
lines changed

1 file changed

+77
-44
lines changed

Mac/Modules/macosmodule.c

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Rsrc_GetResInfo(RsrcObject *r, PyObject *args)
111111
short id;
112112
ResType type;
113113
Str255 name;
114-
if (!PyArg_Parse(args, "()"))
114+
if (!PyArg_ParseTuple(args, ""))
115115
return NULL;
116116
GetResInfo(r->h, &id, &type, name);
117117
return Py_BuildValue("(is#s#)",
@@ -125,7 +125,7 @@ Rsrc_AsBytes(RsrcObject *r, PyObject *args)
125125
PyObject *rv;
126126
char *cp;
127127

128-
if (!PyArg_Parse(args, "(l)", &len))
128+
if (!PyArg_ParseTuple(args, "l", &len))
129129
return NULL;
130130
HLock(r->h);
131131
cp = (char *)*r->h;
@@ -140,7 +140,7 @@ Rsrc_AsString(RsrcObject *r, PyObject *args)
140140
PyObject *rv;
141141
unsigned char *cp;
142142

143-
if (!PyArg_Parse(args, "()"))
143+
if (!PyArg_ParseTuple(args, ""))
144144
return NULL;
145145
HLock(r->h);
146146
cp = (unsigned char *)*r->h;
@@ -187,7 +187,7 @@ MacOS_GetResource(PyObject *self, PyObject *args)
187187
ResType rt;
188188
int id;
189189
Handle h;
190-
if (!PyArg_Parse(args, "(O&i)", GetOSType, &rt, &id))
190+
if (!PyArg_ParseTuple(args, "O&i", GetOSType, &rt, &id))
191191
return NULL;
192192
h = GetResource(rt, id);
193193
return (PyObject *)Rsrc_FromHandle(h);
@@ -199,12 +199,65 @@ MacOS_GetNamedResource(PyObject *self, PyObject *args)
199199
ResType rt;
200200
Str255 name;
201201
Handle h;
202-
if (!PyArg_Parse(args, "(O&O&)", GetOSType, &rt, GetStr255, &name))
202+
if (!PyArg_ParseTuple(args, "O&O&", GetOSType, &rt, GetStr255, &name))
203203
return NULL;
204204
h = GetNamedResource(rt, name);
205205
return (PyObject *)Rsrc_FromHandle(h);
206206
}
207207

208+
/*----------------------------------------------------------------------*/
209+
/* Miscellaneous File System Operations */
210+
211+
static PyObject *
212+
MacOS_GetFileType(PyObject *self, PyObject *args)
213+
{
214+
Str255 name;
215+
FInfo info;
216+
PyObject *type, *creator, *res;
217+
OSErr err;
218+
219+
if (!PyArg_ParseTuple(args, "O&", GetStr255, &name))
220+
return NULL;
221+
if ((err = GetFInfo(name, 0, &info)) != noErr) {
222+
errno = err;
223+
PyErr_SetFromErrno(MacOS_Error);
224+
return NULL;
225+
}
226+
type = PyString_FromStringAndSize((char *)&info.fdType, 4);
227+
creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4);
228+
res = Py_BuildValue("OO", type, creator);
229+
DECREF(type);
230+
DECREF(creator);
231+
return res;
232+
}
233+
234+
static PyObject *
235+
MacOS_SetFileType(PyObject *self, PyObject *args)
236+
{
237+
Str255 name;
238+
ResType type, creator;
239+
FInfo info;
240+
OSErr err;
241+
242+
if (!PyArg_ParseTuple(args, "O&O&O&",
243+
GetStr255, &name, GetOSType, &type, GetOSType, &creator))
244+
return NULL;
245+
if ((err = GetFInfo(name, 0, &info)) != noErr) {
246+
errno = err;
247+
PyErr_SetFromErrno(MacOS_Error);
248+
return NULL;
249+
}
250+
info.fdType = type;
251+
info.fdCreator = creator;
252+
if ((err = SetFInfo(name, 0, &info)) != noErr) {
253+
errno = err;
254+
err_errno(MacOS_Error);
255+
return NULL;
256+
}
257+
Py_INCREF(Py_None);
258+
return Py_None;
259+
}
260+
208261
/*----------------------------------------------------------------------*/
209262
/* SoundChannel objects */
210263

@@ -215,23 +268,13 @@ GetSndCommand(PyObject *v, SndCommand *pc)
215268
int len;
216269
pc->param1 = 0;
217270
pc->param2 = 0;
218-
if (PyArg_Parse(v, "h", &pc->cmd))
219-
return 1;
220-
PyErr_Clear();
221-
if (PyArg_Parse(v, "(h)", &pc->cmd))
222-
return 1;
223-
PyErr_Clear();
224-
if (PyArg_Parse(v, "(hh)", &pc->cmd, &pc->param1))
225-
return 1;
226-
PyErr_Clear();
227-
if (PyArg_Parse(v, "(hhl)",
228-
&pc->cmd, &pc->param1, &pc->param2))
229-
return 1;
230-
PyErr_Clear();
231-
if (PyArg_Parse(v, "(hhs#);SndCommand arg must be 1-3 ints or 2 ints + string",
232-
&pc->cmd, &pc->param1, &pc->param2, &len))
233-
return 1;
234-
return 0;
271+
if (PyTuple_Check(v)) {
272+
if (PyArg_ParseTuple(v, "h|hl", &pc->cmd, &pc->param1, &pc->param2))
273+
return 1;
274+
PyErr_Clear();
275+
return PyArg_ParseTuple(v, "hhs#", &pc->cmd, &pc->param1, &pc->param2, &len);
276+
}
277+
return PyArg_Parse(v, "h", &pc->cmd);
235278
}
236279

237280
typedef struct {
@@ -277,7 +320,7 @@ SndCh_DisposeChannel(SndChObject *s, PyObject *args)
277320
{
278321
int quitNow = 1;
279322
if (PyTuple_Size(args) > 0) {
280-
if (!PyArg_Parse(args, "(i)", &quitNow))
323+
if (!PyArg_ParseTuple(args, "i", &quitNow))
281324
return NULL;
282325
}
283326
SndCh_Cleanup(s, quitNow);
@@ -300,11 +343,8 @@ SndCh_SndPlay(SndChObject *s, PyObject *args)
300343
{
301344
RsrcObject *r;
302345
int async = 0;
303-
if (!PyArg_Parse(args, "(O!)", RsrcType, &r)) {
304-
PyErr_Clear();
305-
if (!PyArg_Parse(args, "(O&i)", RsrcType, &r, &async))
306-
return NULL;
307-
}
346+
if (!PyArg_ParseTuple(args, "O!|i", RsrcType, &r, &async))
347+
return NULL;
308348
if (!SndCh_OK(s))
309349
return NULL;
310350
SndPlay(s->chan, r->h, async);
@@ -318,11 +358,8 @@ SndCh_SndDoCommand(SndChObject *s, PyObject *args)
318358
SndCommand c;
319359
int noWait = 0;
320360
OSErr err;
321-
if (!PyArg_Parse(args, "(O&)", GetSndCommand, &c)) {
322-
PyErr_Clear();
323-
if (!PyArg_Parse(args, "(O&i)", GetSndCommand, &c, &noWait))
324-
return NULL;
325-
}
361+
if (!PyArg_ParseTuple(args, "O&|i", GetSndCommand, &c, &noWait))
362+
return NULL;
326363
if (!SndCh_OK(s))
327364
return NULL;
328365
err = SndDoCommand(s->chan, &c, noWait);
@@ -334,7 +371,7 @@ SndCh_SndDoImmediate(SndChObject *s, PyObject *args)
334371
{
335372
SndCommand c;
336373
OSErr err;
337-
if (!PyArg_Parse(args, "(O&)", GetSndCommand, &c))
374+
if (!PyArg_ParseTuple(args, "O&", GetSndCommand, &c))
338375
return 0;
339376
if (!SndCh_OK(s))
340377
return NULL;
@@ -432,14 +469,8 @@ MacOS_SndNewChannel(PyObject *self, PyObject *args)
432469
SndCallBackUPP userroutine = 0;
433470
OSErr err;
434471
PyObject *res;
435-
if (!PyArg_Parse(args, "(h)", &synth)) {
436-
PyErr_Clear();
437-
if (!PyArg_Parse(args, "(hl)", &synth, &init)) {
438-
PyErr_Clear();
439-
if (!PyArg_Parse(args, "(hlO)", &synth, &init, &callback))
440-
return NULL;
441-
}
442-
}
472+
if (!PyArg_ParseTuple(args, "h|lO", &synth, &init, &callback))
473+
return NULL;
443474
if (callback != NULL) {
444475
p = NEW(cbinfo, 1);
445476
if (p == NULL)
@@ -472,7 +503,7 @@ MacOS_SndPlay(PyObject *self, PyObject *args)
472503
{
473504
RsrcObject *r;
474505
OSErr err;
475-
if (!PyArg_Parse(args, "(O!)", &RsrcType, &r))
506+
if (!PyArg_ParseTuple(args, "O!", &RsrcType, &r))
476507
return NULL;
477508
err = SndPlay((SndChannelPtr)NULL, r->h, 0);
478509
return PyErr_Mac(MacOS_Error, (int)err);
@@ -484,7 +515,7 @@ MacOS_SndControl(PyObject *self, PyObject *args)
484515
int id;
485516
SndCommand c;
486517
OSErr err;
487-
if (!PyArg_Parse(args, "(iO&)", &id, GetSndCommand, &c))
518+
if (!PyArg_ParseTuple(args, "iO&", &id, GetSndCommand, &c))
488519
return NULL;
489520
err = SndControl(id, &c);
490521
if (err)
@@ -495,6 +526,8 @@ MacOS_SndControl(PyObject *self, PyObject *args)
495526
static PyMethodDef MacOS_Methods[] = {
496527
{"GetResource", MacOS_GetResource, 1},
497528
{"GetNamedResource", MacOS_GetNamedResource, 1},
529+
{"GetFileType", MacOS_GetFileType, 1},
530+
{"SetFileType", MacOS_SetFileType, 1},
498531
{"SndNewChannel", MacOS_SndNewChannel, 1},
499532
{"SndPlay", MacOS_SndPlay, 1},
500533
{"SndControl", MacOS_SndControl, 1},

0 commit comments

Comments
 (0)