[Python-checkins] r57245 - in python/branches/alex-py3k: Lib/test/test_memoryio.py Modules/_bytesiomodule.c Modules/_stringiomodule.c
alexandre.vassalotti
python-checkins at python.org
Tue Aug 21 07:00:40 CEST 2007
Author: alexandre.vassalotti
Date: Tue Aug 21 07:00:40 2007
New Revision: 57245
Modified:
python/branches/alex-py3k/Lib/test/test_memoryio.py
python/branches/alex-py3k/Modules/_bytesiomodule.c
python/branches/alex-py3k/Modules/_stringiomodule.c
Log:
In resize_buffer(), use string_size to tell if resize down to exact
size is required.
Move object initialization into the tp_init slot.
Fix whence value checking in the seek() method.
Update tests for the above changes.
Modified: python/branches/alex-py3k/Lib/test/test_memoryio.py
==============================================================================
--- python/branches/alex-py3k/Lib/test/test_memoryio.py (original)
+++ python/branches/alex-py3k/Lib/test/test_memoryio.py Tue Aug 21 07:00:40 2007
@@ -27,14 +27,18 @@
self.assertEqual(f.write(t(" world\n\n\n")), 9)
self.assertEqual(f.seek(0), 0)
self.assertEqual(f.write(t("h")), 1)
+ self.assertEqual(f.truncate(12), 12)
+ self.assertEqual(f.tell(), 12)
def test_write(self):
buf = self.buftype("hello world\n")
memio = self.ioclass(buf)
self.write_ops(memio, self.buftype)
+ self.assertEqual(memio.getvalue(), buf)
memio = self.ioclass()
self.write_ops(memio, self.buftype)
+ self.assertEqual(memio.getvalue(), buf)
memio.close()
memio.write(buf)
@@ -239,12 +243,19 @@
def test_subclassing(self):
buf = self.buftype("1234567890")
- def test():
+ def test1():
class MemIO(self.ioclass):
pass
m = MemIO(buf)
return m.getvalue()
- self.assertEqual(test(), buf)
+ def test2():
+ class MemIO(self.ioclass):
+ def __init__(me, a, b):
+ self.ioclass.__init__(me, a)
+ m = MemIO(buf, None)
+ return m.getvalue()
+ self.assertEqual(test1(), buf)
+ self.assertEqual(test2(), buf)
def test_widechar(self):
buf = self.buftype("\U0002030a\U00020347")
Modified: python/branches/alex-py3k/Modules/_bytesiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_bytesiomodule.c (original)
+++ python/branches/alex-py3k/Modules/_bytesiomodule.c Tue Aug 21 07:00:40 2007
@@ -52,7 +52,7 @@
{
Py_ssize_t alloc = self->buf_size;
- if (size < alloc / 2) {
+ if (self->string_size < alloc / 2) {
/* Major downsize; resize down to exact size */
alloc = size + 1;
}
@@ -469,16 +469,11 @@
BytesIO_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
BytesIOObject *self;
- PyObject *initvalue = NULL, *ret;
enum { INIT_BUFSIZE = 1 };
assert(type != NULL && type->tp_alloc != NULL);
- if (!PyArg_ParseTuple(args, "|O:BytesIO", &initvalue))
- return NULL;
-
self = (BytesIOObject *)type->tp_alloc(type, 0);
-
if (self == NULL)
return NULL;
@@ -488,21 +483,29 @@
return NULL;
}
- /* These variables need to be initialized before attempting to write
- anything to the object. */
self->pos = 0;
self->string_size = 0;
self->buf_size = INIT_BUFSIZE;
+ return (PyObject *)self;
+}
+
+static int
+BytesIO_init(PyObject *self, PyObject *args, PyObject *kwds)
+{
+ PyObject *initvalue = NULL, *ret;
+
+ if (!PyArg_ParseTuple(args, "|O:BytesIO", &initvalue))
+ return -1;
+
if (initvalue && initvalue != Py_None) {
- ret = bytesio_write(self, initvalue);
+ ret = bytesio_write((BytesIOObject *)self, initvalue);
if (ret == NULL)
- return NULL;
+ return -1;
Py_DECREF(ret);
- self->pos = 0;
+ ((BytesIOObject *)self)->pos = 0;
}
-
- return (PyObject *)self;
+ return 0;
}
@@ -672,7 +675,7 @@
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
- 0, /*tp_init*/
+ BytesIO_init, /*tp_init*/
0, /*tp_alloc*/
BytesIO_new, /*tp_new*/
};
Modified: python/branches/alex-py3k/Modules/_stringiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_stringiomodule.c (original)
+++ python/branches/alex-py3k/Modules/_stringiomodule.c Tue Aug 21 07:00:40 2007
@@ -51,7 +51,7 @@
{
Py_ssize_t alloc = self->buf_size;
- if (size < alloc / 2) {
+ if (self->string_size < alloc / 2) {
/* Major downsize; resize down to exact size */
alloc = size + 1;
}
@@ -323,12 +323,17 @@
if (!PyArg_ParseTuple(args, "n|i:seek", &newpos, &mode))
return NULL;
- if (newpos < 0 && mode == 0) {
+ if (mode != 0 && mode != 1 && mode != 2) {
+ PyErr_Format(PyExc_ValueError,
+ "Invalid whence (%i, should be 0, 1 or 2)", mode);
+ return NULL;
+ }
+ else if (newpos < 0 && mode == 0) {
PyErr_Format(PyExc_ValueError,
"Negative seek position %zd", newpos);
return NULL;
}
- if (mode != 0 && newpos != 0) {
+ else if (mode != 0 && newpos != 0) {
PyErr_SetString(PyExc_IOError,
"Can't do nonzero cur-relative seeks");
return NULL;
@@ -343,11 +348,6 @@
else if (mode == 2) {
newpos = self->string_size;
}
- else if (mode != 0) {
- PyErr_Format(PyExc_ValueError,
- "Invalid whence (%i, should be 0, 1 or 2)", mode);
- return NULL;
- }
if (newpos > self->string_size) {
if (resize_buffer(self, newpos + 1) < 0)
@@ -447,16 +447,11 @@
StringIO_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
StringIOObject *self;
- PyObject *initvalue = NULL, *ret;
enum { INIT_BUFSIZE = 1 };
assert(type != NULL && type->tp_alloc != NULL);
- if (!PyArg_ParseTuple(args, "|O:StringIO", &initvalue))
- return NULL;
-
self = (StringIOObject *)type->tp_alloc(type, 0);
-
if (self == NULL)
return NULL;
@@ -466,24 +461,31 @@
return NULL;
}
- /* These variables need to be initialized before attempting to write
- anything to the object. */
self->pos = 0;
self->string_size = 0;
self->buf_size = INIT_BUFSIZE;
+ return (PyObject *)self;
+}
+
+static int
+StringIO_init(PyObject *self, PyObject *args, PyObject *kwds)
+{
+ PyObject *initvalue = NULL, *ret;
+
+ if (!PyArg_ParseTuple(args, "|O:StringIO", &initvalue))
+ return -1;
+
if (initvalue && initvalue != Py_None) {
- ret = stringio_write(self, initvalue);
+ ret = stringio_write((StringIOObject *)self, initvalue);
if (ret == NULL)
- return NULL;
+ return -1;
Py_DECREF(ret);
- self->pos = 0;
+ ((StringIOObject *)self)->pos = 0;
}
-
- return (PyObject *)self;
+ return 0;
}
-
PyDoc_STRVAR(StringIO_doc,
"StringIO([buffer]) -> Return a StringIO stream for reading and writing.");
@@ -634,7 +636,7 @@
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
- 0, /*tp_init*/
+ StringIO_init, /*tp_init*/
0, /*tp_alloc*/
StringIO_new, /*tp_new*/
};
More information about the Python-checkins
mailing list