Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(207044)

Side by Side Diff: Modules/ossaudiodev.c

Issue 23001: Accept mutable bytes-like objects
Patch Set: Created 4 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * ossaudiodev -- Python interface to the OSS (Open Sound System) API. 2 * ossaudiodev -- Python interface to the OSS (Open Sound System) API.
3 * This is the standard audio API for Linux and some 3 * This is the standard audio API for Linux and some
4 * flavours of BSD [XXX which ones?]; it is also available 4 * flavours of BSD [XXX which ones?]; it is also available
5 * for a wide range of commercial Unices. 5 * for a wide range of commercial Unices.
6 * 6 *
7 * Originally written by Peter Bosch, March 2000, as linuxaudiodev. 7 * Originally written by Peter Bosch, March 2000, as linuxaudiodev.
8 * 8 *
9 * Renamed to ossaudiodev and rearranged/revised/hacked up 9 * Renamed to ossaudiodev and rearranged/revised/hacked up
10 * by Greg Ward <gward@python.net>, November 2002. 10 * by Greg Ward <gward@python.net>, November 2002.
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 return NULL; 423 return NULL;
424 } 424 }
425 self->icount += count; 425 self->icount += count;
426 _PyBytes_Resize(&rv, count); 426 _PyBytes_Resize(&rv, count);
427 return rv; 427 return rv;
428 } 428 }
429 429
430 static PyObject * 430 static PyObject *
431 oss_write(oss_audio_t *self, PyObject *args) 431 oss_write(oss_audio_t *self, PyObject *args)
432 { 432 {
433 char *cp; 433 Py_buffer data;
434 int rv, size; 434 int rv;
435 435
436 if (!_is_fd_valid(self->fd)) 436 if (!_is_fd_valid(self->fd))
437 return NULL; 437 return NULL;
438 438
439 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) { 439 if (!PyArg_ParseTuple(args, "y*:write", &data)) {
440 return NULL; 440 return NULL;
441 } 441 }
442 442
443 Py_BEGIN_ALLOW_THREADS 443 Py_BEGIN_ALLOW_THREADS
444 rv = write(self->fd, cp, size); 444 rv = write(self->fd, data.buf, data.len);
445 Py_END_ALLOW_THREADS 445 Py_END_ALLOW_THREADS
446 446
447 PyBuffer_Release(&data);
447 if (rv == -1) { 448 if (rv == -1) {
448 return PyErr_SetFromErrno(PyExc_IOError); 449 return PyErr_SetFromErrno(PyExc_IOError);
449 } else { 450 } else {
450 self->ocount += rv; 451 self->ocount += rv;
451 } 452 }
452 return PyLong_FromLong(rv); 453 return PyLong_FromLong(rv);
453 } 454 }
454 455
455 static PyObject * 456 static PyObject *
456 oss_writeall(oss_audio_t *self, PyObject *args) 457 oss_writeall(oss_audio_t *self, PyObject *args)
457 { 458 {
458 char *cp; 459 Py_buffer data;
459 int rv, size; 460 const char *cp;
461 Py_ssize_t size;
462 int rv;
460 fd_set write_set_fds; 463 fd_set write_set_fds;
461 int select_rv; 464 int select_rv;
462 465
463 /* NB. writeall() is only useful in non-blocking mode: according to 466 /* NB. writeall() is only useful in non-blocking mode: according to
464 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list 467 Guenter Geiger <geiger@xdv.org> on the linux-audio-dev list
465 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that 468 (http://eca.cx/lad/2002/11/0380.html), OSS guarantees that
466 write() in blocking mode consumes the whole buffer. In blocking 469 write() in blocking mode consumes the whole buffer. In blocking
467 mode, the behaviour of write() and writeall() from Python is 470 mode, the behaviour of write() and writeall() from Python is
468 indistinguishable. */ 471 indistinguishable. */
469 472
470 if (!_is_fd_valid(self->fd)) 473 if (!_is_fd_valid(self->fd))
471 return NULL; 474 return NULL;
472 475
473 if (!PyArg_ParseTuple(args, "y#:write", &cp, &size)) 476 if (!PyArg_ParseTuple(args, "y*:writeall", &data))
474 return NULL; 477 return NULL;
475 478
476 if (!_PyIsSelectable_fd(self->fd)) { 479 if (!_PyIsSelectable_fd(self->fd)) {
477 PyErr_SetString(PyExc_ValueError, 480 PyErr_SetString(PyExc_ValueError,
478 "file descriptor out of range for select"); 481 "file descriptor out of range for select");
482 PyBuffer_Release(&data);
479 return NULL; 483 return NULL;
480 } 484 }
481 /* use select to wait for audio device to be available */ 485 /* use select to wait for audio device to be available */
482 FD_ZERO(&write_set_fds); 486 FD_ZERO(&write_set_fds);
483 FD_SET(self->fd, &write_set_fds); 487 FD_SET(self->fd, &write_set_fds);
488 cp = (const char *)data.buf;
489 size = data.len;
484 490
485 while (size > 0) { 491 while (size > 0) {
486 Py_BEGIN_ALLOW_THREADS 492 Py_BEGIN_ALLOW_THREADS
487 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL); 493 select_rv = select(self->fd+1, NULL, &write_set_fds, NULL, NULL);
488 Py_END_ALLOW_THREADS 494 Py_END_ALLOW_THREADS
489 assert(select_rv != 0); /* no timeout, can't expire */ 495 assert(select_rv != 0); /* no timeout, can't expire */
490 if (select_rv == -1) 496 if (select_rv == -1) {
497 PyBuffer_Release(&data);
491 return PyErr_SetFromErrno(PyExc_IOError); 498 return PyErr_SetFromErrno(PyExc_IOError);
499 }
492 500
493 Py_BEGIN_ALLOW_THREADS 501 Py_BEGIN_ALLOW_THREADS
494 rv = write(self->fd, cp, size); 502 rv = write(self->fd, cp, Py_MIN(size, INT_MAX));
495 Py_END_ALLOW_THREADS 503 Py_END_ALLOW_THREADS
496 if (rv == -1) { 504 if (rv == -1) {
497 if (errno == EAGAIN) { /* buffer is full, try again */ 505 if (errno == EAGAIN) { /* buffer is full, try again */
498 errno = 0; 506 errno = 0;
499 continue; 507 continue;
500 } else /* it's a real error */ 508 } else { /* it's a real error */
509 PyBuffer_Release(&data);
501 return PyErr_SetFromErrno(PyExc_IOError); 510 return PyErr_SetFromErrno(PyExc_IOError);
511 }
502 } else { /* wrote rv bytes */ 512 } else { /* wrote rv bytes */
503 self->ocount += rv; 513 self->ocount += rv;
504 size -= rv; 514 size -= rv;
505 cp += rv; 515 cp += rv;
506 } 516 }
507 } 517 }
518 PyBuffer_Release(&data);
508 Py_INCREF(Py_None); 519 Py_INCREF(Py_None);
509 return Py_None; 520 return Py_None;
510 } 521 }
511 522
512 static PyObject * 523 static PyObject *
513 oss_close(oss_audio_t *self, PyObject *unused) 524 oss_close(oss_audio_t *self, PyObject *unused)
514 { 525 {
515 if (self->fd >= 0) { 526 if (self->fd >= 0) {
516 Py_BEGIN_ALLOW_THREADS 527 Py_BEGIN_ALLOW_THREADS
517 close(self->fd); 528 close(self->fd);
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after
1296 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE); 1307 _EXPORT_INT(m, SNDCTL_TMR_CONTINUE);
1297 _EXPORT_INT(m, SNDCTL_TMR_METRONOME); 1308 _EXPORT_INT(m, SNDCTL_TMR_METRONOME);
1298 _EXPORT_INT(m, SNDCTL_TMR_SELECT); 1309 _EXPORT_INT(m, SNDCTL_TMR_SELECT);
1299 _EXPORT_INT(m, SNDCTL_TMR_SOURCE); 1310 _EXPORT_INT(m, SNDCTL_TMR_SOURCE);
1300 _EXPORT_INT(m, SNDCTL_TMR_START); 1311 _EXPORT_INT(m, SNDCTL_TMR_START);
1301 _EXPORT_INT(m, SNDCTL_TMR_STOP); 1312 _EXPORT_INT(m, SNDCTL_TMR_STOP);
1302 _EXPORT_INT(m, SNDCTL_TMR_TEMPO); 1313 _EXPORT_INT(m, SNDCTL_TMR_TEMPO);
1303 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE); 1314 _EXPORT_INT(m, SNDCTL_TMR_TIMEBASE);
1304 return m; 1315 return m;
1305 } 1316 }
OLDNEW
« Modules/fcntlmodule.c ('K') | « Modules/mmapmodule.c ('k') | Modules/socketmodule.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+