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

Skip to content

Commit 97ce361

Browse files
committed
- Use PyErr_Mac for nicer exceptions
- Added methods AsString() and AsBytes() to resource objects, so you can get at the contents of resource items. - Ported to CodeWarrior (mainly ProcPtr/UniversalProcPtr stuff)
1 parent 0c097ea commit 97ce361

1 file changed

Lines changed: 57 additions & 25 deletions

File tree

Mac/Modules/macosmodule.c

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,27 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2525
/* Macintosh OS-specific interface */
2626

2727
#include "Python.h"
28+
#include "macglue.h"
2829

2930
#include <stdio.h>
3031
#include <string.h>
3132
#include <errno.h>
3233

3334
#include <OSUtils.h> /* for Set(Current)A5 */
3435
#include <Resources.h>
36+
#include <Memory.h>
3537
#include <Sound.h>
3638

39+
#ifndef __MWERKS__
40+
#define SndCallBackUPP ProcPtr
41+
#define NewSndCallBackProc(x) (x)
42+
#endif
43+
3744
/*----------------------------------------------------------------------*/
3845
/* General tools */
3946

4047
static PyObject *MacOS_Error; /* Exception MacOS.Error */
4148

42-
/* Set a MAC-specific error from errno, and return NULL; return None if no error */
43-
static PyObject *
44-
Err(OSErr err)
45-
{
46-
char buf[100];
47-
PyObject *v;
48-
if (err == 0) {
49-
Py_INCREF(Py_None);
50-
return Py_None;
51-
}
52-
sprintf(buf, "Mac OS error code %d", (int)err);
53-
v = Py_BuildValue("(is)", (int)err, buf);
54-
PyErr_SetObject(MacOS_Error, v);
55-
Py_DECREF(v);
56-
return NULL;
57-
}
58-
5949
/* Convert a ResType argument */
6050
static int
6151
GetOSType(PyObject *v, ResType *pr)
@@ -101,7 +91,7 @@ Rsrc_FromHandle(Handle h)
10191
{
10292
RsrcObject *r;
10393
if (h == NULL)
104-
return (RsrcObject *)Err(ResError());
94+
return (RsrcObject *)PyErr_Mac(MacOS_Error, (int)ResError());
10595
r = PyObject_NEW(RsrcObject, &RsrcType);
10696
if (r != NULL)
10797
r->h = h;
@@ -111,6 +101,7 @@ Rsrc_FromHandle(Handle h)
111101
static void
112102
Rsrc_Dealloc(RsrcObject *r)
113103
{
104+
/* XXXX Note by Jack: shouldn't we ReleaseResource here? */
114105
PyMem_DEL(r);
115106
}
116107

@@ -127,8 +118,41 @@ Rsrc_GetResInfo(RsrcObject *r, PyObject *args)
127118
(int)id, (char *)&type, 4, name+1, (int)name[0]);
128119
}
129120

121+
static PyObject *
122+
Rsrc_AsBytes(RsrcObject *r, PyObject *args)
123+
{
124+
long len;
125+
PyObject *rv;
126+
char *cp;
127+
128+
if (!PyArg_Parse(args, "(l)", &len))
129+
return NULL;
130+
HLock(r->h);
131+
cp = (char *)*r->h;
132+
rv = PyString_FromStringAndSize(cp, len);
133+
HUnlock(r->h);
134+
return rv;
135+
}
136+
137+
static PyObject *
138+
Rsrc_AsString(RsrcObject *r, PyObject *args)
139+
{
140+
PyObject *rv;
141+
unsigned char *cp;
142+
143+
if (!PyArg_Parse(args, "()"))
144+
return NULL;
145+
HLock(r->h);
146+
cp = (unsigned char *)*r->h;
147+
rv = PyString_FromStringAndSize((char *)cp+1, cp[0]);
148+
HUnlock(r->h);
149+
return rv;
150+
}
151+
130152
static PyMethodDef Rsrc_Methods[] = {
131153
{"GetResInfo", (PyCFunction)Rsrc_GetResInfo, 1},
154+
{"AsString", (PyCFunction)Rsrc_AsString, 1},
155+
{"AsBytes", (PyCFunction)Rsrc_AsBytes, 1},
132156
{NULL, NULL} /* Sentinel */
133157
};
134158

@@ -302,7 +326,7 @@ SndCh_SndDoCommand(SndChObject *s, PyObject *args)
302326
if (!SndCh_OK(s))
303327
return NULL;
304328
err = SndDoCommand(s->chan, &c, noWait);
305-
return Err(err);
329+
return PyErr_Mac(MacOS_Error, (int)err);
306330
}
307331

308332
static PyObject *
@@ -315,7 +339,7 @@ SndCh_SndDoImmediate(SndChObject *s, PyObject *args)
315339
if (!SndCh_OK(s))
316340
return NULL;
317341
err = SndDoImmediate(s->chan, &c);
318-
return Err(err);
342+
return PyErr_Mac(MacOS_Error, (int)err);
319343
}
320344

321345
static PyMethodDef SndCh_Methods[] = {
@@ -380,11 +404,19 @@ MySafeCallback(arg)
380404
}
381405

382406
static pascal void
407+
#ifdef __MWERKS__
408+
MyUserRoutine(SndChannelPtr chan, SndCommand *cmd)
409+
#else
383410
MyUserRoutine(SndChannelPtr chan, SndCommand cmd)
411+
#endif
384412
{
385413
cbinfo *p = (cbinfo *)chan->userInfo;
386414
long A5 = SetA5(p->A5);
415+
#ifdef __MWERKS__
416+
p->cmd = *cmd;
417+
#else
387418
p->cmd = cmd;
419+
#endif
388420
Py_AddPendingCall(MySafeCallback, (void *)p);
389421
SetA5(A5);
390422
}
@@ -397,7 +429,7 @@ MacOS_SndNewChannel(PyObject *self, PyObject *args)
397429
long init = 0;
398430
PyObject *callback = NULL;
399431
cbinfo *p = NULL;
400-
SndCallBackProcPtr userroutine = 0;
432+
SndCallBackUPP userroutine = 0;
401433
OSErr err;
402434
PyObject *res;
403435
if (!PyArg_Parse(args, "(h)", &synth)) {
@@ -414,14 +446,14 @@ MacOS_SndNewChannel(PyObject *self, PyObject *args)
414446
return PyErr_NoMemory();
415447
p->A5 = SetCurrentA5();
416448
p->callback = callback;
417-
userroutine = MyUserRoutine;
449+
userroutine = NewSndCallBackProc(MyUserRoutine);
418450
}
419451
chan = NULL;
420452
err = SndNewChannel(&chan, synth, init, userroutine);
421453
if (err) {
422454
if (p)
423455
DEL(p);
424-
return Err(err);
456+
return PyErr_Mac(MacOS_Error, (int)err);
425457
}
426458
res = (PyObject *)SndCh_FromSndChannelPtr(chan);
427459
if (res == NULL) {
@@ -443,7 +475,7 @@ MacOS_SndPlay(PyObject *self, PyObject *args)
443475
if (!PyArg_Parse(args, "(O!)", &RsrcType, &r))
444476
return NULL;
445477
err = SndPlay((SndChannelPtr)NULL, r->h, 0);
446-
return Err(err);
478+
return PyErr_Mac(MacOS_Error, (int)err);
447479
}
448480

449481
static PyObject *
@@ -456,7 +488,7 @@ MacOS_SndControl(PyObject *self, PyObject *args)
456488
return NULL;
457489
err = SndControl(id, &c);
458490
if (err)
459-
return Err(err);
491+
return PyErr_Mac(MacOS_Error, (int)err);
460492
return Py_BuildValue("(hhl)", c.cmd, c.param1, c.param2);
461493
}
462494

0 commit comments

Comments
 (0)