Cleanup guarantees?

Alf P. Steinbach alfps at start.no
Fri Apr 9 00:13:37 EDT 2010


Consider ...


<code language="Py3">
import urllib.request   # urlopen
import codecs           # getreader
import sys              # stderr

def text_stream_from( url, encoding ):
     text_reader = codecs.getreader( encoding )
     connection = urllib.request.urlopen( url )
     return text_reader( connection )

def list_text( url, encoding ):
     lines = text_stream_from( url, encoding )
     for line in lines:
         print( line, end = "" )
     lines.close()       # Undocumented?

url = "http://www.rfc-editor.org/rfc/rfc1149.txt"
list_text( url, "ascii" )
</code>


First, I'm unable to find documentation that there /is/ a close method in the 
text_reader object, and I'm unable to find documentation that there is a close 
method in the file like connection object; is there such documentation?

Second, I'm unable to find documentation of when they're called and what they 
do. It seems that (A) when the connection object's stream is exhausted by 
reading, its close() method is called automatically, and (B) that when the 
text_reader object's close method is called it calls the close method of the 
wrapped stream (i.e. on the connection object). But where is this documented?


Cheers,

- Alf



More information about the Python-list mailing list