Beispiel #1
0
 def test_blocksize(self):
     """ Core driver supports variable block size """
     fname = self.mktemp()
     fid = File(fname, 'w', driver='core', block_size=1024,
                backing_store=False)
     self.assert_(fid)
     fid.close()
Beispiel #2
0
class BaseGroup(TestCase):
    def setUp(self):
        self.f = File(self.mktemp(), "w")

    def tearDown(self):
        if self.f:
            self.f.close()
Beispiel #3
0
class TestVisit(TestCase):

    """
        Feature: The .visit and .visititems methods allow iterative access to
        group and subgroup members
    """

    def setUp(self):
        self.f = File(self.mktemp(), "w")
        self.groups = ["grp1", "grp1/sg1", "grp1/sg2", "grp2", "grp2/sg1", "grp2/sg1/ssg1"]
        for x in self.groups:
            self.f.create_group(x)

    def tearDown(self):
        self.f.close()

    def test_visit(self):
        """ All subgroups are visited """
        l = []
        self.f.visit(l.append)
        self.assertSameElements(l, self.groups)

    def test_visititems(self):
        """ All subgroups and contents are visited """
        l = []
        comp = [(x, self.f[x]) for x in self.groups]
        self.f.visititems(lambda x, y: l.append((x, y)))
        self.assertSameElements(comp, l)

    def test_bailout(self):
        """ Returning a non-None value immediately aborts iteration """
        x = self.f.visit(lambda x: x)
        self.assertEqual(x, self.groups[0])
        x = self.f.visititems(lambda x, y: (x, y))
        self.assertEqual(x, (self.groups[0], self.f[self.groups[0]]))
class BaseDataset(TestCase):
    def setUp(self):
        self.f = File(self.mktemp(), 'w')

    def tearDown(self):
        if self.f:
            self.f.close()
class BaseDataset(TestCase):

    """
    data is a 3-dimensional dataset with dimensions [z, y, x]

    The z dimension is labeled. It does not have any attached scales.
    The y dimension is not labeled. It has one attached scale.
    The x dimension is labeled. It has two attached scales.

    data2 is a 3-dimensional dataset with no associated dimension scales.
    """

    def setUp(self):
        self.f = File(self.mktemp(), 'w')
        self.f['data'] = np.ones((4, 3, 2), 'f')
        self.f['data2'] = np.ones((4, 3, 2), 'f')
        self.f['x1'] = np.ones((2), 'f')
        h5py.h5ds.set_scale(self.f['x1'].id)
        h5py.h5ds.attach_scale(self.f['data'].id, self.f['x1'].id, 2)
        self.f['x2'] = np.ones((2), 'f')
        h5py.h5ds.set_scale(self.f['x2'].id, b'x2 name')
        h5py.h5ds.attach_scale(self.f['data'].id, self.f['x2'].id, 2)
        self.f['y1'] = np.ones((3), 'f')
        h5py.h5ds.set_scale(self.f['y1'].id, b'y1 name')
        h5py.h5ds.attach_scale(self.f['data'].id, self.f['y1'].id, 1)
        self.f['z1'] = np.ones((4), 'f')

        h5py.h5ds.set_label(self.f['data'].id, 0, b'z')
        h5py.h5ds.set_label(self.f['data'].id, 2, b'x')

    def tearDown(self):
        if self.f:
            self.f.close()
Beispiel #6
0
 def test_core(self):
     """ Core driver is supported (no backing store) """
     fname = self.mktemp()
     fid = File(fname, 'w', driver='core', backing_store=False)
     self.assert_(fid)
     self.assertEqual(fid.driver, 'core')
     fid.close()
     self.assertFalse(os.path.exists(fname))
Beispiel #7
0
 def test_mode(self):
     """ Retrieved File objects have a meaningful mode attribute """
     hfile = File(self.mktemp(),'w')
     try:
         grp = hfile.create_group('foo')
         self.assertEqual(grp.file.mode, hfile.mode)
     finally:
         hfile.close()
Beispiel #8
0
 def test_create_exclusive(self):
     """ Mode 'w-' opens file in exclusive mode """
     fname = self.mktemp()
     fid = File(fname, 'w-')
     self.assert_(fid)
     fid.close()
     with self.assertRaises(IOError):
         File(fname, 'w-')
Beispiel #9
0
 def test_iter_zero(self):
     """ Iteration works properly for the case with no group members """
     hfile = File(self.mktemp(), 'w')
     try:
         lst = [x for x in hfile]
         self.assertEqual(lst, [])
     finally:
         hfile.close()
Beispiel #10
0
 def test_property(self):
     """ File object can be retrieved from subgroup """
     fname = self.mktemp()
     hfile = File(fname, 'w')
     try:
         hfile2 = hfile['/'].file
         self.assertEqual(hfile, hfile2)
     finally:
         hfile.close()
Beispiel #11
0
 def test_filename(self):
     """ .filename behaves properly for string data """
     fname = self.mktemp()
     fid = File(fname, 'w')
     try:
         self.assertEqual(fid.filename, fname)
         self.assertIsInstance(fid.filename, unicode)
     finally:
         fid.close()
Beispiel #12
0
 def test_unicode(self):
     """ Unicode filenames can be used, and retrieved properly via .filename
     """
     fname = self.mktemp(prefix=six.unichr(0x201A))
     fid = File(fname, "w")
     try:
         self.assertEqual(fid.filename, fname)
         self.assertIsInstance(fid.filename, six.text_type)
     finally:
         fid.close()
Beispiel #13
0
 def test_unicode(self):
     """ Unicode filenames can be used, and retrieved properly via .filename
     """
     fname = self.mktemp(prefix = u'\u201a')
     fid = File(fname, 'w')
     try:
         self.assertEqual(fid.filename, fname)
         self.assertIsInstance(fid.filename, unicode)
     finally:
         fid.close()
Beispiel #14
0
    def test_mode_external(self):
        """ Mode property works for files opened via external links

        Issue 190.
        """
        fname1 = self.mktemp()
        fname2 = self.mktemp()

        f1 = File(fname1,'w')
        f1.close()

        f2 = File(fname2,'w')
        try:
            f2['External'] = h5py.ExternalLink(fname1, '/')
            f3 = f2['External'].file
            self.assertEqual(f3.mode, 'r+')
        finally:
            f2.close()
            f3.close()

        f2 = File(fname2,'r')
        try:
            f3 = f2['External'].file
            self.assertEqual(f3.mode, 'r')
        finally:
            f2.close()
            f3.close()
Beispiel #15
0
    def test_mode_external(self):
        """ Mode property works for files opened via external links

        Issue 190.
        """
        fname1 = self.mktemp()
        fname2 = self.mktemp()

        f1 = File(fname1, "w")
        f1.close()

        f2 = File(fname2, "w")
        try:
            f2["External"] = h5py.ExternalLink(fname1, "/")
            f3 = f2["External"].file
            self.assertEqual(f3.mode, "r+")
        finally:
            f2.close()
            f3.close()

        f2 = File(fname2, "r")
        try:
            f3 = f2["External"].file
            self.assertEqual(f3.mode, "r")
        finally:
            f2.close()
            f3.close()
Beispiel #16
0
 def test_readwrite(self):
     """ Mode 'r+' opens existing file in readwrite mode """
     fname = self.mktemp()
     fid = File(fname, "w")
     fid.create_group("foo")
     fid.close()
     fid = File(fname, "r+")
     self.assert_("foo" in fid)
     fid.create_group("bar")
     self.assert_("bar" in fid)
     fid.close()
Beispiel #17
0
 def test_close(self):
     """ All retrieved File objects are closed at the same time """
     fname = self.mktemp()
     hfile = File(fname, 'w')
     grp = hfile.create_group('foo')
     hfile2 = grp.file
     hfile3 = hfile['/'].file
     hfile2.close()
     self.assertFalse(hfile)
     self.assertFalse(hfile2)
     self.assertFalse(hfile3)
Beispiel #18
0
 def test_readwrite(self):
     """ Mode 'r+' opens existing file in readwrite mode """
     fname = self.mktemp()
     fid = File(fname, 'w')
     fid.create_group('foo')
     fid.close()
     fid = File(fname, 'r+')
     self.assert_('foo' in fid)
     fid.create_group('bar')
     self.assert_('bar' in fid)
     fid.close()
Beispiel #19
0
    def test_write_only(self):
        """ User block only allowed for write """
        name = self.mktemp()
        f = File(name, 'w')
        f.close()

        with self.assertRaises(ValueError):
            f = h5py.File(name, 'r', userblock_size=512)

        with self.assertRaises(ValueError):
            f = h5py.File(name, 'r+', userblock_size=512)
Beispiel #20
0
 def test_readonly(self):
     """ Core driver can be used to open existing files """
     fname = self.mktemp()
     fid = File(fname, 'w')
     fid.create_group('foo')
     fid.close()
     fid = File(fname, 'r', driver='core')
     self.assert_(fid)
     self.assert_('foo' in fid)
     with self.assertRaises(ValueError):
         fid.create_group('bar')
     fid.close()
Beispiel #21
0
 def test_readonly(self):
     """ Core driver can be used to open existing files """
     fname = self.mktemp()
     fid = File(fname, "w")
     fid.create_group("foo")
     fid.close()
     fid = File(fname, "r", driver="core")
     self.assert_(fid)
     self.assert_("foo" in fid)
     with self.assertRaises(ValueError):
         fid.create_group("bar")
     fid.close()
Beispiel #22
0
    def test_issue_212(self):
        """ Issue 212
    
        Fails with:

        AttributeError: 'SharedConfig' object has no attribute 'lapl'
        """
        def closer(x):
            def w():
                try:
                    if x:
                        x.close()
                except IOError:
                    pass
            return w
        orig_name = self.mktemp()
        new_name = self.mktemp()
        f = File(orig_name, 'w')
        self.addCleanup(closer(f))
        f.create_group('a')
        f.close()

        g = File(new_name, 'w')
        self.addCleanup(closer(g))
        g['link'] = ExternalLink(orig_name, '/')  # note root group
        g.close()

        h = File(new_name, 'r')
        self.addCleanup(closer(h))
        self.assertIsInstance(h['link']['a'], Group)
Beispiel #23
0
class BaseMapping(BaseGroup):

    """
        Base class for mapping tests
    """
    def setUp(self):
        self.f = File(self.mktemp(), 'w')
        self.groups = ('a','b','c','d')
        for x in self.groups:
            self.f.create_group(x)

    def tearDown(self):
        if self.f:
            self.f.close()
Beispiel #24
0
class BaseMapping(BaseGroup):

    """
        Base class for mapping tests
    """

    def setUp(self):
        self.f = File(self.mktemp(), "w")
        self.groups = ("a", "b", "c", "d")
        for x in self.groups:
            self.f.create_group(x)

    def tearDown(self):
        if self.f:
            self.f.close()
Beispiel #25
0
 def setUp(self):
     self.f = File(self.mktemp(), 'w')
     self.groups = [
         'grp1', 'grp1/sg1', 'grp1/sg2', 'grp2', 'grp2/sg1', 'grp2/sg1/ssg1'
         ]
     for x in self.groups:
         self.f.create_group(x)
Beispiel #26
0
class BaseMapping(BaseGroup):

    """
        Base class for mapping tests
    """
    def setUp(self):
        self.f = File(self.mktemp(), 'w')
        self.groups = ('a','b','c','d')
        for x in self.groups:
            self.f.create_group(x)
        self.f['x'] = h5py.SoftLink('/mongoose')
        self.groups = self.groups + ('x',)

    def tearDown(self):
        if self.f:
            self.f.close()
 def setUp(self):
     self.f = File(self.mktemp(), "w")
     self.groups = ("a", "b", "c", "d")
     for x in self.groups:
         self.f.create_group(x)
     self.f["x"] = h5py.SoftLink("/mongoose")
     self.groups = self.groups + ("x",)
Beispiel #28
0
 def setUp(self):
     self.f = File(self.mktemp(), 'w')
     self.groups = ('a','b','c','d')
     for x in self.groups:
         self.f.create_group(x)
     self.f['x'] = h5py.SoftLink('/mongoose')
     self.groups = self.groups + ('x',)
Beispiel #29
0
 def test_backing(self):
     """ Core driver saves to file when backing store used """
     fname = self.mktemp()
     fid = File(fname, 'w', driver='core', backing_store=True)
     fid.create_group('foo')
     fid.close()
     fid = File(fname, 'r')
     self.assert_('foo' in fid)
     fid.close()
Beispiel #30
0
 def test_backing(self):
     """ Core driver saves to file when backing store used """
     fname = self.mktemp()
     fid = File(fname, "w", driver="core", backing_store=True)
     fid.create_group("foo")
     fid.close()
     fid = File(fname, "r")
     self.assert_("foo" in fid)
     fid.close()
Beispiel #31
0
 def test_closed_file(self):
     """ Trying to modify closed file raises ValueError """
     fid = File(self.mktemp(), 'w')
     fid.close()
     with self.assertRaises(ValueError):
         fid.create_group('foo')
Beispiel #32
0
 def test_flush(self):
     """ Flush via .flush method """
     fid = File(self.mktemp(), 'w')
     fid.flush()
     fid.close()
Beispiel #33
0
 def test_stdio(self):
     """ Stdio driver is supported on posix """
     fid = File(self.mktemp(), 'w', driver='stdio')
     self.assertTrue(fid)
     self.assertEqual(fid.driver, 'stdio')
     fid.close()
Beispiel #34
0
 def test_unicode_hdf5_python_consistent(self):
     """ Unicode filenames can be used, and seen correctly from python
     """
     fname = self.mktemp(prefix=six.unichr(0x201a))
     with File(fname, 'w') as f:
         self.assertTrue(os.path.exists(fname))
Beispiel #35
0
 def test_pathlib_accepted_file(self):
     """ Check that pathlib is accepted by h5py.File """
     with closed_tempfile() as f:
         path = pathlib.Path(f)
         with File(path) as f2:
             self.assertTrue(True)
Beispiel #36
0
 def test_default(self):
     """ Opening with no libver arg """
     f = File(self.mktemp(), 'w')
     self.assertEqual(f.libver, ('earliest', 'latest'))
     f.close()
Beispiel #37
0
 def test_context_manager(self):
     """ File objects can be used in with statements """
     with File(self.mktemp(), 'w') as fid:
         self.assertTrue(fid)
     self.assertTrue(not fid)
Beispiel #38
0
 def test_multiple(self):
     """ Opening with two libver args """
     f = File(self.mktemp(), 'w', libver=('earliest', 'latest'))
     self.assertEqual(f.libver, ('earliest', 'latest'))
     f.close()
Beispiel #39
0
 def test_readonly(self):
     """ Core driver can be used to open existing files """
     fname = self.mktemp()
     fid = File(fname, 'w')
     fid.create_group('foo')
     fid.close()
     fid = File(fname, 'r', driver='core')
     self.assert_(fid)
     self.assert_('foo' in fid)
     with self.assertRaises(ValueError):
         fid.create_group('bar')
     fid.close()
Beispiel #40
0
 def test_append(self):
     """ Mode 'a' opens file in append/readwrite mode, creating if necessary """
     fname = self.mktemp()
     fid = File(fname, 'a')
     try:
         self.assert_(fid)
         fid.create_group('foo')
         self.assert_('foo' in fid)
     finally:
         fid.close()
     fid = File(fname, 'a')
     try:
         self.assert_('foo' in fid)
         fid.create_group('bar')
         self.assert_('bar' in fid)
     finally:
         fid.close()
Beispiel #41
0
 def test_invalid_mode(self):
     """ Invalid modes raise ValueError """
     with self.assertRaises(ValueError):
         File(self.mktemp(), 'mongoose')
Beispiel #42
0
class TestAdditionalMappingFuncs(BaseMapping):
    """
    Feature: Other dict methods (pop, pop_item, clear, update, setdefault) are
    available.
    """
    def setUp(self):
        self.f = File(self.mktemp(), 'w')
        for x in ('/test/a', '/test/b', '/test/c', '/test/d'):
            self.f.create_group(x)
        self.group = self.f['test']

    def tearDown(self):
        if self.f:
            self.f.close()

    def test_pop_item(self):
        """.pop_item exists and removes item"""
        key, val = self.group.popitem()
        self.assertNotIn(key, self.group)

    def test_pop(self):
        """.pop exists and removes specified item"""
        self.group.pop('a')
        self.assertNotIn('a', self.group)

    def test_pop_default(self):
        """.pop falls back to default"""
        # e shouldn't exist as a group
        value = self.group.pop('e', None)
        self.assertEqual(value, None)

    def test_pop_raises(self):
        """.pop raises KeyError for non-existence"""
        # e shouldn't exist as a group
        with self.assertRaises(KeyError):
            key = self.group.pop('e')

    def test_clear(self):
        """.clear removes groups"""
        self.group.clear()
        self.assertEqual(len(self.group), 0)

    def test_update_dict(self):
        """.update works with dict"""
        new_items = {'e': np.array([42])}
        self.group.update(new_items)
        self.assertIn('e', self.group)

    def test_update_iter(self):
        """.update works with list"""
        new_items = [('e', np.array([42])), ('f', np.array([42]))]
        self.group.update(new_items)
        self.assertIn('e', self.group)

    def test_update_kwargs(self):
        """.update works with kwargs"""
        new_items = {'e': np.array([42])}
        self.group.update(**new_items)
        self.assertIn('e', self.group)

    def test_setdefault(self):
        """.setdefault gets group if it exists"""
        value = self.group.setdefault('a')
        self.assertEqual(value, self.group.get('a'))

    def test_setdefault_with_default(self):
        """.setdefault gets default if group doesn't exist"""
        # e shouldn't exist as a group
        # 42 used as groups should be strings
        value = self.group.setdefault('e', np.array([42]))
        self.assertEqual(value, 42)

    def test_setdefault_no_default(self):
        """
        .setdefault gets None if group doesn't exist, but as None isn't defined
        as data for a dataset, this should raise a TypeError.
        """
        # e shouldn't exist as a group
        with self.assertRaises(TypeError):
            self.group.setdefault('e')
Beispiel #43
0
 def test_close(self):
     """ Close file via .close method """
     fid = File(self.mktemp())
     self.assert_(fid)
     fid.close()
     self.assert_(not fid)
Beispiel #44
0
 def test_sec2(self):
     """ Sec2 driver is supported on posix """
     fid = File(self.mktemp(), 'w', driver='sec2')
     self.assert_(fid)
     self.assertEqual(fid.driver, 'sec2')
     fid.close()
Beispiel #45
0
 def setUp(self):
     self.f1 = File(self.mktemp(), 'w')
     self.f2 = File(self.mktemp(), 'w')
Beispiel #46
0
    def test_issue_212(self):
        """ Issue 212

        Fails with:

        AttributeError: 'SharedConfig' object has no attribute 'lapl'
        """
        def closer(x):
            def w():
                try:
                    if x:
                        x.close()
                except IOError:
                    pass

            return w

        orig_name = self.mktemp()
        new_name = self.mktemp()
        f = File(orig_name, 'w')
        self.addCleanup(closer(f))
        f.create_group('a')
        f.close()

        g = File(new_name, 'w')
        self.addCleanup(closer(g))
        g['link'] = ExternalLink(orig_name, '/')  # note root group
        g.close()

        h = File(new_name, 'r')
        self.addCleanup(closer(h))
        self.assertIsInstance(h['link']['a'], Group)
Beispiel #47
0
 def test_none(self):
     """ Omitting libver arg results in maximum compatibility """
     f = File(self.mktemp(), 'w')
     self.assertEqual(f.libver, ('earliest', 'latest'))
     f.close()
Beispiel #48
0
 def test_single(self):
     """ Opening with single libver arg """
     f = File(self.mktemp(), 'w', libver='latest')
     self.assertEqual(f.libver, ('latest', 'latest'))
     f.close()
Beispiel #49
0
class TestCopy(TestCase):
    def setUp(self):
        self.f1 = File(self.mktemp(), 'w')
        self.f2 = File(self.mktemp(), 'w')

    def tearDown(self):
        if self.f1:
            self.f1.close()
        if self.f2:
            self.f2.close()

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_path_to_path(self):
        foo = self.f1.create_group('foo')
        foo['bar'] = [1, 2, 3]

        self.f1.copy('foo', 'baz')
        baz = self.f1['baz']
        self.assertIsInstance(baz, Group)
        self.assertArrayEqual(baz['bar'], np.array([1, 2, 3]))

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_path_to_group(self):
        foo = self.f1.create_group('foo')
        foo['bar'] = [1, 2, 3]
        baz = self.f1.create_group('baz')

        self.f1.copy('foo', baz)
        baz = self.f1['baz']
        self.assertIsInstance(baz, Group)
        self.assertArrayEqual(baz['foo/bar'], np.array([1, 2, 3]))

        self.f1.copy('foo', self.f2['/'])
        self.assertIsInstance(self.f2['/foo'], Group)
        self.assertArrayEqual(self.f2['foo/bar'], np.array([1, 2, 3]))

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_group_to_path(self):

        foo = self.f1.create_group('foo')
        foo['bar'] = [1, 2, 3]

        self.f1.copy(foo, 'baz')
        baz = self.f1['baz']
        self.assertIsInstance(baz, Group)
        self.assertArrayEqual(baz['bar'], np.array([1, 2, 3]))

        self.f2.copy(foo, 'foo')
        self.assertIsInstance(self.f2['/foo'], Group)
        self.assertArrayEqual(self.f2['foo/bar'], np.array([1, 2, 3]))

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_group_to_group(self):

        foo = self.f1.create_group('foo')
        foo['bar'] = [1, 2, 3]
        baz = self.f1.create_group('baz')

        self.f1.copy(foo, baz)
        baz = self.f1['baz']
        self.assertIsInstance(baz, Group)
        self.assertArrayEqual(baz['foo/bar'], np.array([1, 2, 3]))

        self.f1.copy(foo, self.f2['/'])
        self.assertIsInstance(self.f2['/foo'], Group)
        self.assertArrayEqual(self.f2['foo/bar'], np.array([1, 2, 3]))

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_dataset(self):
        self.f1['foo'] = [1, 2, 3]
        foo = self.f1['foo']

        self.f1.copy(foo, 'bar')
        self.assertArrayEqual(self.f1['bar'], np.array([1, 2, 3]))

        self.f1.copy('foo', 'baz')
        self.assertArrayEqual(self.f1['baz'], np.array([1, 2, 3]))

        self.f1.copy('foo', self.f2)
        self.assertArrayEqual(self.f2['foo'], np.array([1, 2, 3]))

        self.f2.copy(self.f1['foo'], self.f2, 'bar')
        self.assertArrayEqual(self.f2['bar'], np.array([1, 2, 3]))

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_shallow(self):

        foo = self.f1.create_group('foo')
        bar = foo.create_group('bar')
        foo['qux'] = [1, 2, 3]
        bar['quux'] = [4, 5, 6]

        self.f1.copy(foo, 'baz', shallow=True)
        baz = self.f1['baz']
        self.assertIsInstance(baz, Group)
        self.assertIsInstance(baz['bar'], Group)
        self.assertEqual(len(baz['bar']), 0)
        self.assertArrayEqual(baz['qux'], np.array([1, 2, 3]))

        self.f2.copy(foo, 'foo', shallow=True)
        self.assertIsInstance(self.f2['/foo'], Group)
        self.assertIsInstance(self.f2['foo/bar'], Group)
        self.assertEqual(len(self.f2['foo/bar']), 0)
        self.assertArrayEqual(self.f2['foo/qux'], np.array([1, 2, 3]))

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_without_attributes(self):

        self.f1['foo'] = [1, 2, 3]
        foo = self.f1['foo']
        foo.attrs['bar'] = [4, 5, 6]

        self.f1.copy(foo, 'baz', without_attrs=True)
        self.assertArrayEqual(self.f1['baz'], np.array([1, 2, 3]))
        self.assert_('bar' not in self.f1['baz'].attrs)

        self.f2.copy(foo, 'baz', without_attrs=True)
        self.assertArrayEqual(self.f2['baz'], np.array([1, 2, 3]))
        self.assert_('bar' not in self.f2['baz'].attrs)

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_soft_links(self):

        self.f1['bar'] = [1, 2, 3]
        foo = self.f1.create_group('foo')
        foo['baz'] = SoftLink('/bar')

        self.f1.copy(foo, 'qux', expand_soft=True)
        self.f2.copy(foo, 'foo', expand_soft=True)
        del self.f1['bar']

        self.assertIsInstance(self.f1['qux'], Group)
        self.assertArrayEqual(self.f1['qux/baz'], np.array([1, 2, 3]))

        self.assertIsInstance(self.f2['/foo'], Group)
        self.assertArrayEqual(self.f2['foo/baz'], np.array([1, 2, 3]))

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_external_links(self):

        filename = self.f1.filename
        self.f1['foo'] = [1, 2, 3]
        self.f2['bar'] = ExternalLink(filename, 'foo')
        self.f1.close()
        self.f1 = None

        self.assertArrayEqual(self.f2['bar'], np.array([1, 2, 3]))

        self.f2.copy('bar', 'baz', expand_external=True)
        os.unlink(filename)
        self.assertArrayEqual(self.f2['baz'], np.array([1, 2, 3]))

    @ut.skipIf(h5py.version.hdf5_version_tuple < (1, 8, 9),
               "Bug in HDF5<1.8.8 prevents copying open dataset")
    def test_copy_refs(self):

        self.f1['foo'] = [1, 2, 3]
        self.f1['bar'] = [4, 5, 6]
        foo = self.f1['foo']
        bar = self.f1['bar']
        foo.attrs['bar'] = bar.ref

        self.f1.copy(foo, 'baz', expand_refs=True)
        self.assertArrayEqual(self.f1['baz'], np.array([1, 2, 3]))
        baz_bar = self.f1['baz'].attrs['bar']
        self.assertArrayEqual(self.f1[baz_bar], np.array([4, 5, 6]))
        # The reference points to a copy of bar, not to bar itself.
        self.assertNotEqual(self.f1[baz_bar].name, bar.name)

        self.f1.copy('foo', self.f2, 'baz', expand_refs=True)
        self.assertArrayEqual(self.f2['baz'], np.array([1, 2, 3]))
        baz_bar = self.f2['baz'].attrs['bar']
        self.assertArrayEqual(self.f2[baz_bar], np.array([4, 5, 6]))

        self.f1.copy('/', self.f2, 'root', expand_refs=True)
        self.assertArrayEqual(self.f2['root/foo'], np.array([1, 2, 3]))
        self.assertArrayEqual(self.f2['root/bar'], np.array([4, 5, 6]))
        foo_bar = self.f2['root/foo'].attrs['bar']
        self.assertArrayEqual(self.f2[foo_bar], np.array([4, 5, 6]))
        # There's only one copy of bar, which the reference points to.
        self.assertEqual(self.f2[foo_bar], self.f2['root/bar'])
Beispiel #50
0
 def test_readwrite(self):
     """ Mode 'r+' opens existing file in readwrite mode """
     fname = self.mktemp()
     fid = File(fname, 'w')
     fid.create_group('foo')
     fid.close()
     fid = File(fname, 'r+')
     self.assert_('foo' in fid)
     fid.create_group('bar')
     self.assert_('bar' in fid)
     fid.close()
Beispiel #51
0
    def test_create_blocksize(self):
        """ User blocks created with w, w-, x and properties work correctly """
        f = File(self.mktemp(),'w-', userblock_size=512)
        try:
            self.assertEqual(f.userblock_size, 512)
        finally:
            f.close()

        f = File(self.mktemp(),'x', userblock_size=512)
        try:
            self.assertEqual(f.userblock_size, 512)
        finally:
            f.close()

        f = File(self.mktemp(),'w', userblock_size=512)
        try:
            self.assertEqual(f.userblock_size, 512)
        finally:
            f.close()
Beispiel #52
0
class TestExternalLinks(TestCase):
    """
        Feature: Create and manage external links
    """
    def setUp(self):
        self.f = File(self.mktemp(), 'w')
        self.ename = self.mktemp()
        self.ef = File(self.ename, 'w')
        self.ef.create_group('external')
        self.ef.close()

    def tearDown(self):
        if self.f:
            self.f.close()
        if self.ef:
            self.ef.close()

    def test_epath(self):
        """ External link paths attributes """
        el = ExternalLink('foo.hdf5', '/foo')
        self.assertEqual(el.filename, 'foo.hdf5')
        self.assertEqual(el.path, '/foo')

    def test_erepr(self):
        """ External link repr """
        el = ExternalLink('foo.hdf5', '/foo')
        self.assertIsInstance(repr(el), six.string_types)

    def test_create(self):
        """ Creating external links """
        self.f['ext'] = ExternalLink(self.ename, '/external')
        grp = self.f['ext']
        self.ef = grp.file
        self.assertNotEqual(self.ef, self.f)
        self.assertEqual(grp.name, '/external')

    def test_exc(self):
        """ KeyError raised when attempting to open broken link """
        self.f['ext'] = ExternalLink(self.ename, '/missing')
        with self.assertRaises(KeyError):
            self.f['ext']

    # I would prefer IOError but there's no way to fix this as the exception
    # class is determined by HDF5.
    def test_exc_missingfile(self):
        """ KeyError raised when attempting to open missing file """
        self.f['ext'] = ExternalLink('mongoose.hdf5', '/foo')
        with self.assertRaises(KeyError):
            self.f['ext']

    def test_close_file(self):
        """ Files opened by accessing external links can be closed

        Issue 189.
        """
        self.f['ext'] = ExternalLink(self.ename, '/')
        grp = self.f['ext']
        f2 = grp.file
        f2.close()
        self.assertFalse(f2)

    @ut.skipIf(NO_FS_UNICODE, "No unicode filename support")
    def test_unicode_encode(self):
        """
        Check that external links encode unicode filenames properly
        Testing issue #732
        """
        ext_filename = os.path.join(mkdtemp(), u"α.hdf5")
        with File(ext_filename, "w") as ext_file:
            ext_file.create_group('external')
        self.f['ext'] = ExternalLink(ext_filename, '/external')

    @ut.skipIf(NO_FS_UNICODE, "No unicode filename support")
    def test_unicode_decode(self):
        """
        Check that external links decode unicode filenames properly
        Testing issue #732
        """
        ext_filename = os.path.join(mkdtemp(), u"α.hdf5")
        with File(ext_filename, "w") as ext_file:
            ext_file.create_group('external')
            ext_file["external"].attrs["ext_attr"] = "test"
        self.f['ext'] = ExternalLink(ext_filename, '/external')
        self.assertEqual(self.f["ext"].attrs["ext_attr"], "test")
Beispiel #53
0
 def setUp(self):
     self.f = File(self.mktemp(), 'w')
     self.arr = np.arange(10)
     self.dset = self.f.create_dataset('x', data=self.arr)
Beispiel #54
0
 def test_repr(self):
     """ __repr__ behaves itself when files are open and closed """
     fid = File(self.mktemp())
     self.assertIsInstance(repr(fid), basestring)
     fid.close()
     self.assertIsInstance(repr(fid), basestring)
Beispiel #55
0
 def setUp(self):
     self.f = File(self.mktemp(), 'w')
     self.ename = self.mktemp()
     self.ef = File(self.ename, 'w')
     self.ef.create_group('external')
     self.ef.close()
 def setUp(self):
     self.f = File(self.mktemp(), 'w')
Beispiel #57
0
 def test_fid(self):
     """ File objects provide a .fid attribute aliased to the file ID """
     with File(self.mktemp(), 'w') as hfile:
         self.assertIs(hfile.fid, hfile.id)
Beispiel #58
0
    def test_mode_external(self):
        """ Mode property works for files opened via external links

        Issue 190.
        """
        fname1 = self.mktemp()
        fname2 = self.mktemp()

        f1 = File(fname1, 'w')
        f1.close()

        f2 = File(fname2, 'w')
        try:
            f2['External'] = h5py.ExternalLink(fname1, '/')
            f3 = f2['External'].file
            self.assertEqual(f3.mode, 'r+')
        finally:
            f2.close()
            f3.close()

        f2 = File(fname2, 'r')
        try:
            f3 = f2['External'].file
            self.assertEqual(f3.mode, 'r')
        finally:
            f2.close()
            f3.close()
Beispiel #59
0
 def test_pathlib_accepted_file(self):
     """ Check that pathlib is accepted by h5py.File """
     with tempfile.NamedTemporaryFile() as f:
         path = pathlib.Path(f.name)
         with File(path) as f2:
             self.assertTrue(True)
Beispiel #60
0
 def setUp(self):
     self.f = File(self.mktemp(), 'w')
     for x in ('/test/a', '/test/b', '/test/c', '/test/d'):
         self.f.create_group(x)
     self.group = self.f['test']