[Numpy-svn] r5319 - trunk/numpy/lib

numpy-svn at scipy.org numpy-svn at scipy.org
Sat Jun 28 13:05:40 EDT 2008


Author: charris
Date: 2008-06-28 12:05:37 -0500 (Sat, 28 Jun 2008)
New Revision: 5319

Modified:
   trunk/numpy/lib/format.py
Log:
Fix ticket #828 by explicitly sorting keys instead of relying on pprint.
Thanks to Neil Muller for the analysis and patch.


Modified: trunk/numpy/lib/format.py
===================================================================
--- trunk/numpy/lib/format.py	2008-06-27 05:26:25 UTC (rev 5318)
+++ trunk/numpy/lib/format.py	2008-06-28 17:05:37 UTC (rev 5319)
@@ -42,10 +42,9 @@
     "shape" : tuple of int
         The shape of the array.
 
-For repeatability and readability, this dictionary is formatted using
-pprint.pformat() so the keys are in alphabetic order. This is for convenience
-only. A writer SHOULD implement this if possible. A reader MUST NOT depend on
-this.
+For repeatability and readability, the dictionary keys are sorted in alphabetic
+order. This is for convenience only. A writer SHOULD implement this if possible.
+A reader MUST NOT depend on this.
 
 Following the header comes the array data. If the dtype contains Python objects
 (i.e. dtype.hasobject is True), then the data is a Python pickle of the array.
@@ -56,7 +55,6 @@
 """
 
 import cPickle
-import pprint
 import struct
 
 import numpy
@@ -102,9 +100,11 @@
     """
     magic_str = fp.read(MAGIC_LEN)
     if len(magic_str) != MAGIC_LEN:
-        raise ValueError("could not read %d characters for the magic string; got %r" % (MAGIC_LEN, magic_str))
+        msg = "could not read %d characters for the magic string; got %r"
+        raise ValueError(msg % (MAGIC_LEN, magic_str))
     if magic_str[:-2] != MAGIC_PREFIX:
-        raise ValueError("the magic string is not correct; expected %r, got %r" % (MAGIC_PREFIX, magic_str[:-2]))
+        msg = "the magic string is not correct; expected %r, got %r"
+        raise ValueError(msg % (MAGIC_PREFIX, magic_str[:-2]))
     major, minor = map(ord, magic_str[-2:])
     return major, minor
 
@@ -164,7 +164,11 @@
         This has the appropriate entries for writing its string representation
         to the header of the file.
     """
-    header = pprint.pformat(d)
+    header = "{"
+    for key, value in sorted(d.items()):
+        # Need to use repr here, since we eval these when reading
+        header += "'%s': %s, " % (key, repr(value))
+    header += "}"
     # Pad the header with spaces and a final newline such that the magic string,
     # the header-length short and the header are aligned on a 16-byte boundary.
     # Hopefully, some system, possibly memory-mapping, can take advantage of




More information about the Numpy-svn mailing list