Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- A failed open of a dfs file shorter than 4 bytes, such as an empty file, no
longer leaves the file open. Each such open used up a file handle, so after
about 500 of them no file could be opened (Error code 2003)
([#85](https://github.com/DHI/mikecore-python/issues/85)).
- `import mikecore` works with a MIKE installation's `bin` on
`LD_LIBRARY_PATH`, for example after sourcing `mikevars.sh`. It failed with
`undefined symbol: _Z13fnGetLanguagev`, because the bundled libraries
Expand Down
9 changes: 8 additions & 1 deletion mikecore/DfsFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import locale
import numpy as np
from mikecore.eum import *
from mikecore.DfsDLL import DfsDLL
from mikecore.DfsDLL import DfsDLL, DfsError
from mikecore.eum import eumQuantity


Expand Down Expand Up @@ -811,6 +811,13 @@ def Open(self, filename, mode=DfsFileMode.Read, parameters=None):
"File is readonly and can not be opened for editing: " + filename
)

# The native library leaves the file open when it can not read the
# first 4-byte tag, so refuse such a file with the same error.
if os.path.getsize(filename) < 4:
raise Exception(
f"Could not load file {filename} (Error code {DfsError.F_ERR_READ})"
)

if parameters is None:
parameters = DfsParameters()

Expand Down
29 changes: 29 additions & 0 deletions tests/test_dfs_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ctypes
import os
import sys

import pytest

Expand Down Expand Up @@ -153,3 +155,30 @@ def test_item_quantity_descriptions():

assert ["Water Level", "Water Depth"] == [q.ItemDescription for q in quantities]
assert ["meter", "meter"] == [q.UnitDescription for q in quantities]


def open_file_count():
return len(os.listdir("/proc/self/fd"))


@pytest.mark.skipif(sys.platform != "linux", reason="counts /proc/self/fd")
@pytest.mark.parametrize("size, error_code", [(0, 2001), (3, 2001), (4, 2014)])
@pytest.mark.parametrize(
"open_file",
[
DfsFileFactory.DfsGenericOpen,
DfsFileFactory.DfsGenericOpenEdit,
DfsFileFactory.DfsGenericOpenAppend,
],
)
def test_failed_open_of_short_file_leaves_no_file_open(
tmp_path, size, error_code, open_file
):
filename = tmp_path / "short.dfs0"
filename.write_bytes(b"x" * size)
before = open_file_count()

with pytest.raises(Exception, match=rf"\(Error code {error_code}\)"):
open_file(str(filename))

assert open_file_count() == before
Loading