Commit e7b146fb authored by Guido van Rossum's avatar Guido van Rossum

The third and final doc-string sweep by Ka-Ping Yee.

The attached patches update the standard library so that all modules
have docstrings beginning with one-line summaries.

A new docstring was added to formatter.  The docstring for os.py
was updated to mention nt, os2, ce in addition to posix, dos, mac.
parent 54f22ed3
#! /usr/bin/env python #! /usr/bin/env python
# Conversions to/from quoted-printable transport encoding as per RFC-1521 """Conversions to/from quoted-printable transport encoding as per RFC-1521."""
# (Dec 1991 version). # (Dec 1991 version).
ESCAPE = '=' ESCAPE = '='
...@@ -8,11 +9,15 @@ MAXLINESIZE = 76 ...@@ -8,11 +9,15 @@ MAXLINESIZE = 76
HEX = '0123456789ABCDEF' HEX = '0123456789ABCDEF'
def needsquoting(c, quotetabs): def needsquoting(c, quotetabs):
"""Decide whether a particular character needs to be quoted.
The 'quotetabs' flag indicates whether tabs should be quoted."""
if c == '\t': if c == '\t':
return not quotetabs return not quotetabs
return c == ESCAPE or not(' ' <= c <= '~') return c == ESCAPE or not(' ' <= c <= '~')
def quote(c): def quote(c):
"""Quote a single character."""
if c == ESCAPE: if c == ESCAPE:
return ESCAPE * 2 return ESCAPE * 2
else: else:
...@@ -20,6 +25,10 @@ def quote(c): ...@@ -20,6 +25,10 @@ def quote(c):
return ESCAPE + HEX[i/16] + HEX[i%16] return ESCAPE + HEX[i/16] + HEX[i%16]
def encode(input, output, quotetabs): def encode(input, output, quotetabs):
"""Read 'input', apply quoted-printable encoding, and write to 'output'.
'input' and 'output' are files with readline() and write() methods.
The 'quotetabs' flag indicates whether tabs should be quoted."""
while 1: while 1:
line = input.readline() line = input.readline()
if not line: break if not line: break
...@@ -42,6 +51,9 @@ def encode(input, output, quotetabs): ...@@ -42,6 +51,9 @@ def encode(input, output, quotetabs):
output.write(new + '\n') output.write(new + '\n')
def decode(input, output): def decode(input, output):
"""Read 'input', apply quoted-printable decoding, and write to 'output'.
'input' and 'output' are files with readline() and write() methods."""
new = '' new = ''
while 1: while 1:
line = input.readline() line = input.readline()
...@@ -73,9 +85,11 @@ def decode(input, output): ...@@ -73,9 +85,11 @@ def decode(input, output):
output.write(new) output.write(new)
def ishex(c): def ishex(c):
"""Return true if the character 'c' is a hexadecimal digit."""
return '0' <= c <= '9' or 'a' <= c <= 'f' or 'A' <= c <= 'F' return '0' <= c <= '9' or 'a' <= c <= 'f' or 'A' <= c <= 'F'
def unhex(s): def unhex(s):
"""Get the integer value of a hexadecimal number."""
bits = 0 bits = 0
for c in s: for c in s:
if '0' <= c <= '9': if '0' <= c <= '9':
......
# R A N D O M V A R I A B L E G E N E R A T O R S """Random variable generators.
#
# distributions on the real line: distributions on the real line:
# ------------------------------ ------------------------------
# normal (Gaussian) normal (Gaussian)
# lognormal lognormal
# negative exponential negative exponential
# gamma gamma
# beta beta
#
# distributions on the circle (angles 0 to 2pi) distributions on the circle (angles 0 to 2pi)
# --------------------------------------------- ---------------------------------------------
# circular uniform circular uniform
# von Mises von Mises
# Translated from anonymously contributed C/C++ source. Translated from anonymously contributed C/C++ source.
# Multi-threading note: the random number generator used here is not Multi-threading note: the random number generator used here is not
# thread-safe; it is possible that two calls return the same random thread-safe; it is possible that two calls return the same random
# value. See whrandom.py for more info. value. See whrandom.py for more info.
"""
import whrandom import whrandom
from whrandom import random, uniform, randint, choice, randrange # For export! from whrandom import random, uniform, randint, choice, randrange # For export!
......
# These bits are passed to regex.set_syntax() to choose among """Constants for selecting regexp syntaxes for the obsolete regex module.
# alternative regexp syntaxes.
This module is only for backward compatibility. "regex" has now
been replaced by the new regular expression module, "re".
These bits are passed to regex.set_syntax() to choose among
alternative regexp syntaxes.
"""
# 1 means plain parentheses serve as grouping, and backslash # 1 means plain parentheses serve as grouping, and backslash
# parentheses are needed for literal searching. # parentheses are needed for literal searching.
......
# Regular expression subroutines: """Regexp-based split and replace using the obsolete regex module.
# sub(pat, repl, str): replace first occurrence of pattern in string
# gsub(pat, repl, str): replace all occurrences of pattern in string
# split(str, pat, maxsplit): split string using pattern as delimiter
# splitx(str, pat, maxsplit): split string using pattern as delimiter plus
# return delimiters
This module is only for backward compatibility. These operations
are now provided by the new regular expression module, "re".
sub(pat, repl, str): replace first occurrence of pattern in string
gsub(pat, repl, str): replace all occurrences of pattern in string
split(str, pat, maxsplit): split string using pattern as delimiter
splitx(str, pat, maxsplit): split string using pattern as delimiter plus
return delimiters
"""
import regex import regex
......
# Redo the `...` (representation) but with limits on most sizes. """Redo the `...` (representation) but with limits on most sizes."""
import string import string
......
# A parser for SGML, using the derived class as static DTD. """A parser for SGML, using the derived class as a static DTD."""
# XXX This only supports those SGML features used by HTML. # XXX This only supports those SGML features used by HTML.
......
"""A lexical analyzer class for simple shell-like syntaxes."""
# Module and documentation by Eric S. Raymond, 21 Dec 1998 # Module and documentation by Eric S. Raymond, 21 Dec 1998
import sys import sys
......
"""Utility functions for copying files. """Utility functions for copying files and directory trees.
XXX The functions here don't copy the resource fork or other metadata on Mac. XXX The functions here don't copy the resource fork or other metadata on Mac.
......
# Module 'stat' """Constants/functions for interpreting results of os.stat() and os.lstat().
#
# Defines constants and functions for interpreting stat/lstat struct Suggested usage: from stat import *
# as returned by os.stat() and os.lstat() (if it exists). """
#
# Suggested usage: from stat import *
#
# XXX Strictly spoken, this module may have to be adapted for each POSIX # XXX Strictly spoken, this module may have to be adapted for each POSIX
# implementation; in practice, however, the numeric constants used by # implementation; in practice, however, the numeric constants used by
# stat() are almost universal (even for stat() emulations on non-UNIX # stat() are almost universal (even for stat() emulations on non-UNIX
......
# Module 'statcache' """Maintain a cache of file stats.
# There are functions to reset the cache or to selectively remove items.
# Maintain a cache of file stats. """
# There are functions to reset the cache or to selectively remove items.
import os import os
from stat import * from stat import *
...@@ -12,42 +11,37 @@ from stat import * ...@@ -12,42 +11,37 @@ from stat import *
cache = {} cache = {}
# Stat a file, possibly out of the cache.
#
def stat(path): def stat(path):
"""Stat a file, possibly out of the cache."""
if cache.has_key(path): if cache.has_key(path):
return cache[path] return cache[path]
cache[path] = ret = os.stat(path) cache[path] = ret = os.stat(path)
return ret return ret
# Reset the cache completely.
#
def reset(): def reset():
"""Reset the cache completely."""
global cache global cache
cache = {} cache = {}
# Remove a given item from the cache, if it exists.
#
def forget(path): def forget(path):
"""Remove a given item from the cache, if it exists."""
if cache.has_key(path): if cache.has_key(path):
del cache[path] del cache[path]
# Remove all pathnames with a given prefix.
#
def forget_prefix(prefix): def forget_prefix(prefix):
"""Remove all pathnames with a given prefix."""
n = len(prefix) n = len(prefix)
for path in cache.keys(): for path in cache.keys():
if path[:n] == prefix: if path[:n] == prefix:
del cache[path] del cache[path]
# Forget about a directory and all entries in it, but not about
# entries in subdirectories.
#
def forget_dir(prefix): def forget_dir(prefix):
"""Forget about a directory and all entries in it, but not about
entries in subdirectories."""
if prefix[-1:] == '/' and prefix <> '/': if prefix[-1:] == '/' and prefix <> '/':
prefix = prefix[:-1] prefix = prefix[:-1]
forget(prefix) forget(prefix)
...@@ -62,19 +56,17 @@ def forget_dir(prefix): ...@@ -62,19 +56,17 @@ def forget_dir(prefix):
del cache[path] del cache[path]
# Remove all pathnames except with a given prefix.
# Normally used with prefix = '/' after a chdir().
#
def forget_except_prefix(prefix): def forget_except_prefix(prefix):
"""Remove all pathnames except with a given prefix.
Normally used with prefix = '/' after a chdir()."""
n = len(prefix) n = len(prefix)
for path in cache.keys(): for path in cache.keys():
if path[:n] <> prefix: if path[:n] <> prefix:
del cache[path] del cache[path]
# Check for directory.
#
def isdir(path): def isdir(path):
"""Check for directory."""
try: try:
st = stat(path) st = stat(path)
except os.error: except os.error:
......
# Module 'statvfs' """Constants for interpreting the results of os.statvfs() and os.fstatvfs()."""
#
# Defines constants for interpreting statvfs struct as returned
# by os.statvfs() and os.fstatvfs() (if they exist).
#
# Indices for statvfs struct members in the tuple returned by # Indices for statvfs struct members in the tuple returned by
# os.statvfs() and os.fstatvfs(). # os.statvfs() and os.fstatvfs().
......
# module 'string' -- A collection of string operations """A collection of string operations (most are no longer used in Python 1.6).
# Warning: most of the code you see here isn't normally used nowadays. With Warning: most of the code you see here isn't normally used nowadays. With
# Python 1.6, many of these functions are implemented as methods on the Python 1.6, many of these functions are implemented as methods on the
# standard string object. They used to be implemented by a built-in module standard string object. They used to be implemented by a built-in module
# called strop, but strop is now obsolete itself. called strop, but strop is now obsolete itself.
"""Common string manipulations.
Public module variables: Public module variables:
......
# Stuff to parse Sun and NeXT audio files. """Stuff to parse Sun and NeXT audio files.
#
# An audio consists of a header followed by the data. The structure An audio consists of a header followed by the data. The structure
# of the header is as follows. of the header is as follows.
#
# +---------------+ +---------------+
# | magic word | | magic word |
# +---------------+ +---------------+
# | header size | | header size |
# +---------------+ +---------------+
# | data size | | data size |
# +---------------+ +---------------+
# | encoding | | encoding |
# +---------------+ +---------------+
# | sample rate | | sample rate |
# +---------------+ +---------------+
# | # of channels | | # of channels |
# +---------------+ +---------------+
# | info | | info |
# | | | |
# +---------------+ +---------------+
#
# The magic word consists of the 4 characters '.snd'. Apart from the The magic word consists of the 4 characters '.snd'. Apart from the
# info field, all header fields are 4 bytes in size. They are all info field, all header fields are 4 bytes in size. They are all
# 32-bit unsigned integers encoded in big-endian byte order. 32-bit unsigned integers encoded in big-endian byte order.
#
# The header size really gives the start of the data. The header size really gives the start of the data.
# The data size is the physical size of the data. From the other The data size is the physical size of the data. From the other
# parameter the number of frames can be calculated. parameter the number of frames can be calculated.
# The encoding gives the way in which audio samples are encoded. The encoding gives the way in which audio samples are encoded.
# Possible values are listed below. Possible values are listed below.
# The info field currently consists of an ASCII string giving a The info field currently consists of an ASCII string giving a
# human-readable description of the audio file. The info field is human-readable description of the audio file. The info field is
# padded with NUL bytes to the header size. padded with NUL bytes to the header size.
#
# Usage. Usage.
#
# Reading audio files: Reading audio files:
# f = sunau.open(file, 'r') f = sunau.open(file, 'r')
# where file is either the name of a file or an open file pointer. where file is either the name of a file or an open file pointer.
# The open file pointer must have methods read(), seek(), and close(). The open file pointer must have methods read(), seek(), and close().
# When the setpos() and rewind() methods are not used, the seek() When the setpos() and rewind() methods are not used, the seek()
# method is not necessary. method is not necessary.
#
# This returns an instance of a class with the following public methods: This returns an instance of a class with the following public methods:
# getnchannels() -- returns number of audio channels (1 for getnchannels() -- returns number of audio channels (1 for
# mono, 2 for stereo) mono, 2 for stereo)
# getsampwidth() -- returns sample width in bytes getsampwidth() -- returns sample width in bytes
# getframerate() -- returns sampling frequency getframerate() -- returns sampling frequency
# getnframes() -- returns number of audio frames getnframes() -- returns number of audio frames
# getcomptype() -- returns compression type ('NONE' or 'ULAW') getcomptype() -- returns compression type ('NONE' or 'ULAW')
# getcompname() -- returns human-readable version of getcompname() -- returns human-readable version of
# compression type ('not compressed' matches 'NONE') compression type ('not compressed' matches 'NONE')
# getparams() -- returns a tuple consisting of all of the getparams() -- returns a tuple consisting of all of the
# above in the above order above in the above order
# getmarkers() -- returns None (for compatibility with the getmarkers() -- returns None (for compatibility with the
# aifc module) aifc module)
# getmark(id) -- raises an error since the mark does not getmark(id) -- raises an error since the mark does not
# exist (for compatibility with the aifc module) exist (for compatibility with the aifc module)
# readframes(n) -- returns at most n frames of audio readframes(n) -- returns at most n frames of audio
# rewind() -- rewind to the beginning of the audio stream rewind() -- rewind to the beginning of the audio stream
# setpos(pos) -- seek to the specified position setpos(pos) -- seek to the specified position
# tell() -- return the current position tell() -- return the current position
# close() -- close the instance (make it unusable) close() -- close the instance (make it unusable)
# The position returned by tell() and the position given to setpos() The position returned by tell() and the position given to setpos()
# are compatible and have nothing to do with the actual postion in the are compatible and have nothing to do with the actual postion in the
# file. file.
# The close() method is called automatically when the class instance The close() method is called automatically when the class instance
# is destroyed. is destroyed.
#
# Writing audio files: Writing audio files:
# f = sunau.open(file, 'w') f = sunau.open(file, 'w')
# where file is either the name of a file or an open file pointer. where file is either the name of a file or an open file pointer.
# The open file pointer must have methods write(), tell(), seek(), and The open file pointer must have methods write(), tell(), seek(), and
# close(). close().
#
# This returns an instance of a class with the following public methods: This returns an instance of a class with the following public methods:
# setnchannels(n) -- set the number of channels setnchannels(n) -- set the number of channels
# setsampwidth(n) -- set the sample width setsampwidth(n) -- set the sample width
# setframerate(n) -- set the frame rate setframerate(n) -- set the frame rate
# setnframes(n) -- set the number of frames setnframes(n) -- set the number of frames
# setcomptype(type, name) setcomptype(type, name)
# -- set the compression type and the -- set the compression type and the
# human-readable compression type human-readable compression type
# setparams(tuple)-- set all parameters at once setparams(tuple)-- set all parameters at once
# tell() -- return current position in output file tell() -- return current position in output file
# writeframesraw(data) writeframesraw(data)
# -- write audio frames without pathing up the -- write audio frames without pathing up the
# file header file header
# writeframes(data) writeframes(data)
# -- write audio frames and patch up the file header -- write audio frames and patch up the file header
# close() -- patch up the file header and close the close() -- patch up the file header and close the
# output file output file
# You should set the parameters before the first writeframesraw or You should set the parameters before the first writeframesraw or
# writeframes. The total number of frames does not need to be set, writeframes. The total number of frames does not need to be set,
# but when it is set to the correct value, the header does not have to but when it is set to the correct value, the header does not have to
# be patched up. be patched up.
# It is best to first set all parameters, perhaps possibly the It is best to first set all parameters, perhaps possibly the
# compression type, and then write audio frames using writeframesraw. compression type, and then write audio frames using writeframesraw.
# When all frames have been written, either call writeframes('') or When all frames have been written, either call writeframes('') or
# close() to patch up the sizes in the header. close() to patch up the sizes in the header.
# The close() method is called automatically when the class instance The close() method is called automatically when the class instance
# is destroyed. is destroyed.
"""
# from <multimedia/audio_filehdr.h> # from <multimedia/audio_filehdr.h>
AUDIO_FILE_MAGIC = 0x2e736e64 AUDIO_FILE_MAGIC = 0x2e736e64
......
# Module 'sunaudio' -- interpret sun audio headers """Interpret sun audio headers."""
MAGIC = '.snd' MAGIC = '.snd'
error = 'sunaudio sound header conversion error' error = 'sunaudio sound header conversion error'
# convert a 4-char value to integer
def get_long_be(s): def get_long_be(s):
"""Convert a 4-char value to integer."""
return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3]) return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
# read a sound header from an open file
def gethdr(fp): def gethdr(fp):
"""Read a sound header from an open file."""
if fp.read(4) <> MAGIC: if fp.read(4) <> MAGIC:
raise error, 'gethdr: bad magic word' raise error, 'gethdr: bad magic word'
hdr_size = get_long_be(fp.read(4)) hdr_size = get_long_be(fp.read(4))
...@@ -31,9 +29,8 @@ def gethdr(fp): ...@@ -31,9 +29,8 @@ def gethdr(fp):
return (data_size, encoding, sample_rate, channels, info) return (data_size, encoding, sample_rate, channels, info)
# read and print the sound header of a named file
def printhdr(file): def printhdr(file):
"""Read and print the sound header of a named file."""
hdr = gethdr(open(file, 'r')) hdr = gethdr(open(file, 'r'))
data_size, encoding, sample_rate, channels, info = hdr data_size, encoding, sample_rate, channels, info = hdr
while info[-1:] == '\0': while info[-1:] == '\0':
......
#! /usr/bin/env python #! /usr/bin/env python
#
# Non-terminal symbols of Python grammar (from "graminit.h") """Non-terminal symbols of Python grammar (from "graminit.h")."""
#
# This file is automatically generated; please don't muck it up! # This file is automatically generated; please don't muck it up!
# #
# To update the symbols in this file, 'cd' to the top directory of # To update the symbols in this file, 'cd' to the top directory of
......
# Temporary file name allocation """Temporary files and filenames."""
#
# XXX This tries to be not UNIX specific, but I don't know beans about # XXX This tries to be not UNIX specific, but I don't know beans about
# how to choose a temp directory or filename on MS-DOS or other # how to choose a temp directory or filename on MS-DOS or other
# systems so it may have to be changed... # systems so it may have to be changed...
...@@ -14,9 +14,8 @@ tempdir = None ...@@ -14,9 +14,8 @@ tempdir = None
template = None template = None
# Function to calculate the directory to use
def gettempdir(): def gettempdir():
"""Function to calculate the directory to use."""
global tempdir global tempdir
if tempdir is not None: if tempdir is not None:
return tempdir return tempdir
...@@ -58,11 +57,10 @@ def gettempdir(): ...@@ -58,11 +57,10 @@ def gettempdir():
return tempdir return tempdir
# Function to calculate a prefix of the filename to use
_pid = None _pid = None
def gettempprefix(): def gettempprefix():
"""Function to calculate a prefix of the filename to use."""
global template, _pid global template, _pid
if os.name == 'posix' and _pid and _pid != os.getpid(): if os.name == 'posix' and _pid and _pid != os.getpid():
# Our pid changed; we must have forked -- zap the template # Our pid changed; we must have forked -- zap the template
...@@ -85,9 +83,8 @@ def gettempprefix(): ...@@ -85,9 +83,8 @@ def gettempprefix():
counter = 0 counter = 0
# User-callable function to return a unique temporary file name
def mktemp(suffix=""): def mktemp(suffix=""):
"""User-callable function to return a unique temporary file name."""
global counter global counter
dir = gettempdir() dir = gettempdir()
pre = gettempprefix() pre = gettempprefix()
...@@ -126,6 +123,7 @@ class TemporaryFileWrapper: ...@@ -126,6 +123,7 @@ class TemporaryFileWrapper:
def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
"""Create and return a temporary file (opened read-write by default)."""
name = mktemp(suffix) name = mktemp(suffix)
if os.name == 'posix': if os.name == 'posix':
# Unix -- be very careful # Unix -- be very careful
......
# threading.py: """Proposed new threading module, emulating a subset of Java's threading model."""
# Proposed new threading module, emulating a subset of Java's threading model
import sys import sys
import time import time
......
# Convert "arbitrary" sound files to AIFF files (Apple and SGI's audio format). """Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
# Input may be compressed.
# Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others. Input may be compressed.
# An exception is raised if the file is not of a recognized type. Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
# Returned filename is either the input filename or a temporary filename; An exception is raised if the file is not of a recognized type.
# in the latter case the caller must ensure that it is removed. Returned filename is either the input filename or a temporary filename;
# Other temporary files used are removed by the function. in the latter case the caller must ensure that it is removed.
Other temporary files used are removed by the function.
"""
import os import os
import tempfile import tempfile
......
#! /usr/bin/env python #! /usr/bin/env python
#
# Tokens (from "token.h") """Token constants (from "token.h")."""
#
# This file is automatically generated; please don't muck it up! # This file is automatically generated; please don't muck it up!
# #
# To update the symbols in this file, 'cd' to the top directory of # To update the symbols in this file, 'cd' to the top directory of
......
# Format and print Python stack traces """Extract, format and print information about Python stack traces."""
import linecache import linecache
import string import string
...@@ -10,6 +10,8 @@ def _print(file, str='', terminator='\n'): ...@@ -10,6 +10,8 @@ def _print(file, str='', terminator='\n'):
def print_list(extracted_list, file=None): def print_list(extracted_list, file=None):
"""Print the list of tuples as returned by extract_tb() or
extract_stack() as a formatted stack trace to the given file."""
if not file: if not file:
file = sys.stderr file = sys.stderr
for filename, lineno, name, line in extracted_list: for filename, lineno, name, line in extracted_list:
...@@ -19,6 +21,12 @@ def print_list(extracted_list, file=None): ...@@ -19,6 +21,12 @@ def print_list(extracted_list, file=None):
_print(file, ' %s' % string.strip(line)) _print(file, ' %s' % string.strip(line))
def format_list(extracted_list): def format_list(extracted_list):
"""Given a list of tuples as returned by extract_tb() or
extract_stack(), return a list of strings ready for printing.
Each string in the resulting list corresponds to the item with
the same index in the argument list. Each string ends in a
newline; the strings may contain internal newlines as well, for
those items whose source text line is not None."""
list = [] list = []
for filename, lineno, name, line in extracted_list: for filename, lineno, name, line in extracted_list:
item = ' File "%s", line %d, in %s\n' % (filename,lineno,name) item = ' File "%s", line %d, in %s\n' % (filename,lineno,name)
...@@ -29,6 +37,10 @@ def format_list(extracted_list): ...@@ -29,6 +37,10 @@ def format_list(extracted_list):
def print_tb(tb, limit=None, file=None): def print_tb(tb, limit=None, file=None):
"""Print up to 'limit' stack trace entries from the traceback 'tb'.
If 'limit' is omitted or None, all entries are printed. If 'file' is
omitted or None, the output goes to sys.stderr; otherwise 'file'
should be an open file or file-like object with a write() method."""
if not file: if not file:
file = sys.stderr file = sys.stderr
if limit is None: if limit is None:
...@@ -49,9 +61,18 @@ def print_tb(tb, limit=None, file=None): ...@@ -49,9 +61,18 @@ def print_tb(tb, limit=None, file=None):
n = n+1 n = n+1
def format_tb(tb, limit = None): def format_tb(tb, limit = None):
"""A shorthand for 'format_list(extract_stack(f, limit))."""
return format_list(extract_tb(tb, limit)) return format_list(extract_tb(tb, limit))
def extract_tb(tb, limit = None): def extract_tb(tb, limit = None):
"""Return a list of up to 'limit' pre-processed stack trace entries
extracted from the traceback object 'traceback'. This is useful for
alternate formatting of stack traces. If 'limit' is omitted or None,
all entries are extracted. A pre-processed stack trace entry is a
quadruple (filename, line number, function name, text) representing
the information that is usually printed for a stack trace. The text
is a string with leading and trailing whitespace stripped; if the
source is not available it is None."""
if limit is None: if limit is None:
if hasattr(sys, 'tracebacklimit'): if hasattr(sys, 'tracebacklimit'):
limit = sys.tracebacklimit limit = sys.tracebacklimit
...@@ -73,6 +94,14 @@ def extract_tb(tb, limit = None): ...@@ -73,6 +94,14 @@ def extract_tb(tb, limit = None):
def print_exception(etype, value, tb, limit=None, file=None): def print_exception(etype, value, tb, limit=None, file=None):
"""Print exception information and up to 'limit' stack trace entries
from the traceback 'tb' to 'file'. This differs from print_tb() in
the following ways: (1) if traceback is not None, it prints a header
"Traceback (innermost last):"; (2) it prints the exception type and
value after the stack trace; (3) if type is SyntaxError and value has
the appropriate format, it prints the line where the syntax error
occurred with a caret on the next line indicating the approximate
position of the error."""
if not file: if not file:
file = sys.stderr file = sys.stderr
if tb: if tb:
...@@ -84,6 +113,12 @@ def print_exception(etype, value, tb, limit=None, file=None): ...@@ -84,6 +113,12 @@ def print_exception(etype, value, tb, limit=None, file=None):
_print(file, lines[-1], '') _print(file, lines[-1], '')
def format_exception(etype, value, tb, limit = None): def format_exception(etype, value, tb, limit = None):
"""Format a stack trace and the exception information. The arguments
have the same meaning as the corresponding arguments to
print_exception(). The return value is a list of strings, each
ending in a newline and some containing internal newlines. When
these lines are contatenated and printed, exactly the same text is
printed as does print_exception()."""
if tb: if tb:
list = ['Traceback (innermost last):\n'] list = ['Traceback (innermost last):\n']
list = list + format_tb(tb, limit) list = list + format_tb(tb, limit)
...@@ -93,6 +128,14 @@ def format_exception(etype, value, tb, limit = None): ...@@ -93,6 +128,14 @@ def format_exception(etype, value, tb, limit = None):
return list return list
def format_exception_only(etype, value): def format_exception_only(etype, value):
"""Format the exception part of a traceback. The arguments are the
exception type and value such as given by sys.last_type and
sys.last_value. The return value is a list of strings, each ending
in a newline. Normally, the list contains a single string;
however, for SyntaxError exceptions, it contains several lines that
(when printed) display detailed information about where the syntax
error occurred. The message indicating which exception occurred is
the always last string in the list."""
list = [] list = []
if type(etype) == types.ClassType: if type(etype) == types.ClassType:
stype = etype.__name__ stype = etype.__name__
...@@ -128,6 +171,10 @@ def format_exception_only(etype, value): ...@@ -128,6 +171,10 @@ def format_exception_only(etype, value):
def print_exc(limit=None, file=None): def print_exc(limit=None, file=None):
"""This is a shorthand for 'print_exception(sys.exc_type,
sys.exc_value, sys.exc_traceback, limit, file)'.
(In fact, it uses sys.exc_info() to retrieve the same information
in a thread-safe way.)"""
if not file: if not file:
file = sys.stderr file = sys.stderr
try: try:
...@@ -137,6 +184,8 @@ def print_exc(limit=None, file=None): ...@@ -137,6 +184,8 @@ def print_exc(limit=None, file=None):
etype = value = tb = None etype = value = tb = None
def print_last(limit=None, file=None): def print_last(limit=None, file=None):
"""This is a shorthand for 'print_exception(sys.last_type,
sys.last_value, sys.last_traceback, limit, file)'."""
if not file: if not file:
file = sys.stderr file = sys.stderr
print_exception(sys.last_type, sys.last_value, sys.last_traceback, print_exception(sys.last_type, sys.last_value, sys.last_traceback,
...@@ -144,6 +193,10 @@ def print_last(limit=None, file=None): ...@@ -144,6 +193,10 @@ def print_last(limit=None, file=None):
def print_stack(f=None, limit=None, file=None): def print_stack(f=None, limit=None, file=None):
"""This function prints a stack trace from its invocation point.
The optional 'f' argument can be used to specify an alternate stack
frame at which to start. The optional 'limit' and 'file' arguments
have the same meaning as for print_exception()."""
if f is None: if f is None:
try: try:
raise ZeroDivisionError raise ZeroDivisionError
...@@ -152,6 +205,7 @@ def print_stack(f=None, limit=None, file=None): ...@@ -152,6 +205,7 @@ def print_stack(f=None, limit=None, file=None):
print_list(extract_stack(f, limit), file) print_list(extract_stack(f, limit), file)
def format_stack(f=None, limit=None): def format_stack(f=None, limit=None):
"""A shorthand for 'format_list(extract_stack(f, limit))'."""
if f is None: if f is None:
try: try:
raise ZeroDivisionError raise ZeroDivisionError
...@@ -160,6 +214,12 @@ def format_stack(f=None, limit=None): ...@@ -160,6 +214,12 @@ def format_stack(f=None, limit=None):
return format_list(extract_stack(f, limit)) return format_list(extract_stack(f, limit))
def extract_stack(f=None, limit = None): def extract_stack(f=None, limit = None):
"""Extract the raw traceback from the current stack frame. The
return value has the same format as for extract_tb(). The optional
'f' and 'limit' arguments have the same meaning as for print_stack().
Each item in the list is a quadruple (filename, line number,
function name, text), and the entries are in order from outermost
to innermost stack frame."""
if f is None: if f is None:
try: try:
raise ZeroDivisionError raise ZeroDivisionError
...@@ -184,13 +244,14 @@ def extract_stack(f=None, limit = None): ...@@ -184,13 +244,14 @@ def extract_stack(f=None, limit = None):
list.reverse() list.reverse()
return list return list
# Calculate the correct line number of the traceback given in tb (even
# with -O on).
# Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line()
# in compile.c.
# Revised version by Jim Hugunin to work with JPython too.
def tb_lineno(tb): def tb_lineno(tb):
"""Calculate the correct line number of the traceback given in tb
(even with -O on)."""
# Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line()
# in compile.c.
# Revised version by Jim Hugunin to work with JPython too.
c = tb.tb_frame.f_code c = tb.tb_frame.f_code
if not hasattr(c, 'co_lnotab'): if not hasattr(c, 'co_lnotab'):
return tb.tb_lineno return tb.tb_lineno
......
# tty.py -- Terminal utilities. """Terminal utilities."""
# Author: Steen Lumholt. # Author: Steen Lumholt.
from TERMIOS import * from TERMIOS import *
...@@ -13,8 +14,8 @@ ISPEED = 4 ...@@ -13,8 +14,8 @@ ISPEED = 4
OSPEED = 5 OSPEED = 5
CC = 6 CC = 6
# Put terminal into a raw mode.
def setraw(fd, when=TCSAFLUSH): def setraw(fd, when=TCSAFLUSH):
"""Put terminal into a raw mode."""
mode = tcgetattr(fd) mode = tcgetattr(fd)
mode[IFLAG] = mode[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON) mode[IFLAG] = mode[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON)
mode[OFLAG] = mode[OFLAG] & ~(OPOST) mode[OFLAG] = mode[OFLAG] & ~(OPOST)
...@@ -25,8 +26,8 @@ def setraw(fd, when=TCSAFLUSH): ...@@ -25,8 +26,8 @@ def setraw(fd, when=TCSAFLUSH):
mode[CC][VTIME] = 0 mode[CC][VTIME] = 0
tcsetattr(fd, when, mode) tcsetattr(fd, when, mode)
# Put terminal into a cbreak mode.
def setcbreak(fd, when=TCSAFLUSH): def setcbreak(fd, when=TCSAFLUSH):
"""Put terminal into a cbreak mode."""
mode = tcgetattr(fd) mode = tcgetattr(fd)
mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON) mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON)
mode[CC][VMIN] = 1 mode[CC][VMIN] = 1
......
# Define names for all type symbols known in the standard interpreter. """Define names for all type symbols known in the standard interpreter.
# Types that are part of optional modules (e.g. array) are not listed.
Types that are part of optional modules (e.g. array) are not listed.
"""
import sys import sys
......
# Parse a timezone specification. """Parse a timezone specification."""
# XXX Unfinished. # XXX Unfinished.
# XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported. # XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
...@@ -8,6 +9,12 @@ tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);' ...@@ -8,6 +9,12 @@ tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
tzprog = None tzprog = None
def tzparse(tzstr): def tzparse(tzstr):
"""Given a timezone spec, return a tuple of information
(tzname, delta, dstname, daystart, hourstart, dayend, hourend),
where 'tzname' is the name of the timezone, 'delta' is the offset
in hours from GMT, 'dstname' is the name of the daylight-saving
timezone, and 'daystart'/'hourstart' and 'dayend'/'hourend'
specify the starting and ending points for daylight saving time."""
global tzprog global tzprog
if tzprog == None: if tzprog == None:
import re import re
...@@ -24,6 +31,9 @@ def tzparse(tzstr): ...@@ -24,6 +31,9 @@ def tzparse(tzstr):
return (tzname, delta, dstname, daystart, hourstart, dayend, hourend) return (tzname, delta, dstname, daystart, hourstart, dayend, hourend)
def tzlocaltime(secs, params): def tzlocaltime(secs, params):
"""Given a Unix time in seconds and a tuple of information about
a timezone as returned by tzparse(), return the local time in the
form (year, month, day, hour, min, sec, yday, wday, tzname)."""
import time import time
(tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
year, month, days, hours, mins, secs, yday, wday, isdst = \ year, month, days, hours, mins, secs, yday, wday, isdst = \
...@@ -34,6 +44,7 @@ def tzlocaltime(secs, params): ...@@ -34,6 +44,7 @@ def tzlocaltime(secs, params):
return year, month, days, hours, mins, secs, yday, wday, tzname return year, month, days, hours, mins, secs, yday, wday, tzname
def tzset(): def tzset():
"""Determine the current timezone from the "TZ" environment variable."""
global tzparams, timezone, altzone, daylight, tzname global tzparams, timezone, altzone, daylight, tzname
import os import os
tzstr = os.environ['TZ'] tzstr = os.environ['TZ']
...@@ -44,6 +55,8 @@ def tzset(): ...@@ -44,6 +55,8 @@ def tzset():
tzname = tzparams[0], tzparams[2] tzname = tzparams[0], tzparams[2]
def isdst(secs): def isdst(secs):
"""Return true if daylight-saving time is in effect for the given
Unix time in the current timezone."""
import time import time
(tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \ (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
tzparams tzparams
...@@ -54,6 +67,7 @@ def isdst(secs): ...@@ -54,6 +67,7 @@ def isdst(secs):
tzset() tzset()
def localtime(secs): def localtime(secs):
"""Get the local time in the current timezone."""
return tzlocaltime(secs, tzparams) return tzlocaltime(secs, tzparams)
def test(): def test():
......
This diff is collapsed.
"""An extensible library for opening URLs using a variety protocols """An extensible library for opening URLs using a variety of protocols
The simplest way to use this module is to call the urlopen function, The simplest way to use this module is to call the urlopen function,
which accepts a string containing a URL or a Request object (described which accepts a string containing a URL or a Request object (described
......
# Parse (absolute and relative) URLs. See RFC 1808: "Relative Uniform """Parse (absolute and relative) URLs.
# Resource Locators", by R. Fielding, UC Irvine, June 1995.
See RFC 1808: "Relative Uniform Resource Locators", by R. Fielding,
UC Irvine, June 1995.
"""
# Standard/builtin Python modules # Standard/builtin Python modules
import string import string
...@@ -39,12 +42,12 @@ def clear_cache(): ...@@ -39,12 +42,12 @@ def clear_cache():
_parse_cache = {} _parse_cache = {}
# Parse a URL into 6 components:
# <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
# Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
# Note that we don't break the components up in smaller bits
# (e.g. netloc is a single string) and we don't expand % escapes.
def urlparse(url, scheme = '', allow_fragments = 1): def urlparse(url, scheme = '', allow_fragments = 1):
"""Parse a URL into 6 components:
<scheme>://<netloc>/<path>;<params>?<query>#<fragment>
Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
Note that we don't break the components up in smaller bits
(e.g. netloc is a single string) and we don't expand % escapes."""
key = url, scheme, allow_fragments key = url, scheme, allow_fragments
cached = _parse_cache.get(key, None) cached = _parse_cache.get(key, None)
if cached: if cached:
...@@ -107,11 +110,11 @@ def urlparse(url, scheme = '', allow_fragments = 1): ...@@ -107,11 +110,11 @@ def urlparse(url, scheme = '', allow_fragments = 1):
_parse_cache[key] = tuple _parse_cache[key] = tuple
return tuple return tuple
# Put a parsed URL back together again. This may result in a slightly
# different, but equivalent URL, if the URL that was parsed originally
# had redundant delimiters, e.g. a ? with an empty query (the draft
# states that these are equivalent).
def urlunparse((scheme, netloc, url, params, query, fragment)): def urlunparse((scheme, netloc, url, params, query, fragment)):
"""Put a parsed URL back together again. This may result in a
slightly different, but equivalent URL, if the URL that was parsed
originally had redundant delimiters, e.g. a ? with an empty query
(the draft states that these are equivalent)."""
if netloc or (scheme in uses_netloc and url[:2] == '//'): if netloc or (scheme in uses_netloc and url[:2] == '//'):
if url[:1] != '/': url = '/' + url if url[:1] != '/': url = '/' + url
url = '//' + (netloc or '') + url url = '//' + (netloc or '') + url
...@@ -125,9 +128,9 @@ def urlunparse((scheme, netloc, url, params, query, fragment)): ...@@ -125,9 +128,9 @@ def urlunparse((scheme, netloc, url, params, query, fragment)):
url = url + '#' + fragment url = url + '#' + fragment
return url return url
# Join a base URL and a possibly relative URL to form an absolute
# interpretation of the latter.
def urljoin(base, url, allow_fragments = 1): def urljoin(base, url, allow_fragments = 1):
"""Join a base URL and a possibly relative URL to form an absolute
interpretation of the latter."""
if not base: if not base:
return url return url
bscheme, bnetloc, bpath, bparams, bquery, bfragment = \ bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
......
...@@ -23,11 +23,12 @@ ...@@ -23,11 +23,12 @@
# between ascii and binary. This results in a 1000-fold speedup. The C # between ascii and binary. This results in a 1000-fold speedup. The C
# version is still 5 times faster, though. # version is still 5 times faster, though.
# - Arguments more compliant with python standard # - Arguments more compliant with python standard
#
# This file implements the UUencode and UUdecode functions.
# encode(in_file, out_file [,name, mode]) """Implementation of the UUencode and UUdecode functions.
# decode(in_file [, out_file, mode])
encode(in_file, out_file [,name, mode])
decode(in_file [, out_file, mode])
"""
import binascii import binascii
import os import os
......
This diff is collapsed.
# WICHMANN-HILL RANDOM NUMBER GENERATOR """Wichman-Hill random number generator.
#
# Wichmann, B. A. & Hill, I. D. (1982)
# Algorithm AS 183:
# An efficient and portable pseudo-random number generator
# Applied Statistics 31 (1982) 188-190
#
# see also:
# Correction to Algorithm AS 183
# Applied Statistics 33 (1984) 123
#
# McLeod, A. I. (1985)
# A remark on Algorithm AS 183
# Applied Statistics 34 (1985),198-200
#
#
# USE:
# whrandom.random() yields double precision random numbers
# uniformly distributed between 0 and 1.
#
# whrandom.seed(x, y, z) must be called before whrandom.random()
# to seed the generator
#
# There is also an interface to create multiple independent
# random generators, and to choose from other ranges.
# Translated by Guido van Rossum from C source provided by
# Adrian Baddeley.
# Multi-threading note: the random number generator used here is not
# thread-safe; it is possible that nearly simultaneous calls in
# different theads return the same random value. To avoid this, you
# have to use a lock around all calls. (I didn't want to slow this
# down in the serial case by using a lock here.)
Wichmann, B. A. & Hill, I. D. (1982)
Algorithm AS 183:
An efficient and portable pseudo-random number generator
Applied Statistics 31 (1982) 188-190
see also:
Correction to Algorithm AS 183
Applied Statistics 33 (1984) 123
McLeod, A. I. (1985)
A remark on Algorithm AS 183
Applied Statistics 34 (1985),198-200
USE:
whrandom.random() yields double precision random numbers
uniformly distributed between 0 and 1.
whrandom.seed(x, y, z) must be called before whrandom.random()
to seed the generator
There is also an interface to create multiple independent
random generators, and to choose from other ranges.
Translated by Guido van Rossum from C source provided by
Adrian Baddeley.
Multi-threading note: the random number generator used here is not
thread-safe; it is possible that nearly simultaneous calls in
different theads return the same random value. To avoid this, you
have to use a lock around all calls. (I didn't want to slow this
down in the serial case by using a lock here.)
"""
class whrandom: class whrandom:
#
# Initialize an instance.
# Without arguments, initialize from current time.
# With arguments (x, y, z), initialize from them.
#
def __init__(self, x = 0, y = 0, z = 0): def __init__(self, x = 0, y = 0, z = 0):
"""Initialize an instance.
Without arguments, initialize from current time.
With arguments (x, y, z), initialize from them."""
self.seed(x, y, z) self.seed(x, y, z)
#
# Set the seed from (x, y, z).
# These must be integers in the range [0, 256).
#
def seed(self, x = 0, y = 0, z = 0): def seed(self, x = 0, y = 0, z = 0):
"""Set the seed from (x, y, z).
These must be integers in the range [0, 256)."""
if not type(x) == type(y) == type(z) == type(0): if not type(x) == type(y) == type(z) == type(0):
raise TypeError, 'seeds must be integers' raise TypeError, 'seeds must be integers'
if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256): if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
...@@ -63,10 +61,9 @@ class whrandom: ...@@ -63,10 +61,9 @@ class whrandom:
t, z = divmod(t, 256) t, z = divmod(t, 256)
# Zero is a poor seed, so substitute 1 # Zero is a poor seed, so substitute 1
self._seed = (x or 1, y or 1, z or 1) self._seed = (x or 1, y or 1, z or 1)
#
# Get the next random number in the range [0.0, 1.0).
#
def random(self): def random(self):
"""Get the next random number in the range [0.0, 1.0)."""
# This part is thread-unsafe: # This part is thread-unsafe:
# BEGIN CRITICAL SECTION # BEGIN CRITICAL SECTION
x, y, z = self._seed x, y, z = self._seed
...@@ -79,30 +76,25 @@ class whrandom: ...@@ -79,30 +76,25 @@ class whrandom:
# END CRITICAL SECTION # END CRITICAL SECTION
# #
return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
#
# Get a random number in the range [a, b).
#
def uniform(self, a, b): def uniform(self, a, b):
"""Get a random number in the range [a, b)."""
return a + (b-a) * self.random() return a + (b-a) * self.random()
#
# Get a random integer in the range [a, b] including both end points.
# (Deprecated; use randrange below.)
#
def randint(self, a, b): def randint(self, a, b):
"""Get a random integer in the range [a, b] including both end points.
(Deprecated; use randrange below.)"""
return self.randrange(a, b+1) return self.randrange(a, b+1)
#
# Choose a random element from a non-empty sequence.
#
def choice(self, seq): def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
return seq[int(self.random() * len(seq))] return seq[int(self.random() * len(seq))]
#
# Choose a random item from range([start,] step[, stop]). def randrange(self, start, stop=None, step=1, int=int, default=None):
# This fixes the problem with randint() which includes the """Choose a random item from range([start,] step[, stop]).
# endpoint; in Python this is usually not what you want. This fixes the problem with randint() which includes the
# endpoint; in Python this is usually not what you want.
def randrange(self, start, stop=None, step=1, Do not supply the 'int' and 'default' arguments."""
# Do not supply the following arguments
int=int, default=None):
# This code is a bit messy to make it fast for the # This code is a bit messy to make it fast for the
# common case while still doing adequate error checking # common case while still doing adequate error checking
istart = int(start) istart = int(start)
...@@ -136,7 +128,6 @@ class whrandom: ...@@ -136,7 +128,6 @@ class whrandom:
# Initialize from the current time # Initialize from the current time
#
_inst = whrandom() _inst = whrandom()
seed = _inst.seed seed = _inst.seed
random = _inst.random random = _inst.random
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment