[Numpy-svn] r5320 - branches/1.1.x/numpy/lib

numpy-svn@scip... numpy-svn@scip...
Sat Jun 28 12:51:58 CDT 2008

Author: charris
Date: 2008-06-28 12:51:56 -0500 (Sat, 28 Jun 2008)
New Revision: 5320

Backport fix for ticket #828.

Modified: branches/1.1.x/numpy/lib/format.py
--- branches/1.1.x/numpy/lib/format.py	2008-06-28 17:05:37 UTC (rev 5319)
+++ branches/1.1.x/numpy/lib/format.py	2008-06-28 17:51:56 UTC (rev 5320)
@@ -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
+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