@@ -13,25 +13,34 @@ def test_csv2rec_closefile(self):
13
13
self .failIf ( fh .closed )
14
14
15
15
def test_csv2rec_roundtrip (self ):
16
- # Make sure double-precision floats pass through.
16
+
17
+ # Make sure double-precision floats and strings pass through a
18
+ # roundtrip unaltered.
17
19
18
20
# A bug in numpy (fixed in r4602) meant that numpy scalars
19
21
# lost precision when passing through repr(). csv2rec was
20
22
# affected by this. This test will only pass on numpy >=
21
23
# 1.0.5.
22
- ra = numpy .rec .array ([(123 , 1197346475.0137341 ), (456 , 123.456 )],
23
- dtype = [('a' , '<i8' ), ('b' , '<f8' )])
24
- rec2csv_closes_files = True
25
- if rec2csv_closes_files :
26
- fh = 'mlab_unit_tmp.csv'
27
- else :
28
- fh = StringIO .StringIO ()
24
+ ra = numpy .rec .array ([(123 , 1197346475.0137341 , 'a,bc' ),
25
+ (456 , 123.456 , 'd\' ef' ),
26
+ (789 , 0.000000001 , 'ghi' ),
27
+ ],
28
+ dtype = [('a' , '<i8' ), ('b' , '<f8' ), ('c' , '|S3' )])
29
+ fh = StringIO .StringIO ()
29
30
mlab .rec2csv ( ra , fh )
30
- if not rec2csv_closes_files :
31
+ fh .seek (0 )
32
+ if 0 :
33
+ print 'CSV contents:' ,'-' * 40
34
+ print fh .read ()
35
+ print '-' * 40
31
36
fh .seek (0 )
32
37
ra2 = mlab .csv2rec (fh )
38
+ fh .close ()
33
39
for name in ra .dtype .names :
34
- #print name, repr(ra[name]), repr(ra2[name])
40
+ if 0 :
41
+ print name , repr (ra [name ]), repr (ra2 [name ])
42
+ dt = ra .dtype [name ]
43
+ print 'repr(dt.type)' ,repr (dt .type )
35
44
self .failUnless ( numpy .all (ra [name ] == ra2 [name ]) ) # should not fail with numpy 1.0.5
36
45
37
46
if __name__ == '__main__' :
0 commit comments