Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40006cf

Browse files
committed
Added imghdr,xdrlib; reformatted Makefile somewhat
1 parent 72fba79 commit 40006cf

7 files changed

Lines changed: 614 additions & 31 deletions

File tree

Doc/Makefile

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -108,37 +108,26 @@ ref.ps: ref.dvi
108108

109109
# LaTeX source files for the Python Library Reference
110110
LIBFILES = lib.tex \
111-
libal.tex libaifc.tex liballos.tex \
112-
libamoeba.tex libarray.tex libaudio.tex libaudioop.tex \
113-
libbltin.tex libbinascii.tex \
114-
libcd.tex \
115-
libcgi.tex libcopy.tex libctb.tex libcrypto.tex \
116-
libdbm.tex \
117-
libexcs.tex \
118-
libfcntl.tex libfl.tex libfm.tex libftplib.tex libfuncs.tex \
119-
libgdbm.tex libgetopt.tex libgl.tex libgopherlib.tex libgrp.tex \
120-
libhtmllib.tex libhttplib.tex \
121-
libimageop.tex libimgfile.tex libintro.tex \
122-
libjpeg.tex \
123-
libmac.tex libmacconsole.tex libmacdnr.tex \
124-
libmacfs.tex libmacos.tex libmacostools.tex libmactcp.tex \
125-
libmacspeech.tex libmacui.tex \
126-
libmain.tex libmarshal.tex libmath.tex \
127-
libmd5.tex libmimetools.tex libmisc.tex \
128-
libmm.tex libmpz.tex \
129-
libnntplib.tex \
130-
libobjs.tex libos.tex \
131-
libpanel.tex libparser.tex libpickle.tex libposix.tex libposixfile.tex \
132-
libppath.tex libprofile.tex libpwd.tex libpython.tex \
133-
librand.tex libregex.tex libregsub.tex \
134-
librfc822.tex librgbimg.tex librotor.tex \
135-
libselect.tex libsgi.tex libsgmllib.tex \
136-
libshelve.tex libsocket.tex libsomeos.tex libstdwin.tex \
137-
libstring.tex libstrings.tex libstruct.tex libsun.tex libsys.tex \
138-
libtempfile.tex libtermios.tex libthread.tex libtime.tex \
139-
libtraceback.tex libtypes.tex libtypes2.tex \
140-
libunix.tex liburllib.tex liburlparse.tex \
141-
libwhrandom.tex libwww.tex
111+
libintro.tex libobjs.tex libtypes.tex libexcs.tex libfuncs.tex \
112+
libpython.tex libsys.tex libtypes2.tex libtraceback.tex libpickle.tex \
113+
libshelve.tex libcopy.tex libmarshal.tex libimp.tex libparser.tex \
114+
libbltin.tex libmain.tex libstrings.tex libstring.tex libregex.tex \
115+
libregsub.tex libstruct.tex libmisc.tex libmath.tex librand.tex \
116+
libwhrandom.tex libarray.tex liballos.tex libos.tex libtime.tex \
117+
libgetopt.tex libtempfile.tex liberrno.tex libsomeos.tex libsignal.tex \
118+
libsocket.tex libselect.tex libthread.tex libunix.tex libposix.tex \
119+
libppath.tex libpwd.tex libgrp.tex libcrypt.tex libdbm.tex libgdbm.tex \
120+
libtermios.tex libfcntl.tex libposixfile.tex libsyslog.tex libpdb.tex \
121+
libprofile.tex libwww.tex libcgi.tex liburllib.tex libhttplib.tex \
122+
libftplib.tex libgopherlib.tex libnntplib.tex liburlparse.tex \
123+
libhtmllib.tex libsgmllib.tex librfc822.tex libmimetools.tex \
124+
libbinascii.tex libmm.tex libaudioop.tex libimageop.tex libaifc.tex \
125+
libjpeg.tex librgbimg.tex libcrypto.tex libmd5.tex libmpz.tex \
126+
librotor.tex libmac.tex libctb.tex libmacconsole.tex libmacdnr.tex \
127+
libmacfs.tex libmacos.tex libmacostools.tex libmactcp.tex \
128+
libmacspeech.tex libmacui.tex libstdwin.tex libsgi.tex libal.tex \
129+
libcd.tex libfl.tex libfm.tex libgl.tex libimgfile.tex libsun.tex \
130+
libxdrlib.tex libimghdr.tex
142131

143132
# Library document
144133
lib.dvi: $(LIBFILES)

Doc/lib.tex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,15 @@
142142
\input{librfc822}
143143
\input{libmimetools}
144144
\input{libbinascii}
145+
\input{libxdrlib}
145146

146147
\input{libmm} % Multimedia Services
147148
\input{libaudioop}
148149
\input{libimageop}
149150
\input{libaifc}
150151
\input{libjpeg}
151152
\input{librgbimg}
153+
\input{libimghdr}
152154

153155
\input{libcrypto} % Cryptographic Services
154156
\input{libmd5}

Doc/lib/lib.tex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,15 @@
142142
\input{librfc822}
143143
\input{libmimetools}
144144
\input{libbinascii}
145+
\input{libxdrlib}
145146

146147
\input{libmm} % Multimedia Services
147148
\input{libaudioop}
148149
\input{libimageop}
149150
\input{libaifc}
150151
\input{libjpeg}
151152
\input{librgbimg}
153+
\input{libimghdr}
152154

153155
\input{libcrypto} % Cryptographic Services
154156
\input{libmd5}

Doc/lib/libimghdr.tex

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
\section{Standard module \sectcode{imghdr}}
2+
\stmodindex{imghdr}
3+
4+
The \code{imghdr} module determines the type of image contained in a
5+
file or byte stream.
6+
7+
The \code{imghdr} module defines the following function:
8+
9+
\renewcommand{\indexsubitem}{(in module imghdr)}
10+
11+
\begin{funcdesc}{what}{filename\optional{\, h}}
12+
Tests the image data contained in the file named by \var{filename},
13+
and returns a string describing the image type. If optional \var{h}
14+
is provided, the \var{filename} is ignored and \var{h} is assumed to
15+
contain the byte stream to test.
16+
\end{funcdesc}
17+
18+
The following image types are recognized, as listed below with the
19+
return value from \code{what}:
20+
21+
\begin{enumerate}
22+
\item[``rgb''] SGI ImgLib Files
23+
24+
\item[``gif''] GIF 87a and 89a Files
25+
26+
\item[``pbm''] Portable Bitmap Files
27+
28+
\item[``pgm''] Portable Graymap Files
29+
30+
\item[``ppm''] Portable Pixmap Files
31+
32+
\item[``tiff''] TIFF Files
33+
34+
\item[``rast''] Sun Raster Files
35+
36+
\item[``xbm''] X Bitmap Files
37+
38+
\item[``jpeg''] JPEG data in JIFF format
39+
\end{enumerate}
40+
41+
You can extend the list of file types \code{imghdr} can recognize by
42+
appending to this variable:
43+
44+
\begin{datadesc}{tests}
45+
A list of functions performing the individual tests. Each function
46+
takes two arguments: the byte-stream and an open file-like object.
47+
When \code{what()} is called with a byte-stream, the file-like
48+
object will be \code{None}.
49+
50+
The test function should return a string describing the image type if
51+
the test succeeded, or \code{None} if it failed.
52+
\end{datadesc}
53+
54+
Example:
55+
56+
\begin{verbatim}
57+
>>> import imghdr
58+
>>> imghdr.what('/tmp/bass.gif')
59+
'gif'
60+
\end{verbatim}

Doc/lib/libxdrlib.tex

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
\section{Standard module \sectcode{xdrlib}}
2+
\stmodindex{xdrlib}
3+
\index{XDR}
4+
5+
\renewcommand{\indexsubitem}{(in module xdrlib)}
6+
7+
8+
The \code{xdrlib} module supports the External Data Representation
9+
Standard as described in RFC 1014, written by Sun Microsystems,
10+
Inc. June 1987. It supports most of the data types described in the
11+
RFC, although some, most notably \code{float} and \code{double} are
12+
only supported on those operating systems that provide an XDR
13+
library.
14+
15+
The \code{xdrlib} module defines two classes, one for packing
16+
variables into XDR representation, and another for unpacking from XDR
17+
representation. There are also two exception classes.
18+
19+
20+
\subsection{Packer Objects}
21+
22+
\code{Packer} is the class for packing data into XDR representation.
23+
The \code{Packer} class is instantiated with no arguments.
24+
25+
\begin{funcdesc}{get_buffer}{}
26+
Returns the current pack buffer as a string.
27+
\end{funcdesc}
28+
29+
\begin{funcdesc}{reset}{}
30+
Resets the pack buffer to the empty string.
31+
\end{funcdesc}
32+
33+
In general, you can pack any of the most common XDR data types by
34+
calling the appropriate \code{pack_\var{type}} method. Each method
35+
takes a single argument, the value to pack. The following simple data
36+
type packing methods are supported: \code{pack_uint}, \code{pack_int},
37+
\code{pack_enum}, \code{pack_bool}, \code{pack_uhyper},
38+
and \code{pack_hyper}.
39+
40+
The following methods pack floating point numbers, however they
41+
require C library support. Without the optional C built-in module,
42+
both of these methods will raise an \code{xdrlib.ConversionError}
43+
exception. See the note at the end of this chapter for details.
44+
45+
\begin{funcdesc}{pack_float}{value}
46+
Packs the single-precision floating point number \var{value}.
47+
\end{funcdesc}
48+
49+
\begin{funcdesc}{pack_double}{value}
50+
Packs the double-precision floating point number \var{value}.
51+
\end{funcdesc}
52+
53+
The following methods support packing strings, bytes, and opaque data:
54+
55+
\begin{funcdesc}{pack_fstring}{n\, s}
56+
Packs a fixed length string, \var{s}. \var{n} is the length of the
57+
string but it is \emph{not} packed into the data buffer. The string
58+
is padded with null bytes if necessary to guaranteed 4 byte alignment.
59+
\end{funcdesc}
60+
61+
\begin{funcdesc}{pack_fopaque}{n\, data}
62+
Packs a fixed length opaque data stream, similarly to
63+
\code{pack_fstring}.
64+
\end{funcdesc}
65+
66+
\begin{funcdesc}{pack_string}{s}
67+
Packs a variable length string, \var{s}. The length of the string is
68+
first packed as an unsigned integer, then the string data is packed
69+
with \code{pack_fstring}.
70+
\end{funcdesc}
71+
72+
\begin{funcdesc}{pack_opaque}{data}
73+
Packs a variable length opaque data string, similarly to
74+
\code{pack_string}.
75+
\end{funcdesc}
76+
77+
\begin{funcdesc}{pack_bytes}{bytes}
78+
Packs a variable length byte stream, similarly to \code{pack_string}.
79+
\end{funcdesc}
80+
81+
The following methods support packing arrays and lists:
82+
83+
\begin{funcdesc}{pack_list}{list\, pack_item}
84+
Packs a \var{list} of homogeneous items. This method is useful for
85+
lists with an indeterminate size; i.e. the size is not available until
86+
the entire list has been walked. For each item in the list, an
87+
unsigned integer \code{1} is packed first, followed by the data value
88+
from the list. \var{pack_item} is the function that is called to pack
89+
the individual item. At the end of the list, an unsigned integer
90+
\code{0} is packed.
91+
\end{funcdesc}
92+
93+
\begin{funcdesc}{pack_farray}{n\, array\, pack_item}
94+
Packs a fixed length list (\var{array}) of homogeneous items. \var{n}
95+
is the length of the list; it is \emph{not} packed into the buffer,
96+
but a \code{ValueError} exception is raised if \code{len(array)} is not
97+
equal to \var{n}. As above, \var{pack_item} is the function used to
98+
pack each element.
99+
\end{funcdesc}
100+
101+
\begin{funcdesc}{pack_array}{list\, pack_item}
102+
Packs a variable length \var{list} of homogeneous items. First, the
103+
length of the list is packed as an unsigned integer, then each element
104+
is packed as in \code{pack_farray} above.
105+
\end{funcdesc}
106+
107+
\subsection{Unpacker Objects}
108+
109+
\code{Unpacker} is the complementary class which unpacks XDR data
110+
values from a string buffer, and has the following methods:
111+
112+
\begin{funcdesc}{__init__}{data}
113+
Instantiates an \code{Unpacker} object with the string buffer
114+
\var{data}.
115+
\end{funcdesc}
116+
117+
\begin{funcdesc}{reset}{data}
118+
Resets the string buffer with the given \var{data}.
119+
\end{funcdesc}
120+
121+
\begin{funcdesc}{get_position}{}
122+
Returns the current unpack position in the data buffer.
123+
\end{funcdesc}
124+
125+
\begin{funcdesc}{set_position}{position}
126+
Sets the data buffer unpack position to \var{position}. You should be
127+
careful about using \code{get_position()} and \code{set_position()}.
128+
\end{funcdesc}
129+
130+
\begin{funcdesc}{done}{}
131+
Indicates unpack completion. Raises an \code{xdrlib.Error} exception
132+
if all of the data has not been unpacked.
133+
\end{funcdesc}
134+
135+
In addition, every data type that can be packed with a \code{Packer},
136+
can be unpacked with an \code{Unpacker}. Unpacking methods are of the
137+
form \code{unpack_\var{type}}, and take no arguments. They return the
138+
unpacked object. The same caveats apply for \code{unpack_float} and
139+
\code{unpack_double} as above.
140+
141+
In addition, the following methods unpack strings, bytes, and opaque
142+
data:
143+
144+
\begin{funcdesc}{unpack_fstring}{n}
145+
Unpacks and returns a fixed length string. \var{n} is the number of
146+
characters expected. Padding with null bytes to guaranteed 4 byte
147+
alignment is assumed.
148+
\end{funcdesc}
149+
150+
\begin{funcdesc}{unpack_fopaque}{n}
151+
Unpacks and returns a fixed length opaque data stream, similarly to
152+
\code{unpack_fstring}.
153+
\end{funcdesc}
154+
155+
\begin{funcdesc}{pack_string}{}
156+
Unpacks and returns a variable length string. The length of the
157+
string is first unpacked as an unsigned integer, then the string data
158+
is unpacked with \code{unpack_fstring}.
159+
\end{funcdesc}
160+
161+
\begin{funcdesc}{unpack_opaque}{}
162+
Unpacks and returns a variable length opaque data string, similarly to
163+
\code{pack_string}.
164+
\end{funcdesc}
165+
166+
\begin{funcdesc}{unpack_bytes}{}
167+
Unpacks and returns a variable length byte stream, similarly to
168+
\code{pack_string}.
169+
\end{funcdesc}
170+
171+
The following methods support unpacking arrays and lists:
172+
173+
\begin{funcdesc}{unpack_list}{unpack_item}
174+
Unpacks and returns a list of homogeneous items. The list is unpacked
175+
one element at a time
176+
by first unpacking an unsigned integer flag. If the flag is \code{1},
177+
then the item is unpacked and appended to the list. A flag of
178+
\code{0} indicates the end of the list. \var{unpack_item} is the
179+
function that is called to unpack the items.
180+
\end{funcdesc}
181+
182+
\begin{funcdesc}{unpack_farray}{n\, unpack_item}
183+
Unpacks and returns (as a list) a fixed length array of homogeneous
184+
items. \var{n} is number of list elements to expect in the buffer.
185+
As above, \var{unpack_item} is the function used to unpack each element.
186+
\end{funcdesc}
187+
188+
\begin{funcdesc}{unpack_array}{unpack_item}
189+
Unpacks and returns a variable length \var{list} of homogeneous items.
190+
First, the length of the list is unpacked as an unsigned integer, then
191+
each element is unpacked as in \code{unpack_farray} above.
192+
\end{funcdesc}
193+
194+
\subsection{Exceptions}
195+
196+
Exceptions in this module are coded as class instances:
197+
198+
\begin{excdesc}{Error}
199+
The base exception class. \code{Error} has a single public data
200+
member \code{msg} containing the description of the error.
201+
\end{excdesc}
202+
203+
\begin{excdesc}{ConversionError}
204+
Class derived from \code{Error}. Contains no additional instance
205+
variables.
206+
\end{excdesc}
207+
208+
Here is an example of how you would catch one of these exceptions:
209+
210+
\begin{verbatim}
211+
import xdrlib
212+
p = xdrlib.Packer()
213+
try:
214+
p.pack_double(8.01)
215+
except xdrlib.ConversionError, instance:
216+
print 'packing the double failed:', instance.msg
217+
\end{verbatim}
218+
219+
\subsection{Supporting Floating Point Data}
220+
221+
Packing and unpacking floating point data,
222+
i.e. \code{Packer.pack_float}, \code{Packer.pack_double},
223+
\code{Unpacker.unpack_float}, and \code{Unpacker.unpack_double}, are
224+
only supported with the helper built-in \code{_xdr} module, which
225+
relies on your operating system having the appropriate XDR library
226+
routines.
227+
228+
If you have built the Python interpeter with the \code{_xdr} module,
229+
or have built the \code{_xdr} module as a shared library,
230+
\code{xdrlib} will use these to pack and unpack floating point
231+
numbers. Otherwise, using these routines will raise a
232+
\code{ConversionError} exception.
233+
234+
See the Python installation instructions for details on building the
235+
\code{_xdr} module.

0 commit comments

Comments
 (0)