annotate Modules/cStringIO.c @ 14914:79cdeeeefafa legacy-trunk

Removed the implib_dir instance variable because it is unused. Removed get_ext_libname() because it is unused. Fixed get_libraries() to append an '_d' to the python debug import library. If MSVC is used, do not add 'pythonxx.lib' to the list of libraries, because this is handled better by a pragma in config.h. This should fix bug #115595, but it needs some more testing.
author Thomas Heller <theller@ctypes.org>
date Thu, 28 Sep 2000 19:28:35 +0000
parents 08823099ae62
children acb88607b4bd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /*
10985
63b530c030c5 New version from Jim: don't call Py_FatalError() when initialization fails.
Guido van Rossum <guido@python.org>
parents: 10119
diff changeset
2 * cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp
9521
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
3 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
4 * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
5 * All rights reserved.
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
6 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
7 * Redistribution and use in source and binary forms, with or without
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
8 * modification, are permitted provided that the following conditions are
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
9 * met:
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
10 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
11 * o Redistributions of source code must retain the above copyright
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
12 * notice, this list of conditions, and the disclaimer that follows.
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
13 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
14 * o Redistributions in binary form must reproduce the above copyright
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
15 * notice, this list of conditions, and the following disclaimer in
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
16 * the documentation and/or other materials provided with the
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
17 * distribution.
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
18 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
19 * o All advertising materials mentioning features or use of this
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
20 * software must display the following acknowledgement:
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
21 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
22 * This product includes software developed by Digital Creations
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
23 * and its contributors.
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
24 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
25 * o Neither the name of Digital Creations nor the names of its
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
26 * contributors may be used to endorse or promote products derived
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
27 * from this software without specific prior written permission.
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
28 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
29 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
30 * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
31 * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
33 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
34 * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
36 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
37 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
38 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
39 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
40 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
41 * DAMAGE.
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
42 *
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
43 #
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
44 # If you have questions regarding this software, contact:
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
45 #
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
46 # Digital Creations, L.C.
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
47 # 910 Princess Ann Street
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
48 # Fredericksburge, Virginia 22401
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
49 #
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
50 # info@digicool.com
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
51 #
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
52 # (540) 371-6909
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
53 */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 static char cStringIO_module_documentation[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 "A simple fast partial StringIO replacement.\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 "This module provides a simple useful replacement for\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 "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
59 "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
60 "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
61 "pickle module.\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 "Usage:\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 " from cStringIO import StringIO\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 " an_output_stream=StringIO()\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 " an_output_stream.write(some_stuff)\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 " ...\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 " value=an_output_stream.getvalue() # str(an_output_stream) works too!\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 "\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 " an_input_stream=StringIO(a_string)\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 " spam=an_input_stream.readline()\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 " spam=an_input_stream.read(5)\n"
9521
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
75 " an_input_stream.seek(0) # OK, start over\n"
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 " spam=an_input_stream.read() # and read it all\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 " \n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 "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
79 "go for it. :-) \n"
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
80 "\n"
10985
63b530c030c5 New version from Jim: don't call Py_FatalError() when initialization fails.
Guido van Rossum <guido@python.org>
parents: 10119
diff changeset
81 "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
82 ;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 #include "Python.h"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 #include "import.h"
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
86 #include "cStringIO.h"
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 #define UNLESS(E) if(!(E))
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 /* Declarations for objects of type StringO */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 typedef struct {
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 PyObject_HEAD
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 char *buf;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
95 int pos, string_size, buf_size, softspace;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 } Oobject;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 /* Declarations for objects of type StringI */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 typedef struct {
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 PyObject_HEAD
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 char *buf;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
103 int pos, string_size;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 PyObject *pbuf;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 } Iobject;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 static char O_reset__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 "reset() -- Reset the file position to the beginning"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 ;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
112 O_reset(Oobject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 self->pos = 0;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 Py_INCREF(Py_None);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return Py_None;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 static char O_tell__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 "tell() -- get the current position.";
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
124 O_tell(Oobject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 return PyInt_FromLong(self->pos);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 static char O_seek__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 "seek(position) -- set the current position\n"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
134 O_seek(Oobject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 int position, mode = 0;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10985
diff changeset
137 UNLESS(PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 return NULL;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
6740
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
141 if (mode == 2) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 position += self->string_size;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 }
6740
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
144 else if (mode == 1) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 position += self->pos;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
9521
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
148 if (position > self->buf_size) {
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
149 self->buf_size*=2;
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
150 if(self->buf_size <= position) self->buf_size=position+1;
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
151 UNLESS(self->buf=(char*)realloc(self->buf,self->buf_size*sizeof(char))) {
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
152 self->buf_size=self->pos=0;
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
153 return PyErr_NoMemory();
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
154 }
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
155 }
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
156 else if(position < 0) position=0;
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
157
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
158 self->pos=position;
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
159
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
160 while(--position >= self->string_size) self->buf[position]=0;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
4906
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
162 Py_INCREF(Py_None);
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
163 return Py_None;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 static char O_read__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 "read([s]) -- Read s characters, or the rest of the string"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 ;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 static int
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
171 O_cread(PyObject *self, char **output, int n) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 int l;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
174 l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos;
6740
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
175 if (n < 0 || n > l) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 n = l;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
177 if (n < 0) n=0;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
180 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
181 ((Oobject*)self)->pos += n;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 return n;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
186 O_read(Oobject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 int n = -1;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 char *output;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10985
diff changeset
190 UNLESS(PyArg_ParseTuple(args, "|i:read", &n)) return NULL;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
192 n=O_cread((PyObject*)self,&output,n);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 return PyString_FromStringAndSize(output, n);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
197
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 static char O_readline__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 "readline() -- Read one line"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 ;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 static int
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
203 O_creadline(PyObject *self, char **output) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 char *n, *s;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 int l;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
206
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
207 for (n = ((Oobject*)self)->buf + ((Oobject*)self)->pos,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
208 s = ((Oobject*)self)->buf + ((Oobject*)self)->string_size;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 n < s && *n != '\n'; n++);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 if (n < s) n++;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
212 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
213 l = n - ((Oobject*)self)->buf - ((Oobject*)self)->pos;
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
214 ((Oobject*)self)->pos += l;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 return l;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
219 O_readline(Oobject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 int n;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 char *output;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
223 n=O_creadline((PyObject*)self,&output);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 return PyString_FromStringAndSize(output, n);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
14679
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
227 static char O_readlines__doc__[] =
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
228 "readlines() -- Read all lines"
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
229 ;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
230
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
231 static PyObject *
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
232 O_readlines(Oobject *self, PyObject *args) {
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
233 int n;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
234 char *output;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
235 PyObject *result, *line;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
236 int hint = 0, length = 0;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
237
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
238 UNLESS(PyArg_ParseTuple(args, "|i:write", &hint)) return NULL;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
239 result = PyList_New(0);
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
240 if (!result)
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
241 return NULL;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
242
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
243 while(1){
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
244 n = O_creadline((PyObject*)self,&output);
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
245 if (n == 0)
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
246 break;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
247 line = PyString_FromStringAndSize (output, n);
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
248 if (!line){
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
249 Py_DECREF (result);
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
250 return NULL;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
251 }
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
252 PyList_Append (result, line);
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
253 Py_DECREF (line);
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
254 length += n;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
255 if (hint > 0 && length >= hint)
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
256 break;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
257 }
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
258 return result;
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
259 }
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
260
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 static char O_write__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 "write(s) -- Write a string to the file"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 "\n\nNote (hack:) writing None resets the buffer"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 ;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
266
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 static int
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
268 O_cwrite(PyObject *self, char *c, int l) {
4957
9d58d0e9877e Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents: 4906
diff changeset
269 int newl;
4906
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
270
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
271 newl=((Oobject*)self)->pos+l;
6740
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
272 if(newl >= ((Oobject*)self)->buf_size) {
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
273 ((Oobject*)self)->buf_size*=2;
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
274 if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1;
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
275 UNLESS(((Oobject*)self)->buf=
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
276 (char*)realloc(((Oobject*)self)->buf,
6740
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
277 (((Oobject*)self)->buf_size) *sizeof(char))) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 PyErr_SetString(PyExc_MemoryError,"out of memory");
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
279 ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 return -1;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
283
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
284 memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
286 ((Oobject*)self)->pos += l;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
6740
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
288 if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) {
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
289 ((Oobject*)self)->string_size = ((Oobject*)self)->pos;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
291
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 return l;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
294
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
296 O_write(Oobject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 PyObject *s;
4957
9d58d0e9877e Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents: 4906
diff changeset
298 char *c;
9d58d0e9877e Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents: 4906
diff changeset
299 int l;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
300
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10985
diff changeset
301 UNLESS(PyArg_ParseTuple(args, "O:write", &s)) return NULL;
4906
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
302 UNLESS(-1 != (l=PyString_Size(s))) return NULL;
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
303 UNLESS(c=PyString_AsString(s)) return NULL;
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
304 UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
305
4906
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
306 Py_INCREF(Py_None);
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
307 return Py_None;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
310 static char O_getval__doc__[] =
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
311 "getvalue([use_pos]) -- Get the string value."
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
312 "\n"
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
313 "If use_pos is specified and is a true value, then the string returned\n"
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
314 "will include only the text up to the current file position.\n"
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
315 ;
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
316
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
318 O_getval(Oobject *self, PyObject *args) {
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
319 PyObject *use_pos=Py_None;
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
320 int s;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
321
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
322 use_pos=Py_None;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10985
diff changeset
323 UNLESS(PyArg_ParseTuple(args,"|O:getval",&use_pos)) return NULL;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
324 if(PyObject_IsTrue(use_pos)) {
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
325 s=self->pos;
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
326 if (s > self->string_size) s=self->string_size;
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
327 }
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
328 else
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
329 s=self->string_size;
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
330 return PyString_FromStringAndSize(self->buf, s);
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
331 }
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
332
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
333 static PyObject *
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
334 O_cgetval(PyObject *self) {
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
335 return PyString_FromStringAndSize(((Oobject*)self)->buf,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
336 ((Oobject*)self)->pos);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
338
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 static char O_truncate__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 "truncate(): truncate the file at the current position.";
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
341
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
343 O_truncate(Oobject *self, PyObject *args) {
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
344 if (self->string_size > self->pos)
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
345 self->string_size = self->pos;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 Py_INCREF(Py_None);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 return Py_None;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 static char O_isatty__doc__[] = "isatty(): always returns 0";
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
351
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
353 O_isatty(Oobject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 return PyInt_FromLong(0);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 static char O_close__doc__[] = "close(): explicitly release resources held.";
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
358
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
360 O_close(Oobject *self, PyObject *args) {
6215
eb7a01b86f93 Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents: 5991
diff changeset
361 if (self->buf != NULL)
eb7a01b86f93 Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents: 5991
diff changeset
362 free(self->buf);
eb7a01b86f93 Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents: 5991
diff changeset
363 self->buf = NULL;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 self->pos = self->string_size = self->buf_size = 0;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
366
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 Py_INCREF(Py_None);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 return Py_None;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
370
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 static char O_flush__doc__[] = "flush(): does nothing.";
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
372
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
374 O_flush(Oobject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 Py_INCREF(Py_None);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 return Py_None;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
378
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
379
10119
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
380 static char O_writelines__doc__[] =
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
381 "writelines(sequence_of_strings): write each string";
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
383 O_writelines(Oobject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 PyObject *string_module = 0;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 static PyObject *string_joinfields = 0;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
386
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10985
diff changeset
387 UNLESS(PyArg_ParseTuple(args, "O:writelines", &args)) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 return NULL;
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
6740
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
391 if (!string_joinfields) {
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
392 UNLESS(string_module = PyImport_ImportModule("string")) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 return NULL;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 UNLESS(string_joinfields=
6740
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
397 PyObject_GetAttrString(string_module, "joinfields")) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 return NULL;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
400
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 Py_DECREF(string_module);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
13576
61e4c29d53d8 replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12959
diff changeset
404 if (PyObject_Size(args) == -1) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 return NULL;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
10119
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
408 {
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
409 PyObject *x = PyObject_CallFunction(string_joinfields,
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
410 "Os", args, "");
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
411 if (x == NULL)
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
412 return NULL;
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
413 args = Py_BuildValue("(O)", x);
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
414 Py_DECREF(x);
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
415 if (args == NULL)
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
416 return NULL;
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
417 x = O_write(self, args);
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
418 Py_DECREF(args);
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
419 return x;
a63c9c3e9ecc The writelines() function was never tested and contained numerous bugs
Guido van Rossum <guido@python.org>
parents: 9642
diff changeset
420 }
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
422
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 static struct PyMethodDef O_methods[] = {
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
424 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
425 {"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
426 {"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
14679
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
427 {"readlines", (PyCFunction)O_readlines, METH_VARARGS, O_readlines__doc__},
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
428 {"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
429 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
430 {"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
431 {"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
432 {"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
433 {"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
434 {"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
435 {"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
436 {"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
437 {NULL, NULL} /* sentinel */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
439
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 static void
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
442 O_dealloc(Oobject *self) {
6215
eb7a01b86f93 Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents: 5991
diff changeset
443 if (self->buf != NULL)
eb7a01b86f93 Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents: 5991
diff changeset
444 free(self->buf);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12385
diff changeset
445 PyObject_Del(self);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
447
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
449 O_getattr(Oobject *self, char *name) {
9521
0f27257fb6cf New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents: 8741
diff changeset
450 if (strcmp(name, "softspace") == 0) {
5315
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
451 return PyInt_FromLong(self->softspace);
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
452 }
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 return Py_FindMethod(O_methods, (PyObject *)self, name);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
455
5315
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
456 static int
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
457 O_setattr(Oobject *self, char *name, PyObject *value) {
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
458 long x;
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
459 if (strcmp(name, "softspace") != 0) {
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
460 PyErr_SetString(PyExc_AttributeError, name);
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
461 return -1;
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
462 }
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
463 x = PyInt_AsLong(value);
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
464 if (x == -1 && PyErr_Occurred())
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
465 return -1;
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
466 self->softspace = x;
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
467 return 0;
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
468 }
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
469
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 static char Otype__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 "Simple type for output to strings."
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 ;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
473
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 static PyTypeObject Otype = {
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
475 PyObject_HEAD_INIT(NULL)
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
476 0, /*ob_size*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
477 "StringO", /*tp_name*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
478 sizeof(Oobject), /*tp_basicsize*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
479 0, /*tp_itemsize*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
480 /* methods */
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
481 (destructor)O_dealloc, /*tp_dealloc*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
482 (printfunc)0, /*tp_print*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
483 (getattrfunc)O_getattr, /*tp_getattr*/
5315
65c9e90d8142 My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents: 5280
diff changeset
484 (setattrfunc)O_setattr, /*tp_setattr*/
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
485 (cmpfunc)0, /*tp_compare*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
486 (reprfunc)0, /*tp_repr*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
487 0, /*tp_as_number*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
488 0, /*tp_as_sequence*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
489 0, /*tp_as_mapping*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
490 (hashfunc)0, /*tp_hash*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
491 (ternaryfunc)0, /*tp_call*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
492 (reprfunc)0, /*tp_str*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
493
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
494 /* Space for future expansion */
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
495 0L,0L,0L,0L,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
496 Otype__doc__ /* Documentation string */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
498
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
499 static PyObject *
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
500 newOobject(int size) {
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
501 Oobject *self;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
502
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12385
diff changeset
503 self = PyObject_New(Oobject, &Otype);
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
504 if (self == NULL)
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
505 return NULL;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
506 self->pos=0;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
507 self->string_size = 0;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
508 self->softspace = 0;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
509
6740
1853b5bc0548 New versions of cPickle and cStringIO, from Jim Fulton's cPickle 1.0b1
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
510 UNLESS(self->buf=malloc(size*sizeof(char))) {
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
511 PyErr_SetString(PyExc_MemoryError,"out of memory");
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
512 self->buf_size = 0;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
513 return NULL;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
514 }
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
515
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
516 self->buf_size=size;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
517 return (PyObject*)self;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
518 }
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
519
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 /* End of code for StringO objects */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 /* -------------------------------------------------------- */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
522
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
524 I_close(Iobject *self, PyObject *args) {
6215
eb7a01b86f93 Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents: 5991
diff changeset
525 Py_XDECREF(self->pbuf);
eb7a01b86f93 Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents: 5991
diff changeset
526 self->pbuf = NULL;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
527
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 self->pos = self->string_size = 0;
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
529
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
530 Py_INCREF(Py_None);
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
531 return Py_None;
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
532 }
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
533
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
534 static PyObject *
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
535 I_seek(Oobject *self, PyObject *args) {
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
536 int position, mode = 0;
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
537
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 10985
diff changeset
538 UNLESS(PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) {
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
539 return NULL;
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
540 }
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
541
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
542 if (mode == 2) {
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
543 position += self->string_size;
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
544 }
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
545 else if (mode == 1) {
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
546 position += self->pos;
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
547 }
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
548
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
549 if(position < 0) position=0;
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
550
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
551 self->pos=position;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
552
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 Py_INCREF(Py_None);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 return Py_None;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
556
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 static struct PyMethodDef I_methods[] = {
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
558 {"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
559 {"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
14679
08823099ae62 Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents: 13817
diff changeset
560 {"readlines", (PyCFunction)O_readlines,METH_VARARGS, O_readlines__doc__},
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
561 {"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
562 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
563 {"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
564 {"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
565 {"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
566 {"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
567 {"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
568 {"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__},
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
569 {NULL, NULL}
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
571
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 static void
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
573 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
574 Py_XDECREF(self->pbuf);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12385
diff changeset
575 PyObject_Del(self);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
577
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
579 I_getattr(Iobject *self, char *name) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 return Py_FindMethod(I_methods, (PyObject *)self, name);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
582
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 static char Itype__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 "Simple type for treating strings as input file streams"
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 PyTypeObject Itype = {
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
588 PyObject_HEAD_INIT(NULL)
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
589 0, /*ob_size*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
590 "StringI", /*tp_name*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
591 sizeof(Iobject), /*tp_basicsize*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
592 0, /*tp_itemsize*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
593 /* methods */
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
594 (destructor)I_dealloc, /*tp_dealloc*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
595 (printfunc)0, /*tp_print*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
596 (getattrfunc)I_getattr, /*tp_getattr*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
597 (setattrfunc)0, /*tp_setattr*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
598 (cmpfunc)0, /*tp_compare*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
599 (reprfunc)0, /*tp_repr*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
600 0, /*tp_as_number*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
601 0, /*tp_as_sequence*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
602 0, /*tp_as_mapping*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
603 (hashfunc)0, /*tp_hash*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
604 (ternaryfunc)0, /*tp_call*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
605 (reprfunc)0, /*tp_str*/
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
606
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
607 /* Space for future expansion */
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
608 0L,0L,0L,0L,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
609 Itype__doc__ /* Documentation string */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
611
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
612 static PyObject *
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
613 newIobject(PyObject *s) {
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
614 Iobject *self;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
615 char *buf;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
616 int size;
12385
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
617
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
618 if (!PyString_Check(s)) {
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
619 PyErr_Format(PyExc_TypeError, "expected string, %.200s found",
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
620 s->ob_type->tp_name);
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
621 return NULL;
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
622 }
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
623 buf = PyString_AS_STRING(s);
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
624 size = PyString_GET_SIZE(s);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12385
diff changeset
625 UNLESS(self = PyObject_New(Iobject, &Itype)) return NULL;
5991
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
626 Py_INCREF(s);
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
627 self->buf=buf;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
628 self->string_size=size;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
629 self->pbuf=s;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
630 self->pos=0;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
631
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
632 return (PyObject*)self;
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
633 }
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
634
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 /* End of code for StringI objects */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 /* -------------------------------------------------------- */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
637
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
638
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 static char IO_StringIO__doc__[] =
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 "StringIO([s]) -- Return a StringIO-like stream for reading or writing"
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 ;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
642
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 static PyObject *
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
644 IO_StringIO(PyObject *self, PyObject *args) {
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 PyObject *s=0;
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
646
12385
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
647 if (!PyArg_ParseTuple(args, "|O:StringIO", &s))
627226df9f01 raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11882
diff changeset
648 return NULL;
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
649 if(s) return newIobject(s);
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
650 return newOobject(128);
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 }
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
652
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 /* List of methods defined in the module */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
654
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 static struct PyMethodDef IO_methods[] = {
13817
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13690
diff changeset
656 {"StringIO", (PyCFunction)IO_StringIO,
3c0f75a2198f Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents: 13690
diff changeset
657 METH_VARARGS, IO_StringIO__doc__},
4906
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
658 {NULL, NULL} /* sentinel */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 };
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
660
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
661
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 /* Initialization function for the module (*must* be called initcStringIO) */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
663
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
664 static struct PycStringIO_CAPI CAPI = {
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
665 O_cread,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
666 O_creadline,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
667 O_cwrite,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
668 O_cgetval,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
669 newOobject,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
670 newIobject,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
671 &Itype,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
672 &Otype,
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
673 };
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
674
9642
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
675 #ifndef DL_EXPORT /* declarations for DLL import/export */
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
676 #define DL_EXPORT(RTYPE) RTYPE
08379c7f5fa9 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
677 #endif
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 9521
diff changeset
678 DL_EXPORT(void)
13690
c09c0171474f ANSIfy some more forward declarations.
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
679 initcStringIO(void) {
6227
613f976a57b4 #Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents: 6215
diff changeset
680 PyObject *m, *d, *v;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
681
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
682
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 /* Create the module and add the functions */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 m = Py_InitModule4("cStringIO", IO_methods,
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
685 cStringIO_module_documentation,
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 (PyObject*)NULL,PYTHON_API_VERSION);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
687
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 /* Add some symbolic constants to the module */
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 d = PyModule_GetDict(m);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
690
4906
fce781b127b9 Jim's latest version.
Guido van Rossum <guido@python.org>
parents: 4598
diff changeset
691 /* Export C API */
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
692 Itype.ob_type=&PyType_Type;
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
693 Otype.ob_type=&PyType_Type;
6227
613f976a57b4 #Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents: 6215
diff changeset
694 PyDict_SetItemString(d,"cStringIO_CAPI",
613f976a57b4 #Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents: 6215
diff changeset
695 v = PyCObject_FromVoidPtr(&CAPI,NULL));
613f976a57b4 #Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents: 6215
diff changeset
696 Py_XDECREF(v);
5280
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
697
e8f8cf358054 Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents: 4957
diff changeset
698 /* Export Types */
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 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
701
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
702 /* Maybe make certain warnings go away */
a5737dd96979 cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents: 5315
diff changeset
703 if(0) PycString_IMPORT;
4598
8fdb0539cfb1 Jim F's brainchild
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 }