Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception when closing non-eventio files #282

Merged
merged 1 commit into from
Oct 17, 2024

Conversation

vmharvey
Copy link
Contributor

If a non-eventio file is opened (either a file not in the expected formats, or an empty file such as caused by a failed run of sim_telarray), an exception is raised if close() is called on the EventIOFile object. This happens automatically if the variable goes out of scope, such as in the common operation of a loop over input files.

Example

# File: test.py

import sys
from eventio import EventIOFile

for _ in range(1):
  try:
    file = EventIOFile(sys.argv[1])
  except Exception as ex:
    print("Error:", ex)
    continue

  ## processing would go here ##
$ python test.py /dev/random

Output:

Error: File /dev/random is not an eventio file
Exception ignored in: <function EventIOFile.__del__ at 0x7fcfb01ce2a0>
Traceback (most recent call last):
  File "<snip>/lib/python3.12/site-packages/eventio/base.py", line 156, in __del__
    self.close()
  File "<snip>/lib/python3.12/site-packages/eventio/base.py", line 153, in close
    self._filehandle.close()
    ^^^^^^^^^^^^^^^^
AttributeError: 'EventIOFile' object has no attribute '_filehandle'

Fortunately in this case, the exception isn't considered fatal. But it is still noisy and can be fixed by checking the status of _filehandle before operating on it.

Other notes

Incidentally, in the case of empty files, instead an opaque IndexError is raised by the method that attempts to read the file and discern its type. You can see this if you try /dev/null instead of /dev/random with this test script. But the follow-on issues are the same.

@maxnoe maxnoe merged commit 03ae8e3 into cta-observatory:main Oct 17, 2024
10 checks passed
@vmharvey vmharvey deleted the patch-1 branch October 17, 2024 06:45
@maxnoe
Copy link
Member

maxnoe commented Oct 17, 2024

Thanks a lot @vmharvey

@maxnoe
Copy link
Member

maxnoe commented Oct 17, 2024

I'll also make a fix for the other issue, or do you already have a solution?

@vmharvey
Copy link
Contributor Author

I'll also make a fix for the other issue, or do you already have a solution?

All good! I was considering preparing a fix, but didn't have one ready to go. Perfectly happy to see that you got to it first :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants