[h5py] 303/455: Fix nested compound literal assignment

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:44 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag 1.3.0
in repository h5py.

commit 294e905f1731e9419d27675565805ef138c4458b
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Thu Aug 13 03:38:51 2009 +0000

    Fix nested compound literal assignment
---
 h5py/highlevel.py          | 10 +++++-----
 h5py/tests/test_dataset.py | 22 +++++++++++++++++++++-
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/h5py/highlevel.py b/h5py/highlevel.py
index 57d6451..24aa876 100644
--- a/h5py/highlevel.py
+++ b/h5py/highlevel.py
@@ -1012,13 +1012,13 @@ class Dataset(HLObject):
             if len(names) != 0:
                 raise TypeError("Field name selections are not allowed for write.")
 
-            val2 = numpy.asarray(val, order='C')
-
-            # Special fudge factor for weirdness with scalar compound literals
-            if self.dtype.kind == 'V' and val2.dtype.kind != 'V':
+            # Generally we try to avoid converting the arrays on the Python
+            # side.  However, for compound literals this is unavoidable.
+            if self.dtype.kind == 'V' and \
+            (not isinstance(val, numpy.ndarray) or val.dtype.kind != 'V'):
                 val = numpy.asarray(val, dtype=self.dtype, order='C')
             else:
-                val = val2
+                val = numpy.asarray(val, order='C')
 
             # Check for array dtype compatibility and convert
             if self.dtype.subdtype is not None:
diff --git a/h5py/tests/test_dataset.py b/h5py/tests/test_dataset.py
index 913a834..1e2fc38 100644
--- a/h5py/tests/test_dataset.py
+++ b/h5py/tests/test_dataset.py
@@ -48,4 +48,24 @@ class TestDataset(TestCasePlus):
                 assert np.all(dset[...] == data), msg
 
                 
-                
+    def test_literal(self):
+        # Literal assignment for compound types
+
+        dtypes = [ [('a','i'), ('b','f')],
+                   [('a','i'), ('b', [('c','i2'),('d','f')] ) ] ]
+
+        values = [ (42, 39.5),
+                   (42, (356, 34.0)) ]
+
+        for val, dt in zip(values, dtypes):
+            ds = self.f.create_dataset('ds', (1,), dt)
+            arr = np.ndarray((1,), dtype=dt)
+            ds[0] = val
+            arr[0] = val
+            assert ds[0] == np.asscalar(arr[0]), "%r: %r" % (ds[0], arr)
+            del self.f['ds']
+        
+
+
+
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/h5py.git



More information about the debian-science-commits mailing list