473,473 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: Two functionaly identical functions -> different results ??!

On Wed, Nov 19, 2008 at 11:24 PM, Barak, Ron <Ro*******@lsi.comwrote:
Hi Guys,

I cannot see any difference between read1() and read2() below, and yet, one
is okay, the other give an exception.

In the first run, read2() is executed, and as expected, the text file is
printed

$ cat ./gzip_try.py
import gzip

FILE = "text_file.txt"

def read2():
try:
fl = gzip.GzipFile(FILE, "r")
print fl.read()
except IOError:
fl = open(FILE, "r")
print fl.read()

def read1():
try:
fl = gzip.GzipFile(FILE, "r")
except IOError:
fl = open(FILE, "r")

print fl.read()

read2()
#read1()

$ python ./gzip_try.py
abc
123

In the second run, read1() is executed, and an "IOError: Not a gzipped file"
exception is thrown from the "print fl.read()" line of read1().
This is baffling to me, as the try...except should have established that the
file is a text and not gzip file !

$ cat ./gzip_try.py
import gzip

FILE = "text_file.txt"

def read2():
try:
fl = gzip.GzipFile(FILE, "r")
print fl.read()
except IOError:
fl = open(FILE, "r")
print fl.read()

def read1():
try:
fl = gzip.GzipFile(FILE, "r")
except IOError:
fl = open(FILE, "r")

print fl.read()

#read2()
read1()

$ python ./gzip_try.py
Traceback (most recent call last):
File "./gzip_try.py", line 22, in <module>
read1()
File "./gzip_try.py", line 19, in read1
print fl.read()
File "c:\Python25\lib\gzip.py", line 220, in read
self._read(readsize)
File "c:\Python25\lib\gzip.py", line 263, in _read
self._read_gzip_header()
File "c:\Python25\lib\gzip.py", line 164, in _read_gzip_header
raise IOError, 'Not a gzipped file'
IOError: Not a gzipped file

$

Can anyone explain why read1() throws an exception, while read2() behaves
correctly ?
As I believe someone else pointed out recently in a extremely similar
thread, GzipFile apparently doesn't check that the underlying file is
actually in gzip format until the .read() call, hence why its
placement affects where the exception is thrown and thus how it's
handled. read2() has the .read() for the gzipped case within the
`try`, so the exception, if it's going to get thrown, is thrown there
and handled by the except. In contrast, read1() has the .read() call
outside the `try` and so the possible exception doesn't get caught.
IOW, putting the GzipFile() creation in a `try` is pointless in this
case as .read(), and not the initialization, throws the exception.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Thanks,
Ron.

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

Nov 20 '08 #1
0 1250

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Powell | last post by:
Consider these two classes. Class Accepter in placement_classes.inc.php works as a form validation object, and it works like a charm: PHP: // placement_classes.inc.php - THIS ONE WORKS! ...
3
by: Raseliarison nirinA | last post by:
hi all, i found an unanswered question at http://www.faqts.com/knowledge_base/index.phtml/fid/538 with possible response below. i've tried to send it at faqt.python but can't figure out how to...
3
by: Sander Janssen | last post by:
Hello, I have this peculiar problem concerning MS SQL Server. My company works with an mailing application (ASP) which uses SQL Server as it's repository. What I want to do is send data...
8
by: Ares Lagae | last post by:
When adopting the coding style of the standard C++ library, you often run into naming problems because class names are lower case, and member functions do not have get/set prefixes. For example:...
4
by: Robert W. | last post by:
I've scanned through the various methods of the 'File' library but can't see any method that would be equivalent to the DOS "COMP" command. Is there, in fact, a simple method to do this or do I...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
0
by: prashant | last post by:
Hi, I have set up transactional replication with queued updation. But I want to make sure that identical columns dont get replicated. For that, I wanted to make all identical columns in all the...
18
by: Luminari | last post by:
I have two pages. Both of them are supposed to have one table on it with a fixed header. Both pages were tested in IE6. Structurally they are identical. Both have a <divthat wraps a <tablethat...
3
by: Juha Nieminen | last post by:
It occurred to me while developing an application: What happens if two (completely independent) base classes have the exact same virtual function, and then a derived class is derived from both,...
5
by: gamename | last post by:
Hi, I have 2 vendor libraries. One is for transmitting, the other receiving data. Virtually all of the functions in either lib are almost exactly alike in the way they're called. The only...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.