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 ResourceWarning in tests (and more) #225

Merged
merged 3 commits into from
Jun 16, 2024
Merged

Conversation

sbraz
Copy link
Contributor

@sbraz sbraz commented Jun 16, 2024

Hi @otsaloma,
I noticed tests showed a lot of warnings so I attempted to fix those related to filehandles. Please let me know if I made any mistake, especially when I touched files that are not only used for tests.

For instance:
```
aeidon/test/test_encodings.py:166: ResourceWarning: unclosed file <_io.BufferedReader name='/tmp/gaupol-my14g87e.srt'>
    blob = open(path, "rb").read()
```
For example:
```
aeidon/test/test_encodings.py:167: ResourceWarning: unclosed file <_io.BufferedWriter name='/tmp/gaupol-my14g87e.srt'>
    open(path, "wb").write(codecs.BOM_UTF16_BE + blob)
```
Some of these changes are not strictly necessary as the file descriptor
was properly closed in some cases but I decided to change them for
increased consistency.
@otsaloma
Copy link
Owner

otsaloma commented Jun 16, 2024

Thanks! I think this is quite straight-forward, but can you post the command you're using to run the tests and what exact warning do you get related to these?

@sbraz
Copy link
Contributor Author

sbraz commented Jun 16, 2024

You can try something like pytest -Wdefault aeidon/test/test_encodings.py and you will see the warnings with the master branch.

@otsaloma
Copy link
Owner

Oh, right. I've been using

export PYTEST_ADDOPTS="-p no:cacheprovider -p no:hypothesispytest -p no:warnings"

in my ~/.profile, probably because of excessive warnings from GTK.

It's this:

aeidon/test/test_encodings.py::TestModule::test_detect_bom__utf_8_sig
  /home/osmo/Source/gaupol/aeidon/test/test_encodings.py:199: ResourceWarning: unclosed file <_io.BufferedWriter name='/tmp/gaupol-lll8hiun.srt'>
    open(path, "wb").write(codecs.BOM_UTF8 + blob)

This might have been around for quite a while. I remember I left those open intentionally to squeeze three lines (open, write, close) into one. That was before with was added to the language. This is fine to fix now.

@otsaloma otsaloma merged commit 5347ec9 into otsaloma:master Jun 16, 2024
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