Import Upstream version 2.7.18

This commit is contained in:
geos_one
2025-08-15 16:28:06 +02:00
commit ba1f69ab39
4521 changed files with 1778434 additions and 0 deletions

View File

@@ -0,0 +1,512 @@
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
#include "Python.h"
#include "pymactoolbox.h"
#include <Sound.h>
#pragma options align=mac68k
struct SampleRateAvailable_arg {
short numrates;
Handle rates;
};
struct SampleSizeAvailable_arg {
short numsizes;
Handle sizes;
};
#pragma options align=reset
static PyObject *ErrorObject;
/* Convert Python object to unsigned Fixed */
static int
PyMac_GetUFixed(PyObject *v, Fixed *f)
{
double d;
unsigned long uns;
if( !PyArg_Parse(v, "d", &d))
return 0;
uns = (unsigned long)(d * 0x10000);
*f = (Fixed)uns;
return 1;
}
/* Convert a Point to a Python object */
static PyObject *
PyMac_BuildUFixed(Fixed f)
{
double d;
unsigned long funs;
funs = (unsigned long)f;
d = funs;
d = d / 0x10000;
return Py_BuildValue("d", d);
}
/* ----------------------------------------------------- */
static char sndih_getChannelAvailable__doc__[] =
""
;
static PyObject *
sndih_getChannelAvailable(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short nchannel;
OSErr err;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siChannelAvailable, (Ptr)&nchannel)) != noErr )
return PyMac_Error(err);
return Py_BuildValue("h", nchannel);
}
static char sndih_getNumberChannels__doc__[] =
""
;
static PyObject *
sndih_getNumberChannels(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short nchannel;
OSErr err;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siNumberChannels, (Ptr)&nchannel)) != noErr )
return PyMac_Error(err);
return Py_BuildValue("h", nchannel);
}
static char sndih_setNumberChannels__doc__[] =
""
;
static PyObject *
sndih_setNumberChannels(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short nchannel;
OSErr err;
if (!PyArg_ParseTuple(args, "lh", &inRefNum, &nchannel))
return NULL;
if( (err=SPBSetDeviceInfo(inRefNum, siNumberChannels, (Ptr)&nchannel)) != noErr )
return PyMac_Error(err);
Py_INCREF(Py_None);
return Py_None;
}
static char sndih_getContinuous__doc__[] =
""
;
static PyObject *
sndih_getContinuous(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short onoff;
OSErr err;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siContinuous, (Ptr)&onoff)) != noErr )
return PyMac_Error(err);
return Py_BuildValue("h", onoff);
}
static char sndih_setContinuous__doc__[] =
""
;
static PyObject *
sndih_setContinuous(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short onoff;
OSErr err;
if (!PyArg_ParseTuple(args, "lh", &inRefNum, &onoff))
return NULL;
if( (err=SPBSetDeviceInfo(inRefNum, siContinuous, (Ptr)&onoff)) != noErr )
return PyMac_Error(err);
Py_INCREF(Py_None);
return Py_None;
}
static char sndih_getInputSourceNames__doc__[] =
""
;
static PyObject *
sndih_getInputSourceNames(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
Handle names;
OSErr err;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siInputSourceNames, (Ptr)&names)) != noErr )
return PyMac_Error(err);
return Py_BuildValue("O&", ResObj_New, names);
}
static char sndih_getInputSource__doc__[] =
""
;
static PyObject *
sndih_getInputSource(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short source;
OSErr err;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siInputSource, (Ptr)&source)) != noErr )
return PyMac_Error(err);
return Py_BuildValue("h", source);
}
static char sndih_setInputSource__doc__[] =
""
;
static PyObject *
sndih_setInputSource(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short source;
OSErr err;
if (!PyArg_ParseTuple(args, "lh", &inRefNum, &source))
return NULL;
if( (err=SPBSetDeviceInfo(inRefNum, siInputSource, (Ptr)&source)) != noErr )
return PyMac_Error(err);
Py_INCREF(Py_None);
return Py_None;
}
static char sndih_getPlayThruOnOff__doc__[] =
""
;
static PyObject *
sndih_getPlayThruOnOff(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short onoff;
OSErr err;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siPlayThruOnOff, (Ptr)&onoff)) != noErr )
return PyMac_Error(err);
return Py_BuildValue("h", onoff);
}
static char sndih_setPlayThruOnOff__doc__[] =
""
;
static PyObject *
sndih_setPlayThruOnOff(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short onoff;
OSErr err;
if (!PyArg_ParseTuple(args, "lh", &inRefNum, &onoff))
return NULL;
if( (err=SPBSetDeviceInfo(inRefNum, siPlayThruOnOff, (Ptr)&onoff)) != noErr )
return PyMac_Error(err);
Py_INCREF(Py_None);
return Py_None;
}
static char sndih_getSampleRate__doc__[] =
""
;
static PyObject *
sndih_getSampleRate(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
Fixed sample_rate;
OSErr err;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siSampleRate, (Ptr)&sample_rate)) != noErr )
return PyMac_Error(err);
return Py_BuildValue("O&", PyMac_BuildUFixed, sample_rate);
}
static char sndih_setSampleRate__doc__[] =
""
;
static PyObject *
sndih_setSampleRate(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
Fixed sample_rate;
OSErr err;
if (!PyArg_ParseTuple(args, "lO&", &inRefNum, PyMac_GetUFixed, &sample_rate))
return NULL;
if( (err=SPBSetDeviceInfo(inRefNum, siSampleRate, (Ptr)&sample_rate)) != noErr )
return PyMac_Error(err);
Py_INCREF(Py_None);
return Py_None;
}
static char sndih_getSampleSize__doc__[] =
""
;
static PyObject *
sndih_getSampleSize(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short bits;
OSErr err;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siSampleSize, (Ptr)&bits)) != noErr )
return PyMac_Error(err);
return Py_BuildValue("h", bits);
}
static char sndih_setSampleSize__doc__[] =
""
;
static PyObject *
sndih_setSampleSize(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
short size;
OSErr err;
if (!PyArg_ParseTuple(args, "lh", &inRefNum, &size))
return NULL;
if( (err=SPBSetDeviceInfo(inRefNum, siSampleSize, (Ptr)&size)) != noErr )
return PyMac_Error(err);
Py_INCREF(Py_None);
return Py_None;
}
static char sndih_getSampleSizeAvailable__doc__[] =
""
;
static PyObject *
sndih_getSampleSizeAvailable(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
struct SampleSizeAvailable_arg arg;
OSErr err;
PyObject *rsizes;
short *fsizes;
int i;
arg.sizes = NULL;
rsizes = NULL;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siSampleSizeAvailable, (Ptr)&arg)) != noErr ) {
return PyMac_Error(err);
}
fsizes = (short *)*(arg.sizes);
/* Handle contains a list of rates */
if( (rsizes = PyTuple_New(arg.numsizes)) == NULL)
return NULL;
for( i=0; i<arg.numsizes; i++ )
PyTuple_SetItem(rsizes, i, PyInt_FromLong((long)fsizes[i]));
return rsizes;
}
static char sndih_getSampleRateAvailable__doc__[] =
""
;
static PyObject *
sndih_getSampleRateAvailable(self, args)
PyObject *self; /* Not used */
PyObject *args;
{
long inRefNum;
struct SampleRateAvailable_arg arg;
OSErr err;
PyObject *rrates, *obj;
Fixed *frates;
int i;
arg.rates = NULL;
rrates = NULL;
if (!PyArg_ParseTuple(args, "l", &inRefNum))
return NULL;
if( (err=SPBGetDeviceInfo(inRefNum, siSampleRateAvailable, (Ptr)&arg)) != noErr ) {
return PyMac_Error(err);
}
frates = (Fixed *)*(arg.rates);
if( arg.numrates == 0 ) {
/* The handle contains upper and lowerbound */
rrates = Py_BuildValue("O&O&", frates[0], frates[1]);
if (rrates == NULL) return NULL;
} else {
/* Handle contains a list of rates */
if( (rrates = PyTuple_New(arg.numrates)) == NULL)
return NULL;
for( i=0; i<arg.numrates; i++ ) {
if( (obj = Py_BuildValue("O&", PyMac_BuildUFixed, frates[i]))==NULL)
goto out;
PyTuple_SetItem(rrates, i, obj);
}
}
return Py_BuildValue("hO", arg.numrates, rrates);
out:
Py_XDECREF(rrates);
return NULL;
}
/* List of methods defined in the module */
static struct PyMethodDef sndih_methods[] = {
{"getChannelAvailable", (PyCFunction)sndih_getChannelAvailable, METH_VARARGS, sndih_getChannelAvailable__doc__},
{"getNumberChannels", (PyCFunction)sndih_getNumberChannels, METH_VARARGS, sndih_getNumberChannels__doc__},
{"setNumberChannels", (PyCFunction)sndih_setNumberChannels, METH_VARARGS, sndih_setNumberChannels__doc__},
{"getContinuous", (PyCFunction)sndih_getContinuous, METH_VARARGS, sndih_getContinuous__doc__},
{"setContinuous", (PyCFunction)sndih_setContinuous, METH_VARARGS, sndih_setContinuous__doc__},
{"getInputSourceNames", (PyCFunction)sndih_getInputSourceNames, METH_VARARGS, sndih_getInputSourceNames__doc__},
{"getInputSource", (PyCFunction)sndih_getInputSource, METH_VARARGS, sndih_getInputSource__doc__},
{"setInputSource", (PyCFunction)sndih_setInputSource, METH_VARARGS, sndih_setInputSource__doc__},
{"getPlayThruOnOff", (PyCFunction)sndih_getPlayThruOnOff, METH_VARARGS, sndih_getPlayThruOnOff__doc__},
{"setPlayThruOnOff", (PyCFunction)sndih_setPlayThruOnOff, METH_VARARGS, sndih_setPlayThruOnOff__doc__},
{"getSampleRate", (PyCFunction)sndih_getSampleRate, METH_VARARGS, sndih_getSampleRate__doc__},
{"setSampleRate", (PyCFunction)sndih_setSampleRate, METH_VARARGS, sndih_setSampleRate__doc__},
{"getSampleSize", (PyCFunction)sndih_getSampleSize, METH_VARARGS, sndih_getSampleSize__doc__},
{"setSampleSize", (PyCFunction)sndih_setSampleSize, METH_VARARGS, sndih_setSampleSize__doc__},
{"getSampleSizeAvailable", (PyCFunction)sndih_getSampleSizeAvailable, METH_VARARGS, sndih_getSampleSizeAvailable__doc__},
{"getSampleRateAvailable", (PyCFunction)sndih_getSampleRateAvailable, METH_VARARGS, sndih_getSampleRateAvailable__doc__},
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};
/* Initialization function for the module (*must* be called initSndihooks) */
static char Sndihooks_module_documentation[] =
""
;
void
init_Sndihooks()
{
PyObject *m, *d;
/* Create the module and add the functions */
m = Py_InitModule4("_Sndihooks", sndih_methods,
Sndihooks_module_documentation,
(PyObject*)NULL,PYTHON_API_VERSION);
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
ErrorObject = PyString_FromString("Sndihooks.error");
PyDict_SetItemString(d, "error", ErrorObject);
/* XXXX Add constants here */
/* Check for errors */
if (PyErr_Occurred())
Py_FatalError("can't initialize module Sndihooks");
}

1163
Mac/Modules/snd/_Sndmodule.c Normal file

File diff suppressed because it is too large Load Diff

127
Mac/Modules/snd/sndscan.py Normal file
View File

@@ -0,0 +1,127 @@
# Scan Sound.h header file, generate sndgen.py and Sound.py files.
# Then import sndsupport (which execs sndgen.py) to generate Sndmodule.c.
# (Should learn how to tell the compiler to compile it as well.)
import sys
from bgenlocations import TOOLBOXDIR, BGENDIR
sys.path.append(BGENDIR)
from scantools import Scanner
def main():
input = "Sound.h"
output = "sndgen.py"
defsoutput = TOOLBOXDIR + "Sound.py"
scanner = SoundScanner(input, output, defsoutput)
scanner.scan()
scanner.close()
print "=== Testing definitions output code ==="
execfile(defsoutput, {}, {})
print "=== Done scanning and generating, now doing 'import sndsupport' ==="
import sndsupport
print "=== Done. It's up to you to compile Sndmodule.c ==="
class SoundScanner(Scanner):
def destination(self, type, name, arglist):
classname = "SndFunction"
listname = "functions"
if arglist:
t, n, m = arglist[0]
if t == "SndChannelPtr" and m == "InMode":
classname = "SndMethod"
listname = "sndmethods"
return classname, listname
def writeinitialdefs(self):
self.defsfile.write("def FOUR_CHAR_CODE(x): return x\n")
def makeblacklistnames(self):
return [
'SndDisposeChannel', # automatic on deallocation
'SndAddModifier', # for internal use only
'SndPlayDoubleBuffer', # very low level routine
# Missing from libraries (UH332)
'SoundManagerSetInfo',
'SoundManagerGetInfo',
# Constants with funny definitions
'rate48khz',
'rate44khz',
'kInvalidSource',
# OS8 only:
'MACEVersion',
'SPBRecordToFile',
'Exp1to6',
'Comp6to1',
'Exp1to3',
'Comp3to1',
'SndControl',
'SndStopFilePlay',
'SndStartFilePlay',
'SndPauseFilePlay',
'SndRecordToFile',
]
def makeblacklisttypes(self):
return [
"GetSoundVol",
"SetSoundVol",
"UnsignedFixed",
# Don't have the time to dig into this...
"Component",
"ComponentInstance",
"SoundComponentDataPtr",
"SoundComponentData",
"SoundComponentData_ptr",
"SoundConverter",
]
def makerepairinstructions(self):
return [
([("Str255", "*", "InMode")],
[("*", "*", "OutMode")]),
([("void_ptr", "*", "InMode"), ("long", "*", "InMode")],
[("InBuffer", "*", "*")]),
([("void", "*", "OutMode"), ("long", "*", "InMode"),
("long", "*", "OutMode")],
[("VarVarOutBuffer", "*", "InOutMode")]),
([("SCStatusPtr", "*", "InMode")],
[("SCStatus", "*", "OutMode")]),
([("SMStatusPtr", "*", "InMode")],
[("SMStatus", "*", "OutMode")]),
([("CompressionInfoPtr", "*", "InMode")],
[("CompressionInfo", "*", "OutMode")]),
# For SndPlay's SndListHandle argument
([("Handle", "sndHdl", "InMode")],
[("SndListHandle", "*", "*")]),
# For SndStartFilePlay
([("long", "bufferSize", "InMode"), ("void", "theBuffer", "OutMode")],
[("*", "*", "*"), ("FakeType('0')", "*", "InMode")]),
# For Comp3to1 etc.
([("void_ptr", "inBuffer", "InMode"),
("void", "outBuffer", "OutMode"),
("unsigned_long", "cnt", "InMode")],
[("InOutBuffer", "buffer", "InOutMode")]),
# Ditto
## ([("void_ptr", "inState", "InMode"), ("void", "outState", "OutMode")],
## [("InOutBuf128", "state", "InOutMode")]),
([("StateBlockPtr", "inState", "InMode"), ("StateBlockPtr", "outState", "InMode")],
[("StateBlock", "state", "InOutMode")]),
# Catch-all for the last couple of void pointers
([("void", "*", "OutMode")],
[("void_ptr", "*", "InMode")]),
]
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,319 @@
# This script generates the Sound interface for Python.
# It uses the "bgen" package to generate C code.
# It execs the file sndgen.py which contain the function definitions
# (sndgen.py was generated by sndscan.py, scanning the <Sound.h> header file).
from macsupport import *
# define our own function and module generators
class SndMixIn: pass
class SndFunction(SndMixIn, OSErrFunctionGenerator): pass
class SndMethod(SndMixIn, OSErrMethodGenerator): pass
# includestuff etc. are imported from macsupport
includestuff = includestuff + """
#include <Carbon/Carbon.h>
"""
initstuff = initstuff + """
"""
# define types used for arguments (in addition to standard and macsupport types)
class SndChannelPtrType(OpaqueByValueType):
def declare(self, name):
# Initializing all SndChannelPtr objects to 0 saves
# special-casing NewSndChannel(), where it is formally an
# input-output parameter but we treat it as output-only
# (since Python users are not supposed to allocate memory)
Output("SndChannelPtr %s = 0;", name)
SndChannelPtr = SndChannelPtrType('SndChannelPtr', 'SndCh')
SndCommand = OpaqueType('SndCommand', 'SndCmd')
SndCommand_ptr = OpaqueType('SndCommand', 'SndCmd')
SndListHandle = OpaqueByValueType("SndListHandle", "ResObj")
SPBPtr = OpaqueByValueType("SPBPtr", "SPBObj")
ModalFilterUPP = FakeType("(ModalFilterUPP)0")
#
# NOTE: the following is pretty dangerous. For void pointers we pass buffer addresses
# but we have no way to check that the buffer is big enough. This is the same problem
# as in C, though (but Pythoneers may not be suspecting this...)
void_ptr = Type("void *", "w")
class SndCallBackType(InputOnlyType):
def __init__(self):
Type.__init__(self, 'PyObject*', 'O')
def getargsCheck(self, name):
Output("if (%s != Py_None && !PyCallable_Check(%s))", name, name)
OutLbrace()
Output('PyErr_SetString(PyExc_TypeError, "callback must be callable");')
Output("goto %s__error__;", name)
OutRbrace()
def passInput(self, name):
return "NewSndCallBackUPP(SndCh_UserRoutine)"
def cleanup(self, name):
# XXX This knows it is executing inside the SndNewChannel wrapper
Output("if (_res != NULL && %s != Py_None)", name)
OutLbrace()
Output("SndChannelObject *p = (SndChannelObject *)_res;")
Output("p->ob_itself->userInfo = (long)p;")
Output("Py_INCREF(%s);", name)
Output("p->ob_callback = %s;", name)
OutRbrace()
DedentLevel()
Output(" %s__error__: ;", name)
IndentLevel()
SndCallBackProcPtr = SndCallBackType()
SndCallBackUPP = SndCallBackProcPtr
SndCompletionProcPtr = FakeType('(SndCompletionProcPtr)0') # XXX
SndCompletionUPP = SndCompletionProcPtr
##InOutBuf128 = FixedInputOutputBufferType(128)
StateBlock = StructInputOutputBufferType('StateBlock')
AudioSelectionPtr = FakeType('0') # XXX
ProcPtr = FakeType('0') # XXX
FilePlayCompletionUPP = FakeType('0') # XXX
SCStatus = StructOutputBufferType('SCStatus')
SMStatus = StructOutputBufferType('SMStatus')
CompressionInfo = StructOutputBufferType('CompressionInfo')
includestuff = includestuff + """
/* Convert a SndCommand argument */
static int
SndCmd_Convert(PyObject *v, SndCommand *pc)
{
int len;
pc->param1 = 0;
pc->param2 = 0;
if (PyTuple_Check(v)) {
if (PyArg_ParseTuple(v, "h|hl", &pc->cmd, &pc->param1, &pc->param2))
return 1;
PyErr_Clear();
return PyArg_ParseTuple(v, "Hhs#", &pc->cmd, &pc->param1, &pc->param2, &len);
}
return PyArg_Parse(v, "H", &pc->cmd);
}
static pascal void SndCh_UserRoutine(SndChannelPtr chan, SndCommand *cmd); /* Forward */
static pascal void SPB_completion(SPBPtr my_spb); /* Forward */
"""
finalstuff = finalstuff + """
/* Routine passed to Py_AddPendingCall -- call the Python callback */
static int
SndCh_CallCallBack(void *arg)
{
SndChannelObject *p = (SndChannelObject *)arg;
PyObject *args;
PyObject *res;
args = Py_BuildValue("(O(hhl))",
p, p->ob_cmd.cmd, p->ob_cmd.param1, p->ob_cmd.param2);
res = PyEval_CallObject(p->ob_callback, args);
Py_DECREF(args);
if (res == NULL)
return -1;
Py_DECREF(res);
return 0;
}
/* Routine passed to NewSndChannel -- schedule a call to SndCh_CallCallBack */
static pascal void
SndCh_UserRoutine(SndChannelPtr chan, SndCommand *cmd)
{
SndChannelObject *p = (SndChannelObject *)(chan->userInfo);
if (p->ob_callback != NULL) {
long A5 = SetA5(p->ob_A5);
p->ob_cmd = *cmd;
Py_AddPendingCall(SndCh_CallCallBack, (void *)p);
SetA5(A5);
}
}
/* SPB callbacks - Schedule callbacks to Python */
static int
SPB_CallCallBack(void *arg)
{
SPBObject *p = (SPBObject *)arg;
PyObject *args;
PyObject *res;
if ( p->ob_thiscallback == 0 ) return 0;
args = Py_BuildValue("(O)", p);
res = PyEval_CallObject(p->ob_thiscallback, args);
p->ob_thiscallback = 0;
Py_DECREF(args);
if (res == NULL)
return -1;
Py_DECREF(res);
return 0;
}
static pascal void
SPB_completion(SPBPtr my_spb)
{
SPBObject *p = (SPBObject *)(my_spb->userLong);
if (p && p->ob_completion) {
long A5 = SetA5(p->ob_A5);
p->ob_thiscallback = p->ob_completion; /* Hope we cannot get two at the same time */
Py_AddPendingCall(SPB_CallCallBack, (void *)p);
SetA5(A5);
}
}
"""
# create the module and object definition and link them
class SndObjectDefinition(PEP252Mixin, ObjectDefinition):
def outputStructMembers(self):
ObjectDefinition.outputStructMembers(self)
Output("/* Members used to implement callbacks: */")
Output("PyObject *ob_callback;")
Output("long ob_A5;");
Output("SndCommand ob_cmd;")
def outputInitStructMembers(self):
ObjectDefinition.outputInitStructMembers(self)
Output("it->ob_callback = NULL;")
Output("it->ob_A5 = SetCurrentA5();");
def outputCleanupStructMembers(self):
ObjectDefinition.outputCleanupStructMembers(self)
Output("Py_XDECREF(self->ob_callback);")
def outputFreeIt(self, itselfname):
Output("SndDisposeChannel(%s, 1);", itselfname)
def outputConvert(self):
pass # Not needed
#
class SpbObjectDefinition(PEP252Mixin, ObjectDefinition):
getsetlist = [
(
'inRefNum',
'return Py_BuildValue("l", self->ob_spb.inRefNum);',
'return -1 + PyArg_Parse(v, "l", &self->ob_spb.inRefNum);',
None,
), (
'count',
'return Py_BuildValue("l", self->ob_spb.count);',
'return -1 + PyArg_Parse(v, "l", &self->ob_spb.count);',
None
), (
'milliseconds',
'return Py_BuildValue("l", self->ob_spb.milliseconds);',
'return -1 + PyArg_Parse(v, "l", &self->ob_spb.milliseconds);',
None,
), (
'error',
'return Py_BuildValue("h", self->ob_spb.error);',
None,
None
), (
'completionRoutine',
None,
"""self->ob_spb.completionRoutine = NewSICompletionUPP(SPB_completion);
self->ob_completion = v;
Py_INCREF(v);
return 0;""",
None,
)]
def outputStructMembers(self):
Output("/* Members used to implement callbacks: */")
Output("PyObject *ob_completion;")
Output("PyObject *ob_interrupt;")
Output("PyObject *ob_thiscallback;");
Output("long ob_A5;")
Output("SPB ob_spb;")
def outputNew(self):
Output()
Output("%sPyObject *%s_New(void)", self.static, self.prefix)
OutLbrace()
Output("%s *it;", self.objecttype)
self.outputCheckNewArg()
Output("it = PyObject_NEW(%s, &%s);", self.objecttype, self.typename)
Output("if (it == NULL) return NULL;")
self.outputInitStructMembers()
Output("return (PyObject *)it;")
OutRbrace()
def outputInitStructMembers(self):
Output("it->ob_completion = NULL;")
Output("it->ob_interrupt = NULL;")
Output("it->ob_thiscallback = NULL;")
Output("it->ob_A5 = SetCurrentA5();")
Output("memset((char *)&it->ob_spb, 0, sizeof(it->ob_spb));")
Output("it->ob_spb.userLong = (long)it;")
def outputCleanupStructMembers(self):
ObjectDefinition.outputCleanupStructMembers(self)
Output("self->ob_spb.userLong = 0;")
Output("self->ob_thiscallback = 0;")
Output("Py_XDECREF(self->ob_completion);")
Output("Py_XDECREF(self->ob_interrupt);")
def outputConvert(self):
Output("%sint %s_Convert(PyObject *v, %s *p_itself)", self.static, self.prefix, self.itselftype)
OutLbrace()
self.outputCheckConvertArg()
Output("if (!%s_Check(v))", self.prefix)
OutLbrace()
Output('PyErr_SetString(PyExc_TypeError, "%s required");', self.name)
Output("return 0;")
OutRbrace()
Output("*p_itself = &((%s *)v)->ob_spb;", self.objecttype)
Output("return 1;")
OutRbrace()
sndobject = SndObjectDefinition('SndChannel', 'SndCh', 'SndChannelPtr')
spbobject = SpbObjectDefinition('SPB', 'SPBObj', 'SPBPtr')
spbgenerator = ManualGenerator("SPB", "_res = SPBObj_New(); return _res;")
module = MacModule('_Snd', 'Snd', includestuff, finalstuff, initstuff)
module.addobject(sndobject)
module.addobject(spbobject)
module.add(spbgenerator)
# create lists of functions and object methods
functions = []
sndmethods = []
# populate the lists
execfile('sndgen.py')
# add the functions and methods to the module and object, respectively
for f in functions: module.add(f)
for f in sndmethods: sndobject.add(f)
# generate output
SetOutputFileName('_Sndmodule.c')
module.generate()