blob: 9279a3f2d6c33c400639d74541e5dbd5440680b1 [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 Drake83ff4af2000-11-28 16:24:28 +000010files}). See the description of file objects for operations (section
Fred Drake543e19d1999-04-21 18:15:22 +000011\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.
Fred Drake83ff4af2000-11-28 16:24:28 +000017
18The \class{StringIO} object can accept either Unicode or 8-bit
19strings, but mixing the two may take some care. If both are used,
Fred Drake907e76b2001-07-06 20:30:11 +0000208-bit strings that cannot be interpreted as 7-bit \ASCII{} (that
Fred Drake83ff4af2000-11-28 16:24:28 +000021use the 8th bit) will cause a \exception{UnicodeError} to be raised
22when \method{getvalue()} is called.
Fred Drake8fe533e1998-03-27 05:27:08 +000023\end{classdesc}
Guido van Rossumcda3d7d1997-03-03 16:01:21 +000024
Fred Drake7e9383a1998-04-11 18:05:24 +000025The following methods of \class{StringIO} objects require special
26mention:
27
Fred Drake8fe533e1998-03-27 05:27:08 +000028\begin{methoddesc}{getvalue}{}
29Retrieve the entire contents of the ``file'' at any time before the
Fred Drake83ff4af2000-11-28 16:24:28 +000030\class{StringIO} object's \method{close()} method is called. See the
31note above for information about mixing Unicode and 8-bit strings;
32such mixing can cause this method to raise \exception{UnicodeError}.
Fred Drake8fe533e1998-03-27 05:27:08 +000033\end{methoddesc}
34
35\begin{methoddesc}{close}{}
36Free the memory buffer.
37\end{methoddesc}
Fred Drake9463de21998-04-11 20:05:43 +000038
39
Fred Drake295da241998-08-10 19:42:37 +000040\section{\module{cStringIO} ---
Fred Drake543e19d1999-04-21 18:15:22 +000041 Faster version of \module{StringIO}}
Fred Drakecd71aa21999-02-18 21:13:03 +000042
Fred Drakeb91e9341998-07-23 17:59:49 +000043\declaremodule{builtin}{cStringIO}
Fred Drake543e19d1999-04-21 18:15:22 +000044\modulesynopsis{Faster version of \module{StringIO}, but not
45 subclassable.}
46\moduleauthor{Jim Fulton}{jfulton@digicool.com}
Fred Drakecd71aa21999-02-18 21:13:03 +000047\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
Fred Drake9463de21998-04-11 20:05:43 +000048
49The module \module{cStringIO} provides an interface similar to that of
Fred Drake543e19d1999-04-21 18:15:22 +000050the \refmodule{StringIO} module. Heavy use of \class{StringIO.StringIO}
Fred Drake9463de21998-04-11 20:05:43 +000051objects can be made more efficient by using the function
52\function{StringIO()} from this module instead.
53
54Since this module provides a factory function which returns objects of
55built-in types, there's no way to build your own version using
Fred Drake543e19d1999-04-21 18:15:22 +000056subclassing. Use the original \refmodule{StringIO} module in that case.
57
Fred Drake83ff4af2000-11-28 16:24:28 +000058Unlike the memory files implemented by the \refmodule{StringIO}
59module, those provided by this module are not able to accept Unicode
60strings that cannot be encoded as plain \ASCII{} strings.
61
Fred Drake543e19d1999-04-21 18:15:22 +000062The following data objects are provided as well:
63
64
65\begin{datadesc}{InputType}
66 The type object of the objects created by calling
67 \function{StringIO} with a string parameter.
68\end{datadesc}
69
70\begin{datadesc}{OutputType}
71 The type object of the objects returned by calling
72 \function{StringIO} with no parameters.
73\end{datadesc}
74
75
76There is a C API to the module as well; refer to the module source for
77more information.