blob: 3992e435e2c6934eab02b8210e613d25fc1efaad [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.
Raymond Hettingerfc113832005-04-11 01:03:44 +000017In both cases, the initial file position starts at zero.
Fred Drake83ff4af2000-11-28 16:24:28 +000018
19The \class{StringIO} object can accept either Unicode or 8-bit
20strings, but mixing the two may take some care. If both are used,
Fred Drake907e76b2001-07-06 20:30:11 +0000218-bit strings that cannot be interpreted as 7-bit \ASCII{} (that
Fred Drake83ff4af2000-11-28 16:24:28 +000022use the 8th bit) will cause a \exception{UnicodeError} to be raised
23when \method{getvalue()} is called.
Fred Drake8fe533e1998-03-27 05:27:08 +000024\end{classdesc}
Guido van Rossumcda3d7d1997-03-03 16:01:21 +000025
Fred Drake7e9383a1998-04-11 18:05:24 +000026The following methods of \class{StringIO} objects require special
27mention:
28
Fred Drake8fe533e1998-03-27 05:27:08 +000029\begin{methoddesc}{getvalue}{}
30Retrieve the entire contents of the ``file'' at any time before the
Fred Drake83ff4af2000-11-28 16:24:28 +000031\class{StringIO} object's \method{close()} method is called. See the
32note above for information about mixing Unicode and 8-bit strings;
33such mixing can cause this method to raise \exception{UnicodeError}.
Fred Drake8fe533e1998-03-27 05:27:08 +000034\end{methoddesc}
35
36\begin{methoddesc}{close}{}
37Free the memory buffer.
38\end{methoddesc}
Fred Drake9463de21998-04-11 20:05:43 +000039
40
Fred Drake295da241998-08-10 19:42:37 +000041\section{\module{cStringIO} ---
Fred Drake543e19d1999-04-21 18:15:22 +000042 Faster version of \module{StringIO}}
Fred Drakecd71aa21999-02-18 21:13:03 +000043
Fred Drakeb91e9341998-07-23 17:59:49 +000044\declaremodule{builtin}{cStringIO}
Fred Drake543e19d1999-04-21 18:15:22 +000045\modulesynopsis{Faster version of \module{StringIO}, but not
46 subclassable.}
Andrew M. Kuchlingc62af022004-01-08 15:01:08 +000047\moduleauthor{Jim Fulton}{jim@zope.com}
Fred Drakecd71aa21999-02-18 21:13:03 +000048\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
Fred Drake9463de21998-04-11 20:05:43 +000049
50The module \module{cStringIO} provides an interface similar to that of
Fred Drake543e19d1999-04-21 18:15:22 +000051the \refmodule{StringIO} module. Heavy use of \class{StringIO.StringIO}
Fred Drake9463de21998-04-11 20:05:43 +000052objects can be made more efficient by using the function
53\function{StringIO()} from this module instead.
54
55Since this module provides a factory function which returns objects of
56built-in types, there's no way to build your own version using
Fred Drake543e19d1999-04-21 18:15:22 +000057subclassing. Use the original \refmodule{StringIO} module in that case.
58
Fred Drake83ff4af2000-11-28 16:24:28 +000059Unlike the memory files implemented by the \refmodule{StringIO}
60module, those provided by this module are not able to accept Unicode
61strings that cannot be encoded as plain \ASCII{} strings.
62
Raymond Hettinger2adbb832003-01-31 05:17:33 +000063Another difference from the \refmodule{StringIO} module is that calling
64\function{StringIO()} with a string parameter creates a read-only object.
65Unlike an object created without a string parameter, it does not have
Skip Montanarobc961e52003-08-11 15:06:07 +000066write methods. These objects are not generally visible. They turn up in
67tracebacks as \class{StringI} and \class{StringO}.
Raymond Hettinger2adbb832003-01-31 05:17:33 +000068
Fred Drake543e19d1999-04-21 18:15:22 +000069The following data objects are provided as well:
70
71
72\begin{datadesc}{InputType}
73 The type object of the objects created by calling
74 \function{StringIO} with a string parameter.
75\end{datadesc}
76
77\begin{datadesc}{OutputType}
78 The type object of the objects returned by calling
79 \function{StringIO} with no parameters.
80\end{datadesc}
81
82
83There is a C API to the module as well; refer to the module source for
84more information.