annotate Modules/cStringIO.c @ 64209:dfd0229748ef 2.7

Merged revisions 84239 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84239 | antoine.pitrou | 2010-08-21 21:09:32 +0200 (sam., 21 août 2010) | 4 lines Issue #9617: Signals received during a low-level write operation aren't ignored by the buffered IO layer anymore. ........
author Antoine Pitrou <solipsis@pitrou.net>
date Sat, 21 Aug 2010 19:17:25 +0000
parents fd25030223b0
children d4909a056d4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
1
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
2 #include "Python.h"
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
3 #include "import.h"
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
4 #include "cStringIO.h"
28772
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
5 #include "structmember.h"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
6
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
7 PyDoc_STRVAR(cStringIO_module_documentation,
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 "A simple fast partial StringIO replacement.\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 "This module provides a simple useful replacement for\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 "the StringIO module that is written in C. It does not provide the\n"
12959
2513cd74147c Fixed docstring typo, reported by Skip Montanaro <skip@mojam.com>.
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
12 "full generality of StringIO, but it provides enough for most\n"
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
13 "applications and is especially useful in conjunction with the\n"
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 "pickle module.\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 "Usage:\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 " from cStringIO import StringIO\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 " an_output_stream=StringIO()\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 " an_output_stream.write(some_stuff)\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 " ...\n"
22218
5440e4b17aff backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21313
diff changeset
23 " value=an_output_stream.getvalue()\n"
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 " an_input_stream=StringIO(a_string)\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 " spam=an_input_stream.readline()\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 " spam=an_input_stream.read(5)\n"
9521
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
28 " an_input_stream.seek(0) # OK, start over\n"
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 " spam=an_input_stream.read() # and read it all\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 " \n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 "If someone else wants to provide a more complete implementation,\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 "go for it. :-) \n"
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
33 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
34 "cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n");
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
36 /* Declaration for file-like objects that manage data as strings
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
38 The IOobject type should be though of as a common base type for
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
39 Iobjects, which provide input (read-only) StringIO objects and
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
40 Oobjects, which provide read-write objects. Most of the methods
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
41 depend only on common data.
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
42 */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 typedef struct {
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 PyObject_HEAD
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 char *buf;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
47 Py_ssize_t pos, string_size;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
48 } IOobject;
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
49
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
50 #define IOOOBJECT(O) ((IOobject*)(O))
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
51
30125
4b46d5449d49 shit - just change the visible name, not the comments - strictly speaking,
Skip Montanaro <skip@pobox.com>
parents: 30124
diff changeset
52 /* Declarations for objects of type StringO */
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
53
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
54 typedef struct { /* Subtype of IOobject */
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
55 PyObject_HEAD
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
56 char *buf;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
57 Py_ssize_t pos, string_size;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
58
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
59 Py_ssize_t buf_size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
60 int softspace;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
61 } Oobject;
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
62
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
63 /* Declarations for objects of type StringI */
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
64
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
65 typedef struct { /* Subtype of IOobject */
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
66 PyObject_HEAD
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
67 char *buf;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
68 Py_ssize_t pos, string_size;
20060
ccec02bfe9a0 StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents: 20033
diff changeset
69 /* We store a reference to the object here in order to keep
ccec02bfe9a0 StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents: 20033
diff changeset
70 the buffer alive during the lifetime of the Iobject. */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 PyObject *pbuf;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 } Iobject;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
74 /* IOobject (common) methods */
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
75
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
76 PyDoc_STRVAR(IO_flush__doc__, "flush(): does nothing.");
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
77
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
78 static int
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
79 IO__opencheck(IOobject *self) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
80 if (!self->buf) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
81 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
82 "I/O operation on closed file");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
83 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
84 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
85 return 1;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
86 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
87
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
88 static PyObject *
30096
06016d5d81d7 SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents: 28772
diff changeset
89 IO_get_closed(IOobject *self, void *closure)
06016d5d81d7 SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents: 28772
diff changeset
90 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
91 PyObject *result = Py_False;
30096
06016d5d81d7 SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents: 28772
diff changeset
92
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
93 if (self->buf == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
94 result = Py_True;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
95 Py_INCREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
96 return result;
30096
06016d5d81d7 SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents: 28772
diff changeset
97 }
06016d5d81d7 SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents: 28772
diff changeset
98
06016d5d81d7 SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents: 28772
diff changeset
99 static PyGetSetDef file_getsetlist[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
100 {"closed", (getter)IO_get_closed, NULL, "True if the file is closed"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
101 {0},
30096
06016d5d81d7 SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents: 28772
diff changeset
102 };
06016d5d81d7 SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents: 28772
diff changeset
103
06016d5d81d7 SF bug #770485: cStringIO does not set closed attr
Raymond Hettinger <python@rcn.com>
parents: 28772
diff changeset
104 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
105 IO_flush(IOobject *self, PyObject *unused) {
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
106
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
107 if (!IO__opencheck(self)) return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
108
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
109 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
110 return Py_None;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
111 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
112
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
113 PyDoc_STRVAR(IO_getval__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
114 "getvalue([use_pos]) -- Get the string value."
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
115 "\n"
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
116 "If use_pos is specified and is a true value, then the string returned\n"
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
117 "will include only the text up to the current file position.\n");
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
118
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
119 static PyObject *
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
120 IO_cgetval(PyObject *self) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
121 if (!IO__opencheck(IOOOBJECT(self))) return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
122 assert(IOOOBJECT(self)->pos >= 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
123 return PyString_FromStringAndSize(((IOobject*)self)->buf,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
124 ((IOobject*)self)->pos);
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
125 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
126
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
127 static PyObject *
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
128 IO_getval(IOobject *self, PyObject *args) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
129 PyObject *use_pos=Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
130 Py_ssize_t s;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
131
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
132 if (!IO__opencheck(self)) return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
133 if (!PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
134
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
135 if (PyObject_IsTrue(use_pos)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
136 s=self->pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
137 if (s > self->string_size) s=self->string_size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
138 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
139 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
140 s=self->string_size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
141 assert(self->pos >= 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
142 return PyString_FromStringAndSize(self->buf, s);
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
143 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
144
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
145 PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0");
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
146
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
147 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
148 IO_isatty(IOobject *self, PyObject *unused) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
149 if (!IO__opencheck(self)) return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
150 Py_INCREF(Py_False);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
151 return Py_False;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
152 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
153
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
154 PyDoc_STRVAR(IO_read__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
155 "read([s]) -- Read s characters, or the rest of the string");
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
156
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
157 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
158 IO_cread(PyObject *self, char **output, Py_ssize_t n) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
159 Py_ssize_t l;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
160
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
161 if (!IO__opencheck(IOOOBJECT(self))) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
162 assert(IOOOBJECT(self)->pos >= 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
163 assert(IOOOBJECT(self)->string_size >= 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
164 l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
165 if (n < 0 || n > l) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
166 n = l;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
167 if (n < 0) n=0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
168 }
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
169
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
170 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
171 ((IOobject*)self)->pos += n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
172 return n;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
173 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
174
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
175 static PyObject *
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
176 IO_read(IOobject *self, PyObject *args) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
177 Py_ssize_t n = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
178 char *output = NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
179
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
180 if (!PyArg_ParseTuple(args, "|n:read", &n)) return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
181
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
182 if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
183
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
184 return PyString_FromStringAndSize(output, n);
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
185 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
186
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
187 PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line");
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
188
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
189 static int
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
190 IO_creadline(PyObject *self, char **output) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
191 char *n, *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
192 Py_ssize_t l;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
193
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
194 if (!IO__opencheck(IOOOBJECT(self))) return -1;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
195
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
196 for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
197 s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
198 n < s && *n != '\n'; n++);
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
199
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
200 if (n < s) n++;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
201
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
202 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
203 l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
204
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
205 assert(IOOOBJECT(self)->pos <= PY_SSIZE_T_MAX - l);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
206 assert(IOOOBJECT(self)->pos >= 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
207 assert(IOOOBJECT(self)->string_size >= 0);
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
208
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
209 ((IOobject*)self)->pos += l;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
210 return (int)l;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
211 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
212
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
213 static PyObject *
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
214 IO_readline(IOobject *self, PyObject *args) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
215 int n, m=-1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
216 char *output;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
217
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
218 if (args)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
219 if (!PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
220
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
221 if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
222 if (m >= 0 && m < n) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
223 m = n - m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
224 n -= m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
225 self->pos -= m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
226 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
227 assert(IOOOBJECT(self)->pos >= 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
228 return PyString_FromStringAndSize(output, n);
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
229 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
230
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
231 PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines");
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
232
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
233 static PyObject *
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
234 IO_readlines(IOobject *self, PyObject *args) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
235 int n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
236 char *output;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
237 PyObject *result, *line;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
238 int hint = 0, length = 0;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
239
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
240 if (!PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
241
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
242 result = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
243 if (!result)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
244 return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
245
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
246 while (1){
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
247 if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
248 goto err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
249 if (n == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
250 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
251 line = PyString_FromStringAndSize (output, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
252 if (!line)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
253 goto err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
254 if (PyList_Append (result, line) == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
255 Py_DECREF (line);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
256 goto err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
257 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
258 Py_DECREF (line);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
259 length += n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
260 if (hint > 0 && length >= hint)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
261 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
262 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
263 return result;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
264 err:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
265 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
266 return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
267 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
268
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
269 PyDoc_STRVAR(IO_reset__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
270 "reset() -- Reset the file position to the beginning");
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
273 IO_reset(IOobject *self, PyObject *unused) {
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
274
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
275 if (!IO__opencheck(self)) return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
276
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
277 self->pos = 0;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
278
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
279 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
280 return Py_None;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
281 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
282
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
283 PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position.");
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
284
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
285 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
286 IO_tell(IOobject *self, PyObject *unused) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
288 if (!IO__opencheck(self)) return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
289
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
290 assert(self->pos >= 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
291 return PyInt_FromSsize_t(self->pos);
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
292 }
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
293
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
294 PyDoc_STRVAR(IO_truncate__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
295 "truncate(): truncate the file at the current position.");
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
296
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
297 static PyObject *
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
298 IO_truncate(IOobject *self, PyObject *args) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
299 Py_ssize_t pos = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
300
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
301 if (!IO__opencheck(self)) return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
302 if (!PyArg_ParseTuple(args, "|n:truncate", &pos)) return NULL;
40358
0fdc2b8211f9 Make cStringIO.truncate raise IOError for negative
Martin v. Löwis <martin@v.loewis.de>
parents: 40143
diff changeset
303
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
304 if (PyTuple_Size(args) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
305 /* No argument passed, truncate to current position */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
306 pos = self->pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
307 }
40358
0fdc2b8211f9 Make cStringIO.truncate raise IOError for negative
Martin v. Löwis <martin@v.loewis.de>
parents: 40143
diff changeset
308
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
309 if (pos < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
310 errno = EINVAL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
311 PyErr_SetFromErrno(PyExc_IOError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
312 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
313 }
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
314
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
315 if (self->string_size > pos) self->string_size = pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
316 self->pos = self->string_size;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
317
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
318 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
319 return Py_None;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
321
28772
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
322 static PyObject *
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
323 IO_iternext(Iobject *self)
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
324 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
325 PyObject *next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
326 next = IO_readline((IOobject *)self, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
327 if (!next)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
328 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
329 if (!PyString_GET_SIZE(next)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
330 Py_DECREF(next);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
331 PyErr_SetNone(PyExc_StopIteration);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
332 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
333 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
334 return next;
28772
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
335 }
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
336
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
338
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
339
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
340 /* Read-write object methods */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
341
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
342 PyDoc_STRVAR(O_seek__doc__,
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 "seek(position) -- set the current position\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
344 "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF");
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
347 O_seek(Oobject *self, PyObject *args) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
348 Py_ssize_t position;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
349 int mode = 0;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
350
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
351 if (!IO__opencheck(IOOOBJECT(self))) return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
352 if (!PyArg_ParseTuple(args, "n|i:seek", &position, &mode))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
353 return NULL;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
355 if (mode == 2) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
356 position += self->string_size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
357 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
358 else if (mode == 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
359 position += self->pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
360 }
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
361
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
362 if (position > self->buf_size) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
363 char *newbuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
364 self->buf_size*=2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
365 if (self->buf_size <= position) self->buf_size=position+1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
366 newbuf = (char*) realloc(self->buf,self->buf_size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
367 if (!newbuf) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
368 free(self->buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
369 self->buf = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
370 self->buf_size=self->pos=0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
371 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
372 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
373 self->buf = newbuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
374 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
375 else if (position < 0) position=0;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
376
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
377 self->pos=position;
14679
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
378
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
379 while (--position >= self->string_size) self->buf[position]=0;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
380
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
381 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
382 return Py_None;
14679
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
383 }
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
384
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
385 PyDoc_STRVAR(O_write__doc__,
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 "write(s) -- Write a string to the file"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
387 "\n\nNote (hack:) writing None resets the buffer");
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
391 O_cwrite(PyObject *self, const char *c, Py_ssize_t l) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
392 Py_ssize_t newl;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
393 Oobject *oself;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
394 char *newbuf;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
395
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
396 if (!IO__opencheck(IOOOBJECT(self))) return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
397 oself = (Oobject *)self;
4906
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
398
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
399 newl = oself->pos+l;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
400 if (newl >= oself->buf_size) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
401 oself->buf_size *= 2;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
402 if (oself->buf_size <= newl) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
403 assert(newl + 1 < INT_MAX);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
404 oself->buf_size = (int)(newl+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
405 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
406 newbuf = (char*)realloc(oself->buf, oself->buf_size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
407 if (!newbuf) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
408 PyErr_SetString(PyExc_MemoryError,"out of memory");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
409 free(oself->buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
410 oself->buf = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
411 oself->buf_size = oself->pos = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
412 return -1;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
413 }
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
414 oself->buf = newbuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
415 }
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
416
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
417 memcpy(oself->buf+oself->pos,c,l);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
418
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
419 assert(oself->pos + l < INT_MAX);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
420 oself->pos += (int)l;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
421
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
422 if (oself->string_size < oself->pos) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
423 oself->string_size = oself->pos;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
424 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
425
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
426 return (int)l;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
428
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
430 O_write(Oobject *self, PyObject *args) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
431 char *c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
432 int l;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
433
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
434 if (!PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL;
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
435
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
436 if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
437
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
438 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
439 return Py_None;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
441
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
442 PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held.");
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
445 O_close(Oobject *self, PyObject *unused) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
446 if (self->buf != NULL) free(self->buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
447 self->buf = NULL;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
448
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
449 self->pos = self->string_size = self->buf_size = 0;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
450
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
451 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
452 return Py_None;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
454
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
455 PyDoc_STRVAR(O_writelines__doc__,
31626
1543afab1b67 SF patch #907403: Improvements to cStringIO.writelines()
Raymond Hettinger <python@rcn.com>
parents: 31593
diff changeset
456 "writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
1543afab1b67 SF patch #907403: Improvements to cStringIO.writelines()
Raymond Hettinger <python@rcn.com>
parents: 31593
diff changeset
457 "\n"
1543afab1b67 SF patch #907403: Improvements to cStringIO.writelines()
Raymond Hettinger <python@rcn.com>
parents: 31593
diff changeset
458 "Note that newlines are not added. The sequence can be any iterable object\n"
1543afab1b67 SF patch #907403: Improvements to cStringIO.writelines()
Raymond Hettinger <python@rcn.com>
parents: 31593
diff changeset
459 "producing strings. This is equivalent to calling write() for each string.");
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
461 O_writelines(Oobject *self, PyObject *args) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
462 PyObject *it, *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
464 it = PyObject_GetIter(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
465 if (it == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
466 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
467 while ((s = PyIter_Next(it)) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
468 Py_ssize_t n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
469 char *c;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
470 if (PyString_AsStringAndSize(s, &c, &n) == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
471 Py_DECREF(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
472 Py_DECREF(s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
473 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
474 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
475 if (O_cwrite((PyObject *)self, c, n) == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
476 Py_DECREF(it);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
477 Py_DECREF(s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
478 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
479 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
480 Py_DECREF(s);
35623
774bf9a38b2a Patches #1298449 and #1298499: Add some missing checks for error
Michael W. Hudson <mwh@python.net>
parents: 33213
diff changeset
481 }
774bf9a38b2a Patches #1298449 and #1298499: Add some missing checks for error
Michael W. Hudson <mwh@python.net>
parents: 33213
diff changeset
482
774bf9a38b2a Patches #1298449 and #1298499: Add some missing checks for error
Michael W. Hudson <mwh@python.net>
parents: 33213
diff changeset
483 Py_DECREF(it);
774bf9a38b2a Patches #1298449 and #1298499: Add some missing checks for error
Michael W. Hudson <mwh@python.net>
parents: 33213
diff changeset
484
774bf9a38b2a Patches #1298449 and #1298499: Add some missing checks for error
Michael W. Hudson <mwh@python.net>
parents: 33213
diff changeset
485 /* See if PyIter_Next failed */
774bf9a38b2a Patches #1298449 and #1298499: Add some missing checks for error
Michael W. Hudson <mwh@python.net>
parents: 33213
diff changeset
486 if (PyErr_Occurred())
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
487 return NULL;
35623
774bf9a38b2a Patches #1298449 and #1298499: Add some missing checks for error
Michael W. Hudson <mwh@python.net>
parents: 33213
diff changeset
488
774bf9a38b2a Patches #1298449 and #1298499: Add some missing checks for error
Michael W. Hudson <mwh@python.net>
parents: 33213
diff changeset
489 Py_RETURN_NONE;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 static struct PyMethodDef O_methods[] = {
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
492 /* Common methods: */
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
493 {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
494 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
495 {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
496 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
497 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
498 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
499 {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
500 {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
501 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
502
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
503 /* Read-write StringIO specific methods: */
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
504 {"close", (PyCFunction)O_close, METH_NOARGS, O_close__doc__},
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
505 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
506 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
507 {"writelines", (PyCFunction)O_writelines, METH_O, O_writelines__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
508 {NULL, NULL} /* sentinel */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
510
28772
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
511 static PyMemberDef O_memberlist[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
512 {"softspace", T_INT, offsetof(Oobject, softspace), 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
513 "flag indicating that a space needs to be printed; used by print"},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
514 /* getattr(f, "closed") is implemented without this table */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
515 {NULL} /* Sentinel */
28772
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
516 };
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
517
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 static void
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
519 O_dealloc(Oobject *self) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
520 if (self->buf != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
521 free(self->buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
522 PyObject_Del(self);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
524
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
525 PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings.");
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
526
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 static PyTypeObject Otype = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41525
diff changeset
528 PyVarObject_HEAD_INIT(NULL, 0)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
529 "cStringIO.StringO", /*tp_name*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
530 sizeof(Oobject), /*tp_basicsize*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
531 0, /*tp_itemsize*/
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
532 /* methods */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
533 (destructor)O_dealloc, /*tp_dealloc*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
534 0, /*tp_print*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
535 0, /*tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
536 0, /*tp_setattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
537 0, /*tp_compare*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
538 0, /*tp_repr*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
539 0, /*tp_as_number*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
540 0, /*tp_as_sequence*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
541 0, /*tp_as_mapping*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
542 0, /*tp_hash*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
543 0 , /*tp_call*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
544 0, /*tp_str*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
545 0, /*tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
546 0, /*tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
547 0, /*tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
548 Py_TPFLAGS_DEFAULT, /*tp_flags*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
549 Otype__doc__, /*tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
550 0, /*tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
551 0, /*tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
552 0, /*tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
553 0, /*tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
554 PyObject_SelfIter, /*tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
555 (iternextfunc)IO_iternext, /*tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
556 O_methods, /*tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
557 O_memberlist, /*tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
558 file_getsetlist, /*tp_getset */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
560
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
561 static PyObject *
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
562 newOobject(int size) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
563 Oobject *self;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
564
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
565 self = PyObject_New(Oobject, &Otype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
566 if (self == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
567 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
568 self->pos=0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
569 self->string_size = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
570 self->softspace = 0;
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
571
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
572 self->buf = (char *)malloc(size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
573 if (!self->buf) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
574 PyErr_SetString(PyExc_MemoryError,"out of memory");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
575 self->buf_size = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
576 Py_DECREF(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
577 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
578 }
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
579
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
580 self->buf_size=size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
581 return (PyObject*)self;
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
582 }
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
583
30125
4b46d5449d49 shit - just change the visible name, not the comments - strictly speaking,
Skip Montanaro <skip@pobox.com>
parents: 30124
diff changeset
584 /* End of code for StringO objects */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 /* -------------------------------------------------------- */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
586
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 static PyObject *
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
588 I_close(Iobject *self, PyObject *unused) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
589 Py_CLEAR(self->pbuf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
590 self->buf = NULL;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
591
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
592 self->pos = self->string_size = 0;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
593
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
594 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
595 return Py_None;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
596 }
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
597
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
598 static PyObject *
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
599 I_seek(Iobject *self, PyObject *args) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
600 Py_ssize_t position;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
601 int mode = 0;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
602
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
603 if (!IO__opencheck(IOOOBJECT(self))) return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
604 if (!PyArg_ParseTuple(args, "n|i:seek", &position, &mode))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
605 return NULL;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
606
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
607 if (mode == 2) position += self->string_size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
608 else if (mode == 1) position += self->pos;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
609
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
610 if (position < 0) position=0;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
611
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
612 self->pos=position;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
613
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
614 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
615 return Py_None;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
617
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 static struct PyMethodDef I_methods[] = {
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
619 /* Common methods: */
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
620 {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
621 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
622 {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
623 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
624 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
625 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
626 {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
627 {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
628 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
629
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
630 /* Read-only StringIO specific methods: */
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
631 {"close", (PyCFunction)I_close, METH_NOARGS, O_close__doc__},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
632 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
633 {NULL, NULL}
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
635
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 static void
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
637 I_dealloc(Iobject *self) {
6215
eb7a01b86f93 Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents: 5991
diff changeset
638 Py_XDECREF(self->pbuf);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12385
diff changeset
639 PyObject_Del(self);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
641
20033
104b5c9b88ea I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents: 16546
diff changeset
642
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
643 PyDoc_STRVAR(Itype__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
644 "Simple type for treating strings as input file streams");
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
645
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 static PyTypeObject Itype = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41525
diff changeset
647 PyVarObject_HEAD_INIT(NULL, 0)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
648 "cStringIO.StringI", /*tp_name*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
649 sizeof(Iobject), /*tp_basicsize*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
650 0, /*tp_itemsize*/
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
651 /* methods */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
652 (destructor)I_dealloc, /*tp_dealloc*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
653 0, /*tp_print*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
654 0, /* tp_getattr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
655 0, /*tp_setattr*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
656 0, /*tp_compare*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
657 0, /*tp_repr*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
658 0, /*tp_as_number*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
659 0, /*tp_as_sequence*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
660 0, /*tp_as_mapping*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
661 0, /*tp_hash*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
662 0, /*tp_call*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
663 0, /*tp_str*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
664 0, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
665 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
666 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
667 Py_TPFLAGS_DEFAULT, /* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
668 Itype__doc__, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
669 0, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
670 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
671 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
672 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
673 PyObject_SelfIter, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
674 (iternextfunc)IO_iternext, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
675 I_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
676 0, /* tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
677 file_getsetlist, /* tp_getset */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
679
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
680 static PyObject *
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
681 newIobject(PyObject *s) {
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
682 Iobject *self;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
683 char *buf;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
684 Py_ssize_t size;
12385
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
685
42494
3f6be12b9534 Fix compilation warning.
Georg Brandl <georg@python.org>
parents: 42492
diff changeset
686 if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
42492
86fd39c2c676 Revert the fix for #1548891, it broke backwards compatibility with arbitrary read buffers.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
687 PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
86fd39c2c676 Revert the fix for #1548891, it broke backwards compatibility with arbitrary read buffers.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
688 s->ob_type->tp_name);
86fd39c2c676 Revert the fix for #1548891, it broke backwards compatibility with arbitrary read buffers.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
689 return NULL;
86fd39c2c676 Revert the fix for #1548891, it broke backwards compatibility with arbitrary read buffers.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
690 }
40099
647598c1a9d9 Bug #1548891: The cStringIO.StringIO() constructor now encodes unicode
Georg Brandl <georg@python.org>
parents: 37305
diff changeset
691
36790
48a28b35dd64 Remove UNLESS.
Martin v. Löwis <martin@v.loewis.de>
parents: 36782
diff changeset
692 self = PyObject_New(Iobject, &Itype);
48a28b35dd64 Remove UNLESS.
Martin v. Löwis <martin@v.loewis.de>
parents: 36782
diff changeset
693 if (!self) return NULL;
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
694 Py_INCREF(s);
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
695 self->buf=buf;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
696 self->string_size=size;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
697 self->pbuf=s;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
698 self->pos=0;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
699
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
700 return (PyObject*)self;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
701 }
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
702
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 /* End of code for StringI objects */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 /* -------------------------------------------------------- */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
705
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
706
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
707 PyDoc_STRVAR(IO_StringIO__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23301
diff changeset
708 "StringIO([s]) -- Return a StringIO-like stream for reading or writing");
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
709
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
711 IO_StringIO(PyObject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 PyObject *s=0;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
713
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 25087
diff changeset
714 if (!PyArg_UnpackTuple(args, "StringIO", 0, 1, &s)) return NULL;
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
715
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
716 if (s) return newIobject(s);
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
717 return newOobject(128);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
719
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 /* List of methods defined in the module */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
721
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 static struct PyMethodDef IO_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
723 {"StringIO", (PyCFunction)IO_StringIO,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
724 METH_VARARGS, IO_StringIO__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
725 {NULL, NULL} /* sentinel */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
727
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
728
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 /* Initialization function for the module (*must* be called initcStringIO) */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
730
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
731 static struct PycStringIO_CAPI CAPI = {
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
732 IO_cread,
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
733 IO_creadline,
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
734 O_cwrite,
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
735 IO_cgetval,
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
736 newOobject,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
737 newIobject,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
738 &Itype,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
739 &Otype,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
740 };
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
741
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
742 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
743 #define PyMODINIT_FUNC void
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
744 #endif
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
745 PyMODINIT_FUNC
13690
c09c0171474f ANSIfy some more forward declarations.
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
746 initcStringIO(void) {
6227
613f976a57b4 #Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents: 6215
diff changeset
747 PyObject *m, *d, *v;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
748
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
749
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 /* Create the module and add the functions */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 m = Py_InitModule4("cStringIO", IO_methods,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
752 cStringIO_module_documentation,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
753 (PyObject*)NULL,PYTHON_API_VERSION);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
754 if (m == NULL) return;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
755
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 /* Add some symbolic constants to the module */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 d = PyModule_GetDict(m);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59998
diff changeset
758
4906
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
759 /* Export C API */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42494
diff changeset
760 Py_TYPE(&Itype)=&PyType_Type;
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42494
diff changeset
761 Py_TYPE(&Otype)=&PyType_Type;
28772
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
762 if (PyType_Ready(&Otype) < 0) return;
0ea5991f6edb SF patch 695710: fix bug 678519: cStringIO self iterator
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
763 if (PyType_Ready(&Itype) < 0) return;
59998
d11fbc796499 Backported PyCapsule from 3.1, and converted most uses of
Larry Hastings <larry@hastings.org>
parents: 47587
diff changeset
764 v = PyCapsule_New(&CAPI, PycStringIO_CAPSULE_NAME, NULL);
d11fbc796499 Backported PyCapsule from 3.1, and converted most uses of
Larry Hastings <larry@hastings.org>
parents: 47587
diff changeset
765 PyDict_SetItemString(d,"cStringIO_CAPI", v);
6227
613f976a57b4 #Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents: 6215
diff changeset
766 Py_XDECREF(v);
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
767
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
768 /* Export Types */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
771
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
772 /* Maybe make certain warnings go away */
15059
acb88607b4bd Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents: 14679
diff changeset
773 if (0) PycString_IMPORT;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
774 }