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 DEBUG_HDF5 and some minor leaks, add fromH5 for ref objects #78

Merged
merged 7 commits into from
Sep 21, 2024
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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
- 'docs/**'
- 'nimhdf5.nimble'
- '.github/workflows/ci.yml'
branches:
- 'master'
pull_request:
paths:
- 'tests/**'
Expand Down
2 changes: 2 additions & 0 deletions src/nimhdf5/attributes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import typeinfo
import tables
import strutils, sequtils

Check warning on line 9 in src/nimhdf5/attributes.nim

View workflow job for this annotation

GitHub Actions / linux (version-1-6)

imported and not used: 'sequtils' [UnusedImport]

import hdf5_wrapper, h5util, H5nimtypes, datatypes, dataspaces, util

Expand Down Expand Up @@ -398,6 +398,7 @@
result = newSeq[string](npoints)
for i, s in buf:
result[i] = $s
discard H5free_memory(s[0].address)

proc readStringAttribute(attr: H5Attr): string =
## proc to read a string attribute from a H5 file, for an existing
Expand All @@ -413,6 +414,7 @@
var buf: cstring
readAttribute(attr, nativeType, buf.addr)
result = $buf
discard H5free_memory(buf.addr)
else:
let nativeType = getNativeType(attr.dtype_c)
let string_len = H5Aget_storage_size(attr.attr_id.id)
Expand Down
2 changes: 1 addition & 1 deletion src/nimhdf5/dataspaces.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func simple_dataspace*[T: (seq | int)](shape: T, maxshape: seq[int] = @[]): Data
## TODO: rewrite this
var m_maxshape: seq[hsize_t] = parseMaxShape(maxshape)
withDebug:
echo "Creating memory dataspace of shape ", shape
debugecho "Creating memory dataspace of shape ", shape
when T is seq:
# convert ints to hsize_t (== culonglong) and create mutable copy (need
# an address to hand it to C function as pointer)
Expand Down
8 changes: 5 additions & 3 deletions src/nimhdf5/datatypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@

proc close*(h5id: H5Id | H5IdObj, msg = "")
proc `=copy`*(target: var H5IdObj, source: H5IdObj) {.error: "H5Id identifiers cannot be copied.".}
proc `=destroy`*(h5id: var H5IdObj) = # {.error: "`=destroy` of a raw `H5Id` is not a valid operation.".}

Check warning on line 323 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 323 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]
h5id.close()

Check warning on line 324 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

close(h5id, "") can raise an unlisted exception: Exception [Effect]

Check warning on line 324 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-1-6)

close(h5id, "") can raise an unlisted exception: Exception [Effect]

Check warning on line 324 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-1-6)

close(h5id, "") can raise an unlisted exception: Exception [Effect]

Check warning on line 324 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

close(h5id, "") can raise an unlisted exception: Exception [Effect]
`=destroy`(h5id.kind)
`=destroy`(h5id.id)

Expand Down Expand Up @@ -519,7 +519,9 @@
proc getMemberName*(typ: DatatypeID, idx: int): string =
## Get the name of the member of the H5T_COMPOUND type at index `idx`
## The argument *must* correspond to a compound datatype. We do not check.
result = $H5Tget_member_name(typ.id, idx.cuint)
var name = H5Tget_member_name(typ.id, idx.cuint)
result = $name
discard H5free_memory(name[0].addr)

proc getSize*(typ: DatatypeID): int =
result = H5Tget_size(typ.id).int
Expand Down Expand Up @@ -626,9 +628,9 @@
proc close*(dset: H5DataSet) = dset[].close()

when (NimMajor, NimMinor, NimPatch) >= (1, 6, 0):
proc `=destroy`*(attr: var H5AttrObj) {.raises: [HDF5LibraryError].} =

Check warning on line 631 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 631 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]
## Closes the given attribute.
attr.close()

Check warning on line 633 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

close(attr) can raise an unlisted exception: Exception [Effect]

Check warning on line 633 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-1-6)

close(attr) can raise an unlisted exception: Exception [Effect]

Check warning on line 633 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-1-6)

close(attr) can raise an unlisted exception: Exception [Effect]

Check warning on line 633 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

close(attr) can raise an unlisted exception: Exception [Effect]
for name, field in fieldPairs(attr):
when typeof(field).distinctBase() isnot H5Id:
`=destroy`(field)
Expand All @@ -636,21 +638,21 @@
if cast[pointer](field) != nil:
`=destroy`(field)

proc `=destroy`*(attrs: var H5AttributesObj) =

Check warning on line 641 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 641 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]
## Closes all attributes that are stored in this `H5Attributes` table.
if attrs.attr_tab != nil:
`=destroy`(attrs.attr_tab)
for name, field in fieldPairs(attrs):
when typeof(field).distinctBase() isnot H5Id:
if name.normalize != "attrtab":
`=destroy`(field)

Check warning on line 648 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

`=destroy`(attrs.parent_id) can raise an unlisted exception: Exception [Effect]

Check warning on line 648 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

`=destroy`(attrs.parent_id) can raise an unlisted exception: Exception [Effect]
else:
if cast[pointer](field) != nil:
`=destroy`(field)

proc `=destroy`*(dset: var H5DataSetObj) {.raises: [HDF5LibraryError].} =

Check warning on line 653 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 653 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]
## Closes the dataset and resets all references to nil.
dset.close() # closes its dataspace etc. as well

Check warning on line 655 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

close(dset) can raise an unlisted exception: Exception [Effect]

Check warning on line 655 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-1-6)

close(dset) can raise an unlisted exception: Exception [Effect]

Check warning on line 655 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

close(dset) can raise an unlisted exception: Exception [Effect]
dset.opened = false
if dset.attrs != nil:
`=destroy`(dset.attrs)
Expand All @@ -663,7 +665,7 @@
`=destroy`(field)


proc `=destroy`*(grp: var H5GroupObj) {.raises: [HDF5LibraryError].} =

Check warning on line 668 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 668 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]
## Closes the group and resets all references to nil.
if grp.file_ref != nil:
`=destroy`(grp.file_ref) # only destroy the `ref` to the file!
Expand All @@ -674,7 +676,7 @@
if grp.groups != nil:
`=destroy`(grp.groups)
if grp.attrs != nil:
`=destroy`(grp.attrs)

Check warning on line 679 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (version-2-0)

`=destroy`(grp.attrs) can raise an unlisted exception: Exception [Effect]

Check warning on line 679 in src/nimhdf5/datatypes.nim

View workflow job for this annotation

GitHub Actions / linux (devel)

`=destroy`(grp.attrs) can raise an unlisted exception: Exception [Effect]
for name, field in fieldPairs(grp):
when typeof(field).distinctBase() isnot H5Id:
if name notin ["attrs", "groups", "datasets", "file_ref"]:
Expand Down Expand Up @@ -725,11 +727,11 @@
if err >= 0:
# successful
withDebug:
debugEcho "getNumAttrs(): ", h5attr
debugEcho "getNumAttrs(): ", $(h5attr[])
result = int(h5info.num_attrs)
else:
withDebug:
debugEcho "getNumAttrs(): ", h5attr
debugEcho "getNumAttrs(): ", $(h5attr[])
raise newException(HDF5LibraryError, "Call to HDF5 library failed in `getNumAttr` when reading $#" % $h5attr.parent_name)

proc initH5Attributes*(p_id: sink ParentID, p_name: string = "", p_type: string = ""): H5Attributes =
Expand Down
2 changes: 1 addition & 1 deletion src/nimhdf5/files.nim
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ proc activateSWMR*(h5f: H5File) =
h5f.accessFlags.incl akWriteSWMR
elif akWriteSWMR in h5f.accessFlags:
withDebug:
echo "File was already in SWMR mode: ", h5f
echo "File was already in SWMR mode: ", $(h5f[])
discard # already activated
else:
raise newException(IOError, "Cannot activate SWMR mode for an input file that misses " &
Expand Down
11 changes: 8 additions & 3 deletions src/nimhdf5/serialize.nim
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ proc fromH5*[T: char | string | cstring | bool](h5f: H5File, res: var T, name =
res = parseBool(obj.attrs[name, string])
elif T is char:
let s = obj.attrs[name, string]
doASsert s.len == 0, "Trying to read a char from a string with more than one element."
doAssert s.len == 1, "Trying to read a char from a string with more than one element."
res = s[0]
elif T is string:
res = obj.attrs[name, string]
Expand Down Expand Up @@ -292,14 +292,19 @@ proc fromH5*[T: object](h5f: H5File, res: var T, name = "", path = "/", exclude:
for field, val in fieldPairs(res):
if field notin exclude:
h5f.fromH5(val, field, grp)
proc fromH5*[T: ref object](h5f: H5File, res: var T, name = "", path = "/", exclude: seq[string] = @[]) =
bind `/`
let grp = path / name
res = T()
fromH5(h5f, res[], name, path, exclude)

proc deserializeH5*[T: object](h5f: H5File, name = "", path = "/", exclude: seq[string] = @[]): T =
proc deserializeH5*[T](h5f: H5File, name = "", path = "/", exclude: seq[string] = @[]): T =
## Cannot name it same as `fromH5` because that causes the compiler to get confused. I don't understand,
## but with `LikelihoodContext` it ends up calling the `var set[LikelihoodContext]` overload, which does
## not make any sense.
h5f.fromH5(result, name, path, exclude)

proc deserializeH5*[T: object](fname: string, name = "", path = "/", exclude: seq[string] = @[]): T =
proc deserializeH5*[T](fname: string, name = "", path = "/", exclude: seq[string] = @[]): T =
## Cannot name it same as `fromH5` because that causes the compiler to get confused. I don't understand,
## but with `LikelihoodContext` it ends up calling the `var set[LikelihoodContext]` overload, which does
## not make any sense.
Expand Down
5 changes: 3 additions & 2 deletions src/nimhdf5/util.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ template withDebug*(actions: untyped) =
## to this template are only performed, if the
## -d:DEBUG_HDF5
## compiler flag is set.
when defined(DEBUG_HDF5):
actions
block:
when defined(DEBUG_HDF5):
actions

proc formatName*(name: string): string =
# this procedure formats a given group / dataset name by prepending
Expand Down
Loading