blob: 332bdf15244e2971a2a7916be2f84c3f7d128eb1 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{StringIO} ---
Fred Drake543e19d1999-04-21 18:15:22 +00002 Read and write strings as files}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{StringIO}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Read and write strings as if they were files.}
6
Guido van Rossumcda3d7d1997-03-03 16:01:21 +00007
Fred Drake8fe533e1998-03-27 05:27:08 +00008This module implements a file-like class, \class{StringIO},
Fred Drakeaf8a0151998-01-14 14:51:31 +00009that reads and writes a string buffer (also known as \emph{memory
Fred Drake543e19d1999-04-21 18:15:22 +000010files}). See the description on file objects for operations (section
11\ref{bltin-file-objects}).
Guido van Rossumcda3d7d1997-03-03 16:01:21 +000012
Fred Drake8fe533e1998-03-27 05:27:08 +000013\begin{classdesc}{StringIO}{\optional{buffer}}
14When a \class{StringIO} object is created, it can be initialized
Guido van Rossumcda3d7d1997-03-03 16:01:21 +000015to an existing string by passing the string to the constructor.
Fred Drake8fe533e1998-03-27 05:27:08 +000016If no string is given, the \class{StringIO} will start empty.
17\end{classdesc}
Guido van Rossumcda3d7d1997-03-03 16:01:21 +000018
Fred Drake7e9383a1998-04-11 18:05:24 +000019The following methods of \class{StringIO} objects require special
20mention:
21
Fred Drake8fe533e1998-03-27 05:27:08 +000022\begin{methoddesc}{getvalue}{}
23Retrieve the entire contents of the ``file'' at any time before the
24\class{StringIO} object's \method{close()} method is called.
25\end{methoddesc}
26
27\begin{methoddesc}{close}{}
28Free the memory buffer.
29\end{methoddesc}
Fred Drake9463de21998-04-11 20:05:43 +000030
31
Fred Drake295da241998-08-10 19:42:37 +000032\section{\module{cStringIO} ---
Fred Drake543e19d1999-04-21 18:15:22 +000033 Faster version of \module{StringIO}}
Fred Drakecd71aa21999-02-18 21:13:03 +000034
Fred Drakeb91e9341998-07-23 17:59:49 +000035\declaremodule{builtin}{cStringIO}
Fred Drake543e19d1999-04-21 18:15:22 +000036\modulesynopsis{Faster version of \module{StringIO}, but not
37 subclassable.}
38\moduleauthor{Jim Fulton}{jfulton@digicool.com}
Fred Drakecd71aa21999-02-18 21:13:03 +000039\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
Fred Drake9463de21998-04-11 20:05:43 +000040
41The module \module{cStringIO} provides an interface similar to that of
Fred Drake543e19d1999-04-21 18:15:22 +000042the \refmodule{StringIO} module. Heavy use of \class{StringIO.StringIO}
Fred Drake9463de21998-04-11 20:05:43 +000043objects can be made more efficient by using the function
44\function{StringIO()} from this module instead.
45
46Since this module provides a factory function which returns objects of
47built-in types, there's no way to build your own version using
Fred Drake543e19d1999-04-21 18:15:22 +000048subclassing. Use the original \refmodule{StringIO} module in that case.
49
50The following data objects are provided as well:
51
52
53\begin{datadesc}{InputType}
54 The type object of the objects created by calling
55 \function{StringIO} with a string parameter.
56\end{datadesc}
57
58\begin{datadesc}{OutputType}
59 The type object of the objects returned by calling
60 \function{StringIO} with no parameters.
61\end{datadesc}
62
63
64There is a C API to the module as well; refer to the module source for
65more information.