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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Rebert

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

    On Wed, Nov 19, 2008 at 11:24 PM, Barak, Ron <Ron.Barak@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(F ILE, "r")
    print fl.read()
    except IOError:
    fl = open(FILE, "r")
    print fl.read()
    >
    def read1():
    try:
    fl = gzip.GzipFile(F ILE, "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(F ILE, "r")
    print fl.read()
    except IOError:
    fl = open(FILE, "r")
    print fl.read()
    >
    def read1():
    try:
    fl = gzip.GzipFile(F ILE, "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\li b\gzip.py", line 220, in read
    self._read(read size)
    File "c:\Python25\li b\gzip.py", line 263, in _read
    self._read_gzip _header()
    File "c:\Python25\li b\gzip.py", line 164, in _read_gzip_head er
    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...

Working...