Repository: numpy/numpy

Issue:
Junk in the representation of string fields of recarrays (Trac #483)
_Original ticket http://projects.scipy.org/numpy/ticket/483 on 2007-03-28 by @FrancescAlted, assigned to unknown._

The next exposes the problem:

```
In [27]:r=numpy.array([['abc']], dtype=[('var1', '|S20')])
In [28]:r['var1']
Out[28]:
array([['abc\xb7\x08\xc1+\x08\xc0>\xde\xb7@\xec\x95\xb7`\xec\x95\xb7']],
      dtype='|S20')
In [30]:r['var1'][0]
Out[30]:
array(['abc\xb7\x08\xc1+\x08\xc0>\xde\xb7@\xec\x95\xb7`\xec\x95\xb7'],
      dtype='|S20')
In [31]:r['var1'][0][0]
Out[31]:'abc\xb7\x08\xc1+\x08\xc0>\xde\xb7@\xec\x95\xb7`\xec\x95\xb7'
In [32]:str(r['var1'][0][0])
Out[32]:'abc\xb7\x08\xc1+\x08\xc0>\xde\xb7@\xec\x95\xb7`\xec\x95\xb7'
```

This seems to be a problem related only with string fields on recarrays, because I'm not able to reproduce it on plain string arrays:

```
In [34]:s2=numpy.array([['abc']], dtype='S20')
In [35]:s2
Out[35]:
array([['abc']],
      dtype='|S20')
In [36]:s2[0][0]
Out[36]:'abc'
In [37]:str(s2[0][0])
Out[37]:'abc'
```

Using 1.0.2.dev3546 here.

Thanks!



Discussion:

Resolving patch:
diff --git a/numpy/core/code_generators/numpy_api.py b/numpy/core/code_generators/numpy_api.py
--- a/numpy/core/code_generators/numpy_api.py
+++ b/numpy/core/code_generators/numpy_api.py
@@ -311,6 +311,7 @@
     'PyArray_EinsteinSum':                  274,
     'PyArray_FillWithZero':                 275,
     'PyArray_NewLikeArray':                 276,
+    'PyArray_GetArrayParamsFromObject':     277,
 }
 
 ufunc_types_api = {


Test patch:
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -723,15 +723,15 @@ def test_object_array_refcounting(self, level=rlevel):
         arr = np.zeros(5, dtype=np.object_)
 
         arr[:] = a
-        assert cnt(a) == cnt0_a + 5
+        assert_equal(cnt(a), cnt0_a + 5)
 
         arr[:] = b
-        assert cnt(a) == cnt0_a
-        assert cnt(b) == cnt0_b + 5
+        assert_equal(cnt(a), cnt0_a)
+        assert_equal(cnt(b), cnt0_b + 5)
 
         arr[:2] = c
-        assert cnt(b) == cnt0_b + 3
-        assert cnt(c) == cnt0_c + 2
+        assert_equal(cnt(b), cnt0_b + 3)
+        assert_equal(cnt(c), cnt0_c + 2)
 
         del arr
 
@@ -1131,14 +1131,18 @@ def test_fromiter_bytes(self):
     def test_array_from_sequence_scalar_array(self):
         """Ticket #1078: segfaults when creating an array with a sequence of 0d
         arrays."""
-        a = np.ones(2)
-        b = np.array(3)
-        assert_raises(ValueError, lambda: np.array((a, b)))
+        a = np.array((np.ones(2), np.array(2)))
+        assert_equal(a.shape, (2,))
+        assert_equal(a.dtype, np.dtype(object))
+        assert_equal(a[0], np.ones(2))
+        assert_equal(a[1], np.array(2))
+
+        a = np.array(((1,), np.array(1)))
+        assert_equal(a.shape, (2,))
+        assert_equal(a.dtype, np.dtype(object))
+        assert_equal(a[0], (1,))
+        assert_equal(a[1], np.array(1))
 
-        t = ((1,), np.array(1))
-        assert_raises(ValueError, lambda: np.array(t))
-
-    @dec.knownfailureif(True, "This is a corner case, see ticket #1081.")
     def test_array_from_sequence_scalar_array2(self):
         """Ticket #1081: weird array with strange input..."""
         t = np.array([np.array([]), np.array(0, object)])
@@ -1509,5 +1513,25 @@ def test_zerosize_accumulate(self):
         x = np.array([[42, 0]], dtype=np.uint32)
         assert_equal(np.add.accumulate(x[:-1,0]), [])
 
+    def test_objectarray_setfield(self):
+        # Setfield directly manipulates the raw array data,
+        # so is invalid for object arrays.
+        x = np.array([1,2,3], dtype=object)
+        assert_raises(RuntimeError, x.setfield, 4, np.int32, 0)
+
+    def test_setting_rank0_string(self):
+        "Ticket #1736"
+        a = np.zeros((), dtype="S10")
+        a[()] = b"hello1"
+        assert_equal(a, np.array(b"hello1"))
+        a[()] = np.array(b"hello2")
+        assert_equal(a, np.array(b"hello2"))
+
+        a = np.zeros((), dtype='f4')
+        a[()] = 3
+        assert_equal(a, np.array(3))
+        a[()] = np.array(4)
+        assert_equal(a, np.array(4))
+
 if __name__ == "__main__":
     run_module_suite()
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -1050,7 +1050,7 @@ def test_inconsistent_dtype(self):
         converters = {4: lambda x:"(%s)" % x}
         kwargs = dict(delimiter=",", converters=converters,
                       dtype=[(_, int) for _ in 'abcde'],)
-        assert_raises(TypeError, np.genfromtxt, mdata, **kwargs)
+        assert_raises(ValueError, np.genfromtxt, mdata, **kwargs)
 
 
     def test_default_field_format(self):