Transactional semantics for the alsapcm_set* calls

This commit is contained in:
Lars Immisch
2018-02-28 09:52:53 +00:00
parent 1aae655d24
commit 25717020ef

View File

@@ -638,8 +638,9 @@ Returns the name of the sound card used by this PCM object.");
static PyObject * static PyObject *
alsapcm_setchannels(alsapcm_t *self, PyObject *args) alsapcm_setchannels(alsapcm_t *self, PyObject *args)
{ {
int channels; int channels, saved;
int res; int res;
if (!PyArg_ParseTuple(args,"i:setchannels", &channels)) if (!PyArg_ParseTuple(args,"i:setchannels", &channels))
return NULL; return NULL;
@@ -648,10 +649,12 @@ alsapcm_setchannels(alsapcm_t *self, PyObject *args)
return NULL; return NULL;
} }
saved = self->channels;
self->channels = channels; self->channels = channels;
res = alsapcm_setup(self); res = alsapcm_setup(self);
if (res < 0) if (res < 0)
{ {
self->channels = saved;
PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(res), PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(res),
self->cardname); self->cardname);
return NULL; return NULL;
@@ -671,7 +674,7 @@ Few sound cards support more than 2 channels.");
static PyObject * static PyObject *
alsapcm_setrate(alsapcm_t *self, PyObject *args) alsapcm_setrate(alsapcm_t *self, PyObject *args)
{ {
int rate; int rate, saved;
int res; int res;
if (!PyArg_ParseTuple(args,"i:setrate", &rate)) if (!PyArg_ParseTuple(args,"i:setrate", &rate))
return NULL; return NULL;
@@ -682,10 +685,12 @@ alsapcm_setrate(alsapcm_t *self, PyObject *args)
return NULL; return NULL;
} }
saved = self->rate;
self->rate = rate; self->rate = rate;
res = alsapcm_setup(self); res = alsapcm_setup(self);
if (res < 0) if (res < 0)
{ {
self->rate = saved;
PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(res), PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(res),
self->cardname); self->cardname);
return NULL; return NULL;
@@ -703,7 +708,7 @@ Set the sample rate in Hz for the device. Typical values are\n\
static PyObject * static PyObject *
alsapcm_setformat(alsapcm_t *self, PyObject *args) alsapcm_setformat(alsapcm_t *self, PyObject *args)
{ {
int format; int format, saved;
int res; int res;
if (!PyArg_ParseTuple(args,"i:setformat", &format)) if (!PyArg_ParseTuple(args,"i:setformat", &format))
return NULL; return NULL;
@@ -714,10 +719,12 @@ alsapcm_setformat(alsapcm_t *self, PyObject *args)
return NULL; return NULL;
} }
saved = self->format;
self->format = format; self->format = format;
res = alsapcm_setup(self); res = alsapcm_setup(self);
if (res < 0) if (res < 0)
{ {
self->format = saved;
PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(res), PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(res),
self->cardname); self->cardname);
return NULL; return NULL;
@@ -731,28 +738,33 @@ PyDoc_STRVAR(setformat_doc,
static PyObject * static PyObject *
alsapcm_setperiodsize(alsapcm_t *self, PyObject *args) alsapcm_setperiodsize(alsapcm_t *self, PyObject *args)
{ {
int periodsize; int periodsize, saved;
int res; int res;
if (!PyArg_ParseTuple(args,"i:setperiodsize", &periodsize)) if (!PyArg_ParseTuple(args,"i:setperiodsize", &periodsize))
return NULL; return NULL;
if (!self->handle) if (!self->handle)
{ {
PyErr_SetString(ALSAAudioError, "PCM device is closed"); PyErr_SetString(ALSAAudioError, "PCM device is closed");
return NULL; return NULL;
} }
saved = self->periodsize;
self->periodsize = periodsize;
res = alsapcm_setup(self); res = alsapcm_setup(self);
if (res < 0) if (res < 0)
{ {
self->periodsize = saved;
PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(res), PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(res),
self->cardname); self->cardname);
return NULL; return NULL;
} }
self->periodsize = periodsize;
return PyLong_FromLong(self->periodsize); return PyLong_FromLong(self->periodsize);
} }