Skip to content

Improve CFReader destructor#3336

Closed
evertrol wants to merge 2 commits intoSciTools:masterfrom
evertrol:cfreader-improve-destructor
Closed

Improve CFReader destructor#3336
evertrol wants to merge 2 commits intoSciTools:masterfrom
evertrol:cfreader-improve-destructor

Conversation

@evertrol
Copy link

This improves the CFReader's destructor, by testing for self._dataset to exist. It fixes #3312.

This prevents errors when cleaning up CFReader during garbage
collection.
with io.StringIO() as buf:
with contextlib.redirect_stderr(buf):
try:
reader = cf.CFReader(fp.name)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F841 local variable 'reader' is assigned to but never used

except OSError:
pass
try:
cubes = iris.load_cubes(fp.name)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F841 local variable 'cubes' is assigned to but never used

@SciTools-assistant SciTools-assistant added the Blocked: CLA needed See https://scitools.org.uk. Submit the form at: https://scitools.org.uk/cla/v4/form label Jun 19, 2019
@evertrol
Copy link
Author

I'm not entirely happy with the unit test:

  • it is included in the TestCFReader test-case class, where it logically belongs, but it doesn't use any of the class attributes, nor does it require the setUp part, or is dependent on test data. Ideally, it should be its own test
  • it is the odd one out, between other tests, requiring three additional imports, and redirection/capture of standard error
  • it won't run on Python 2 (or rather, return early)
  • I'm also too much used to pytest that I find it hard to write within the unittest framework (see also the first bullet point)

This test is slightly complicated: it tests that no errors occur
during garbage collection of the CFReader; that is, during use of the
`__del__` method.

Any errors that may occur, however, will not be raised, but just
printed to standard error, as a safety measure (since a destructor
should always run and complete). Thus, the standard error is captured
and its length verified to be zero.

For the actual test, we create a netCDF4 file with a proper signature
(HDF5 actually), but otherwise broken; the correct signature (magic
number) is needed for use in the indirect `load_cubes` call, which
first attempts to determine the file type before instantiating a
`CFReader` object.

A final remark: this test does not fit very well in the unittest
class, since it doesn't rely on external data. For clarity, however,
it is kept within the TestCFReader class.
@bjlittle bjlittle self-assigned this Jun 19, 2019
@bjlittle bjlittle added this to the v2.3.0 milestone Jun 19, 2019
@stephenworsley
Copy link
Contributor

Hello @evertrol .
Just a couple minor things to do to get the other checks to pass:
It looks like you'll need a CLA. You can get this by filling out this form https://scitools.org.uk/cla/v4/form .
Also, if you change the year license header in each of the files you edited from 2017/2018 to 2019, that should get Travis to pass.

@evertrol
Copy link
Author

I'm curious about the copyright year thing, because I don't understand it fully: does Travis check for modified file that the copyright (but code-commented) line contains the current year?

How does this work for files that haven't been touched for a few years, but contain two dates (e.g., _fast_load.py, © 2016 - 2018); is the copyright for such files expired?
And how does this work for single-date copyrights that aren't 2019? Shouldn't all files have their copyright section updated?

Is the copyright for the entire project not simply renewed when a new version of Iris is released, as per the README (I missed a LICENSE file)?

@pp-mo
Copy link
Member

pp-mo commented Aug 22, 2019

@evertrol the copyright year thing

Following revised legal advice, we are now planning to get rid of this anyway : #3285

@evertrol
Copy link
Author

@pp-mo Thanks for that. Nice to see the default license will be BSD 3-clause (in an LICENSE file), for SciTools projects. Though the end-year of the copyright remains confusing to me (perhaps it shouldn't be YEAR_END but YEAR_MOST_RECENT).

@evertrol
Copy link
Author

I'm going to close and hand off this PR, since I'm not inclined to sign the CLA: that is implicit by submitting this PR.
Someone else is free to take the changes and submit their own PR with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Blocked: CLA needed See https://scitools.org.uk. Submit the form at: https://scitools.org.uk/cla/v4/form Release: Minor Type: Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Iris causes AttributeError when attempting to catch an OSError when loading a dataset

6 participants