? dist/src/Mac/IDE scripts/Hold option to open a script ? dist/src/Mac/IDE scripts/Insert file name ? dist/src/Mac/IDE scripts/Insert folder name ? dist/src/Mac/IDE scripts/Search Python Documentation ? dist/src/Mac/IDE scripts/Hack/Remove .pyc files ? dist/src/Mac/IDE scripts/Hack/Toolbox Assistant Index: dist/src/Doc/lib/libfileinput.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libfileinput.tex,v retrieving revision 1.7 diff -c -r1.7 libfileinput.tex *** dist/src/Doc/lib/libfileinput.tex 10 Nov 2003 14:43:16 -0000 1.7 --- dist/src/Doc/lib/libfileinput.tex 16 Oct 2004 03:49:28 -0000 *************** *** 44,50 **** The following function is the primary interface of this module: \begin{funcdesc}{input}{\optional{files\optional{, ! inplace\optional{, backup}}}} Create an instance of the \class{FileInput} class. The instance will be used as global state for the functions of this module, and is also returned to use during iteration. The parameters to this --- 44,50 ---- The following function is the primary interface of this module: \begin{funcdesc}{input}{\optional{files\optional{, ! inplace\optional{, backup\optional{, encoding}}}}} Create an instance of the \class{FileInput} class. The instance will be used as global state for the functions of this module, and is also returned to use during iteration. The parameters to this *************** *** 105,111 **** module is available for subclassing as well: \begin{classdesc}{FileInput}{\optional{files\optional{, ! inplace\optional{, backup}}}} Class \class{FileInput} is the implementation; its methods \method{filename()}, \method{lineno()}, \method{fileline()}, \method{isfirstline()}, \method{isstdin()}, \method{nextfile()} and --- 105,112 ---- module is available for subclassing as well: \begin{classdesc}{FileInput}{\optional{files\optional{, ! inplace\optional{, backup\optional{, ! encoding}}}}} Class \class{FileInput} is the implementation; its methods \method{filename()}, \method{lineno()}, \method{fileline()}, \method{isfirstline()}, \method{isstdin()}, \method{nextfile()} and *************** *** 120,133 **** \strong{Optional in-place filtering:} if the keyword argument \code{\var{inplace}=1} is passed to \function{input()} or to the \class{FileInput} constructor, the file is moved to a backup file and ! standard output is directed to the input file (if a file of the same ! name as the backup file already exists, it will be replaced silently). ! This makes it possible to write a filter that rewrites its input file ! in place. If the keyword argument \code{\var{backup}='.'} is also given, it specifies the extension for the backup ! file, and the backup file remains around; by default, the extension is ! \code{'.bak'} and it is deleted when the output file is closed. In-place ! filtering is disabled when standard input is read. \strong{Caveat:} The current implementation does not work for MS-DOS 8+3 filesystems. --- 121,145 ---- \strong{Optional in-place filtering:} if the keyword argument \code{\var{inplace}=1} is passed to \function{input()} or to the \class{FileInput} constructor, the file is moved to a backup file and ! standard output is directed to the input file (the backup file uses ! os.tmpfile, if available, or else it uses a random filename that is ! not in use in the current directory). This makes it possible to write ! a filter that rewrites its input file in place. If the keyword ! argument \code{\var{backup}='.'} is also given, it ! specifies the extension for the backup file, and the backup file ! remains around; by default, the extension is \code{'.bak'} and it is ! deleted when the output file is closed. \strong{If a backup extension ! is specified, it will clobber any existing files in the current ! directory if they have the name of the current file plus extension.} ! In-place filtering is disabled when standard input is read. \strong{Caveat:} The current implementation does not work for MS-DOS 8+3 filesystems. + + \strong{Optional file encoding support:} if the keyword argument + \code{\var{encoding}} is specified, I/O will be done using the codec + for the specified encoding. The type of objects returned from reads + will depend on the encoding specified. \strong{Note:} Universal + newlines support is dependent on the implementation of the + \function{readlines()} method of the StreamWriter class for the + specified codec. Index: dist/src/Lib/fileinput.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/fileinput.py,v retrieving revision 1.16 diff -c -r1.16 fileinput.py *** dist/src/Lib/fileinput.py 8 Jan 2003 16:33:16 -0000 1.16 --- dist/src/Lib/fileinput.py 16 Oct 2004 03:49:30 -0000 *************** *** 79,85 **** """ ! import sys, os __all__ = ["input","close","nextfile","filename","lineno","filelineno", "isfirstline","isstdin","FileInput"] --- 79,85 ---- """ ! import sys, os, codecs __all__ = ["input","close","nextfile","filename","lineno","filelineno", "isfirstline","isstdin","FileInput"] *************** *** 88,94 **** DEFAULT_BUFSIZE = 8*1024 ! def input(files=None, inplace=0, backup="", bufsize=0): """input([files[, inplace[, backup]]]) Create an instance of the FileInput class. The instance will be used --- 88,94 ---- DEFAULT_BUFSIZE = 8*1024 ! def input(files=None, inplace=0, backup="", bufsize=0, encoding=None): """input([files[, inplace[, backup]]]) Create an instance of the FileInput class. The instance will be used *************** *** 99,105 **** global _state if _state and _state._file: raise RuntimeError, "input() already active" ! _state = FileInput(files, inplace, backup, bufsize) return _state def close(): --- 99,105 ---- global _state if _state and _state._file: raise RuntimeError, "input() already active" ! _state = FileInput(files, inplace, backup, bufsize, encoding) return _state def close(): *************** *** 183,189 **** sequential order; random access and readline() cannot be mixed. """ ! def __init__(self, files=None, inplace=0, backup="", bufsize=0): if type(files) == type(''): files = (files,) else: --- 183,190 ---- sequential order; random access and readline() cannot be mixed. """ ! def __init__(self, files=None, inplace=0, backup="", bufsize=0, ! encoding=None): if type(files) == type(''): files = (files,) else: *************** *** 196,201 **** --- 197,203 ---- self._files = files self._inplace = inplace self._backup = backup + self._encoding = encoding self._bufsize = bufsize or DEFAULT_BUFSIZE self._savestdout = None self._output = None *************** *** 207,212 **** --- 209,221 ---- self._backupfilename = None self._buffer = [] self._bufindex = 0 + # need parameterized file modes, since the files backing + # encodings should be raw bytestreams + self._readmode = 'r' + self._writemode = 'w' + if self._encoding: + self._readmode += 'b' + self._writemode += 'b' def __del__(self): self.close() *************** *** 243,254 **** def nextfile(self): savestdout = self._savestdout ! self._savestdout = 0 if savestdout: sys.stdout = savestdout output = self._output ! self._output = 0 if output: output.close() --- 252,263 ---- def nextfile(self): savestdout = self._savestdout ! self._savestdout = None if savestdout: sys.stdout = savestdout output = self._output ! self._output = None if output: output.close() *************** *** 285,323 **** self._filelineno = 0 self._file = None self._isstdin = False ! self._backupfilename = 0 if self._filename == '-': self._filename = '' self._file = sys.stdin self._isstdin = True else: if self._inplace: ! self._backupfilename = ( ! self._filename + (self._backup or os.extsep+"bak")) ! try: os.unlink(self._backupfilename) ! except os.error: pass ! # The next few lines may raise IOError ! os.rename(self._filename, self._backupfilename) ! self._file = open(self._backupfilename, "r") ! try: ! perm = os.fstat(self._file.fileno()).st_mode ! except OSError: ! self._output = open(self._filename, "w") else: - fd = os.open(self._filename, - os.O_CREAT | os.O_WRONLY | os.O_TRUNC, - perm) - self._output = os.fdopen(fd, "w") try: ! if hasattr(os, 'chmod'): ! os.chmod(self._filename, perm) ! except OSError: pass self._savestdout = sys.stdout sys.stdout = self._output else: # This may raise IOError ! self._file = open(self._filename, "r") self._buffer = self._file.readlines(self._bufsize) self._bufindex = 0 if not self._buffer: --- 294,350 ---- self._filelineno = 0 self._file = None self._isstdin = False ! self._backupfilename = None if self._filename == '-': self._filename = '' self._file = sys.stdin self._isstdin = True else: if self._inplace: ! if self._backup: ! self._backupfilename = self._filename + self._backup else: try: ! self._file = os.tmpfile() ! # copy our input into the tmpfile to keep the same ! # backup/write relationship ! self._file.write(open(self._filename,'rb').read()) ! self._file.seek(0) ! except NameError: # tmpfile not available ! import random ! self._backupfilename = self._filename ! while os.path.exists( self._backupfilename ): ! self._backupfilename += ( ! '%x' % random.randint(0, 16)) ! # The next few lines may raise IOError ! if not self._file: # _file may already be a tmpfile ! os.unlink(self._backupfilename) ! # self._backupfilename will only exist if the ! # user explicitly specified an extension to ! # use, i.e., it's their fault if we clobber ! # something now ! os.rename(self._filename, self._backupfilename) ! self._file = open(self._backupfilename, self._readmode) ! try: ! perm = os.stat(self._backupfilename).st_mode ! os.chmod(self._filename,perm) ! except (OSError, NameError): pass + + self._output = open(self._filename, self._writemode) self._savestdout = sys.stdout sys.stdout = self._output else: # This may raise IOError ! self._file = open(self._filename, self._readmode) ! # now wrap all our new file objects if the user specified ! # an encoding ! if self._encoding: ! (x, x, reader, writer) = codecs.lookup(self._encoding) ! self._file = reader(self._file) ! if self._output: ! self._output = writer(self._output) ! sys.stdout = self._output self._buffer = self._file.readlines(self._bufsize) self._bufindex = 0 if not self._buffer: Index: dist/src/Lib/test/test_fileinput.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_fileinput.py,v retrieving revision 1.2 diff -c -r1.2 test_fileinput.py *** dist/src/Lib/test/test_fileinput.py 23 Jul 2002 19:03:52 -0000 1.2 --- dist/src/Lib/test/test_fileinput.py 16 Oct 2004 03:49:32 -0000 *************** *** 157,159 **** --- 157,177 ---- verify(fi.lineno() == 6) finally: remove_tempfiles(t1, t2) + + if verbose: + print "15. UTF-8 encoded Unicode files" + try: + t1 = writeTmp(1, [u"\u263a\n\u266e".encode('utf-8')]) + fi = FileInput(files=(t1,), encoding='utf-8' ) + lines = list(fi) + # the codecs module does not support universal newlines in the + # readlines() method of StreamReader + verify(lines in ([u"\u263a\n", u"\u266e"], + [u"\u263a\r\n", u"\u266e"], + [u"\u263a\r", u"\u266e"])) + verify(fi.filelineno() == 2) + verify(fi.lineno() == 2) + finally: + remove_tempfiles(t1) + +