Perplexing assignment behavior with h5py object as instance variable

后端 未结 2 1388
鱼传尺愫
鱼传尺愫 2021-01-14 03:30

I\'m using h5py to access HDF5 files and store the h5py File objects in a class. But I\'m experiencing some strange behavior in attempting to reassign a closed h5py file ins

相关标签:
2条回答
  • 2021-01-14 03:50

    Yes, this is a known bug in h5py 1.3, which shows up when you use HDF5 1.8.5 or newer. It's related to changes in the way identifiers are handled in 1.8.5. You can fix it by using HDF5 1.8.4 or earlier, or by upgrading to h5py 2.0.

    0 讨论(0)
  • 2021-01-14 04:00

    Not sure if this will help, but searching through the source code I found this (abbreviated):

    class HLObject(object):
        def __nonzero__(self):
            register_thread()
            return self.id.__nonzero__()
    
    class Group(HLObject, _DictCompat):
        ...
    
    class File(Group):
        def __repr__(self):
            register_thread()
            if not self:
                return "<Closed HDF5 file>"
            return '<HDF5 file "%s" (mode %s, %s)>' % \
                (os.path.basename(self.filename), self.mode,
                 _extras.sizestring(self.fid.get_filesize()))
    

    Because there is no __str__ method, __repr__ is called to produce the output, and __repr__ first calls register_thread(), then checks to see if self is alive (better known as evaluating to True or False).

    Python then searches the classes until it finds __nonzero__ (which again calls register_thread()), then returns self.id.__nonzero__(), which is apparently returning False.

    So, you are correct in that the issue is not with the name binding (assignment), but why register_thread and/or self.id is bombing out on you, I do not know.

    0 讨论(0)
提交回复
热议问题