This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Possible bug in Python Tutorial
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.1, Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: orsenthil Nosy List: dh, georg.brandl, orsenthil, r.david.murray
Priority: normal Keywords:

Created on 2009-11-26 16:30 by dh, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg95749 - (view) Author: David Henretty (dh) Date: 2009-11-26 16:30
Hi,

In the v3.1.1 Python Tutorial (section 10.7 - Internet Access), the 
sample code (shown below) results in the following error :-

from urllib.request import urlopen
for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
     if 'EST' in line or 'EDT' in line:
         print(line)

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: Type str doesn't support the buffer API

I presume this has something to do with the assumed type of the 
variable 'line', but I am very new to Python.

Replacing the 'if' line with the following DOES work :-

     if 'EST' in str(line) or 'EDT' in str(line):

Can anyone confirm / explain this ?

Thanks,

DH
msg95750 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-11-26 18:22
The example is indeed wrong.  urlopen is returning an object that emits
binary data.  The error comes from using 'in' on incompatible types. 
The solution is to encode the data with an appropriate encoding, once
you figure out what that is.
msg99791 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2010-02-22 17:23
Fixed in r78325, r78326.
History
Date User Action Args
2022-04-11 14:56:55adminsetgithub: 51648
2010-02-22 17:23:05orsenthilsetstatus: open -> closed

nosy: + orsenthil
messages: + msg99791

assignee: georg.brandl -> orsenthil
resolution: fixed
2009-11-26 18:22:36r.david.murraysetpriority: normal

type: compile error -> behavior
versions: + Python 3.2
nosy: + r.david.murray

messages: + msg95750
stage: needs patch
2009-11-26 16:30:25dhcreate