[SciPy-User] Netcdf exception behavior

Aronne Merrelli aronne.merrelli at gmail.com
Tue Nov 12 10:59:23 EST 2013


Tony,

I can easily replicate this behavior, I do think it is a bug. Here is
another example and a clue for what is happening (I trimmed the exceptions
down for brevity):

In [7]: import scipy.io.netcdf as CDF
In [8]: test = CDF.netcdf_file('foo.cdf','r')
---------------------------------------------------------------------------
IOError: [Errno 2] No such file or directory: 'foo.cdf'

In [9]: print test
Exception AttributeError: "'netcdf_file' object has no attribute 'fp'" in
<bound method netcdf_file.close of <scipy.io.netcdf.netcdf_file object at
0x109666750>> ignored
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-f3e50bd42a45> in <module>()
----> 1 print test

NameError: name 'test' is not defined


I think what happens is that when you run netcdf_file() with a non-existent
file, you wind up with a partly constructed object because the IOError
happens during the __init__() method. The partly constructed object will
eventually be garbage collected, which means its __del__() method will be
called. The way the netcdf_file class is written, it assumes that the
self.fp attribute exists, but it does not exist because of the IOError. So,
you get a second AttributeError, but at some point later, depending on when
the partial object is deleted.

In my example above, attempting to print "test" causes the garbage
collection on the next line (also causing a NameError because "test" is not
defined, but that is not part of the netcdf_file problem). In your example
it is garbage collected immediately, so you get the two exceptions right in
a row.

A possible fix is to just test for the fp attribute first, in the __del__
method, so it won't raise the additional AttributeError.

I could file a Bug report / PR if that sounds reasonable to others - I am
not an expert in python internals, so what I've described here might not be
the "correct" fix - maybe someone on list who is a python expert could
comment =)

Cheers,
Aronne






On Wed, Nov 6, 2013 at 12:35 PM, Mannucci, Anthony J (335G) <
anthony.j.mannucci at jpl.nasa.gov> wrote:

>   This snippet of code does not provide the behavior I expect:
>
>  >>>
> import scipy.io.netcdf as CDF
> import sys
>
>  fitfile = 'simpleXXX.nc'
> try:
>     fnc = CDF.netcdf_file(fitfile, 'r')
> except:
>     sys.stderr.write("WARNING: error.\n")
> else:
>     print fnc.variables
>     fnc.close()
> <<<
>
>  If fitfile exists, the read occurs as expected.
>
>  If the fitfile does not exist, the following message is generated:
>
>  WARNING: error.
> Exception AttributeError: "'netcdf_file' object has no attribute 'fp'" in
> <bound method netcdf_file.close of <scipy.io.netcdf.netcdf_file object at
> 0x44c9290>> ignored
>
>  I expected the exception handling code to eliminate the python exception
> handler.
>
>  Thanks for any help.
>
>  -Tony
>
>   --
>  Tony Mannucci
> Supervisor, Ionospheric and Atmospheric Remote Sensing Group
>  Mail-Stop 138-308,                     Tel > (818) 354-1699
>  Jet Propulsion Laboratory,              Fax > (818) 393-5115
>  California Institute of Technology,     Email >
> Tony.Mannucci at jpl.nasa.gov
>  4800 Oak Grove Drive,
> http://scienceandtechnology.jpl.nasa.gov/people/a_mannucci/
>  Pasadena, CA 91109
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20131112/9b3b7753/attachment.html>


More information about the SciPy-User mailing list