NCAR / pynio

PyNIO is a multi-format data I/O package with a NetCDF-style interface
http://www.pyngl.ucar.edu/Nio.shtml
Apache License 2.0
112 stars 37 forks source link

Reference issue for functions returning variables #25

Open bladwig1 opened 6 years ago

bladwig1 commented 6 years ago

The following code fails:

>>> def test():
...     x = Nio.open_file("wrfout_d02_2005-08-28_12:00:00.nc", "r")
...     y = x.variables["P"]
...     return y
... 
>>> q = test()
>>> q[:]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ladwig/miniconda2/envs/pynio2_build/lib/python2.7/site-packages/PyNIO/Nio.py", line 347, in __getitem__
    ret = get_variable(self.file, self.varname, xsel)
  File "/Users/ladwig/miniconda2/envs/pynio2_build/lib/python2.7/site-packages/PyNIO/coordsel.py", line 39, in get_variable
    dims = file.variables[varname].cf_dimensions
ReferenceError: weakly-referenced object no longer exists

It looks like in the newer 1.5.x series of PyNIO, variables are being stored as weak references. This is going to be difficult to fix. Ideally, the weak reference stuff needs to be removed in favor of cyclic garbage collection (e.g. (traverseproc)nio_traverse).