From 40c86b8fd373d257ff5cf98ec2e6ad53e1234627 Mon Sep 17 00:00:00 2001
From: Andrew Davison <andrew.davison@unic.cnrs-gif.fr>
Date: Mon, 27 Oct 2014 19:43:33 +0100
Subject: [PATCH] Fix error when testing PyNNNumpyIO (due to string dtypes of
 different length).

 Note: This patch has been modified by the Debian maintainer to apply to
       version 0.3.3

--- a/neo/io/pynnio.py
+++ b/neo/io/pynnio.py
@@ -13,6 +13,7 @@
 Authors: Andrew Davison, Pierre Yger
 """
 
+from itertools import chain
 import numpy
 import quantities as pq
 
@@ -20,6 +21,13 @@
 from neo.core import Segment, AnalogSignal, AnalogSignalArray, SpikeTrain
 from neo.io.tools import create_many_to_one_relationship
 
+try:
+    unicode
+    PY2 = True
+except NameError:
+    PY2 = False
+
+
 UNITS_MAP = {
     'spikes': pq.ms,
     'v': pq.mV,
@@ -199,7 +207,14 @@
         return data, metadata
 
     def _write_file_contents(self, data, metadata):
-        metadata_array = numpy.array(sorted(metadata.items()))
+        # we explicitly set the dtype to ensure roundtrips preserve file contents exactly
+        max_metadata_length = max(chain([len(k) for k in metadata.keys()],
+                                        [len(str(v)) for v in metadata.values()]))
+        if PY2:
+            dtype = "S%d" % max_metadata_length
+        else:
+            dtype = "U%d" % max_metadata_length
+        metadata_array = numpy.array(sorted(metadata.items()), dtype)
         numpy.savez(self.filename, data=data, metadata=metadata_array)
 
 
