Skip to content

Commit

Permalink
Merge pull request #2410 from takluyver/fix/track-order-root
Browse files Browse the repository at this point in the history
Fix iteration order for reopened files with creation order tracked
  • Loading branch information
tacaswell committed Apr 15, 2024
2 parents d150fc0 + 7e50f9e commit c8972f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
16 changes: 15 additions & 1 deletion h5py/h5f.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ from .utils cimport emalloc, efree
# Python level imports
from collections import namedtuple
import gc
from . import _objects
from . import _objects, h5o
from ._objects import phil, with_phil

from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AsString
Expand Down Expand Up @@ -611,3 +611,17 @@ cdef class FileID(GroupID):
int(evictions[1]), int(bypasses[1]))

return PageBufStats(meta, raw)

# === Special methods =====================================================

# Redefine these methods to use the root group explicitly, because otherwise
# the setting for link order tracking can be missed.
@with_phil
def __iter__(self):
""" Return an iterator over the names of group members. """
return iter(h5o.open(self, b'/'))

@with_phil
def __reversed__(self):
""" Return an iterator over group member names in reverse order. """
return reversed(h5o.open(self, b'/'))
8 changes: 6 additions & 2 deletions h5py/tests/test_file2.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,12 @@ def test_track_order(self):
fname = self.mktemp()
f = h5py.File(fname, 'w', track_order=True) # creation order
self.populate(f)
self.assertEqual(list(f),
[str(i) for i in range(100)])
self.assertEqual(list(f), [str(i) for i in range(100)])
f.close()

# Check order tracking after reopening the file
f2 = h5py.File(fname)
self.assertEqual(list(f2), [str(i) for i in range(100)])

def test_no_track_order(self):
fname = self.mktemp()
Expand Down

0 comments on commit c8972f1

Please sign in to comment.