[Python-checkins] r57201 - python/branches/alex-py3k/Modules/_bytesiomodule.c

alexandre.vassalotti python-checkins at python.org
Mon Aug 20 00:24:00 CEST 2007


Author: alexandre.vassalotti
Date: Mon Aug 20 00:23:58 2007
New Revision: 57201

Modified:
   python/branches/alex-py3k/Modules/_bytesiomodule.c
Log:
Change how arguments are processed, to allow None, in methods which
take optional arguments.


Modified: python/branches/alex-py3k/Modules/_bytesiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_bytesiomodule.c	(original)
+++ python/branches/alex-py3k/Modules/_bytesiomodule.c	Mon Aug 20 00:23:58 2007
@@ -159,25 +159,39 @@
 static PyObject *
 bytesio_read(BytesIOObject *self, PyObject *args)
 {
-    Py_ssize_t len, n = -1;
+    Py_ssize_t size, n;
     char *output;
+    PyObject *arg = Py_None;
 
-    if (!PyArg_ParseTuple(args, "|n:read", &n))
+    if (!PyArg_UnpackTuple(args, "read", 0, 1, &arg))
         return NULL;
 
+    if (PyInt_Check(arg)) {
+        size = PyInt_AsSsize_t(arg);
+    }
+    else if (arg == Py_None) {
+        /* Read until EOF is reached, by default. */
+        size = -1;
+    }
+    else {
+        PyErr_Format(PyExc_TypeError, "integer argument expected, got %s",
+                     Py_Type(arg)->tp_name);
+        return NULL;
+    }
+
     /* adjust invalid sizes */
-    len = self->string_size - self->pos;
-    if (n < 0 || n > len) {
-        n = len;
-        if (n < 0)
-            n = 0;
+    n = self->string_size - self->pos;
+    if (size < 0 || size > n) {
+        size = n;
+        if (size < 0)
+            size = 0;
     }
 
     assert(self->buf != NULL);
     output = self->buf + self->pos;
-    self->pos += n;
+    self->pos += size;
 
-    return PyBytes_FromStringAndSize(output, n);
+    return PyBytes_FromStringAndSize(output, size);
 }
 
 static PyObject *
@@ -185,6 +199,8 @@
 {
     PyObject *arg, *res;
 
+    /* XXX: What should happen if n is Py_None? */
+
     arg = PyTuple_Pack(1, n);
     if (arg == NULL)
         return NULL;
@@ -196,11 +212,25 @@
 static PyObject *
 bytesio_readline(BytesIOObject *self, PyObject *args)
 {
-    Py_ssize_t n, size = -1;
+    Py_ssize_t size, n;
     char *output;
+    PyObject *arg = Py_None;
+
+    if (!PyArg_UnpackTuple(args, "readline", 0, 1, &arg))
+        return NULL;
 
-    if (!PyArg_ParseTuple(args, "|i:readline", &size))
+    if (PyInt_Check(arg)) {
+        size = PyInt_AsSsize_t(arg);
+    }
+    else if (arg == Py_None) {
+        /* No size limit, by default. */
+        size = -1;
+    }
+    else {
+        PyErr_Format(PyExc_TypeError, "integer argument expected, got %s",
+                     Py_Type(arg)->tp_name);
         return NULL;
+    }
 
     n = get_line(self, &output);
 
@@ -216,12 +246,26 @@
 static PyObject *
 bytesio_readlines(BytesIOObject *self, PyObject *args)
 {
-    Py_ssize_t n, size = 0, len = 0;
+    Py_ssize_t maxsize, size, n;
     PyObject *result, *line;
     char *output;
+    PyObject *arg = Py_None;
+    
+    if (!PyArg_UnpackTuple(args, "readlines", 0, 1, &arg))
+        return NULL;
 
-    if (!PyArg_ParseTuple(args, "|i:readlines", &size))
+    if (PyInt_Check(arg)) {
+        maxsize = PyInt_AsSsize_t(arg);
+    }
+    else if (arg == Py_None) {
+        /* No size limit, by default. */
+        maxsize = -1;
+    }
+    else {
+        PyErr_Format(PyExc_TypeError, "integer argument expected, got %s",
+                     Py_Type(arg)->tp_name);
         return NULL;
+    }
 
     result = PyList_New(0);
     if (!result)
@@ -236,8 +280,8 @@
             goto on_error;
         }
         Py_DECREF(line);
-        len += n;
-        if (size > 0 && len >= size)
+        size += n;
+        if (maxsize > 0 && size >= maxsize)
             break;
     }
     return result;
@@ -271,12 +315,23 @@
 bytesio_truncate(BytesIOObject *self, PyObject *args)
 {
     Py_ssize_t size;
+    PyObject *arg = Py_None;
 
-    /* Truncate to current position if no argument is passed. */
-    size = self->pos;
+    if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &arg))
+        return NULL;
 
-    if (!PyArg_ParseTuple(args, "|n:truncate", &size))
+    if (PyInt_Check(arg)) {
+        size = PyInt_AsSsize_t(arg);
+    }
+    else if (arg == Py_None) {
+        /* Truncate to current position if no argument is passed. */
+        size = self->pos;
+    }
+    else {
+        PyErr_Format(PyExc_TypeError, "integer argument expected, got %s",
+                     Py_Type(arg)->tp_name);
         return NULL;
+    }
 
     if (size < 0) {
         /* XXX: Give a better error message. */
@@ -439,7 +494,7 @@
     self->string_size = 0;
     self->buf_size = INIT_BUFSIZE;
 
-    if (initvalue) {
+    if (initvalue && initvalue != Py_None) {
         ret = bytesio_write(self, initvalue);
         if (ret == NULL)
             return NULL;


More information about the Python-checkins mailing list