Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cleanup guarantees?

0 views
Skip to first unread message

Alf P. Steinbach

unread,
Apr 9, 2010, 12:13:37 AM4/9/10
to
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

Martin P. Hellwig

unread,
Apr 9, 2010, 1:59:31 AM4/9/10
to
On 04/09/10 05:13, Alf P. Steinbach wrote:
<cut socket open/close>

>
> 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?
>
If nothing else, my guess would be the source somewhere between the
urllib and socket module.

--
mph

Gabriel Genellina

unread,
Apr 9, 2010, 2:33:07 PM4/9/10
to pytho...@python.org
En Fri, 09 Apr 2010 01:13:37 -0300, Alf P. Steinbach <al...@start.no>
escribió:

> <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?

codecs.getreader returns a StreamReader instance (see [1])
StreamReader is documented here [2] and it says "In addition to the above
methods, the StreamReader must also inherit all other methods and
attributes from the underlying stream." -- the stream being 'connection',
from urllib.request.urlopen.
The 3.x version of the documentation in [3] doesn't provide any details
except being "a file-like object". The 2.x version [4] is much more clear:
"a file-like object is returned. This supports the following methods:
read(), readline(), readlines(), fileno(), close(), info(), getcode() and
geturl(). It also has proper support for the iterator protocol."

[1] http://docs.python.org/py3k/library/codecs.html#codecs.getreader
[2] http://docs.python.org/py3k/library/codecs.html#codecs.StreamReader
[3]
http://docs.python.org/py3k/library/urllib.request.html#urllib.request.urlopen
[4] http://docs.python.org/library/urllib.html#urllib.urlopen


> 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 st

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?

Nowhere, AFAIK.
I bet documentation patches are welcome.

--
Gabriel Genellina

Alf P. Steinbach

unread,
Apr 10, 2010, 12:57:49 PM4/10/10
to
* Gabriel Genellina:

Thanks.


> > 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 st
> 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?
>
> Nowhere, AFAIK.
> I bet documentation patches are welcome.

Well, I think I know too little about intentions to do any documentation patches.


Cheers,

- Alf

0 new messages