'''
    A python 3 question.
    Presume this code is in file p.py.
    The program fails.

        $ python3 p.py
        ...
        ValueError: I/O operation on closed file.

    Removing the comment character to increase the stream
    reference count fixes the program, at the expense of
    an extra TextIOWrapper object.

    Please, what is a better way to write the class with
    regard to this issue?
'''

import re
import io

class file(io.TextIOWrapper):

    '''
        Enhance TextIO.  Streams have many sources,
        a file name is insufficient.
    '''

    def __init__(self,stream):
        #self.stream = stream
        super().__init__(stream.buffer)

    def seek_pattern(self,pattern):
        '''
            A motivational method, otherwise inconsequential to the
problem.
        '''
        search = re.compile(pattern).search
        while True:
            line = next(self)
            if (not line) or search(line):
                return line


print(file(open('p.py')).read())

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to