Commits

Armin Rigo committed bec0477

We need to take all **kwds and stick them into the hash too.

  • Participants
  • Parent commits d3a941a

Comments (0)

Files changed (3)

cffi/ffiplatform.py

                 return os.path.join(*names)
         except OSError:
             pass
+
+# ____________________________________________________________
+
+try:
+    int_or_long = (int, long)
+except NameError:
+    int_or_long = int      # Python 3
+
+def _flatten(x, f):
+    if isinstance(x, str):
+        f.write('%ds%s' % (len(x), x))
+    elif isinstance(x, dict):
+        keys = sorted(x.keys())
+        f.write('%dd' % len(keys))
+        for key in keys:
+            _flatten(key, f)
+            _flatten(x[key], f)
+    elif isinstance(x, (list, tuple)):
+        f.write('%dl' % len(x))
+        for value in x:
+            _flatten(value, f)
+    elif isinstance(x, int_or_long):
+        f.write('%di' % (x,))
+    else:
+        raise TypeError(
+            "the keywords to verify() contains unsupported object %r" % (x,))
+
+def flatten(x):
+    import cStringIO
+    f = cStringIO.StringIO()
+    _flatten(x, f)
+    return f.getvalue()
                  tag='', force_generic_engine=False, **kwds):
         self.ffi = ffi
         self.preamble = preamble
+        flattened_kwds = ffiplatform.flatten(kwds)
         vengine_class = _locate_engine_class(ffi, force_generic_engine)
         self._vengine = vengine_class(self)
         self._vengine.patch_extension_kwds(kwds)
         self.kwds = kwds
         #
-        key = '\x00'.join(['1', sys.version[:3], __version__, preamble] +
+        key = '\x00'.join([sys.version[:3], __version__, preamble,
+                           flattened_kwds] +
                           ffi._cdefsources)
         if sys.version_info >= (3,):
             key = key.encode('utf-8')

testing/test_platform.py

     assert maybe_relative_path(p) == 'baz.py'
     foobaz = os.path.join('foo', 'baz.py')
     assert maybe_relative_path(os.path.abspath(foobaz)) == foobaz
+
+def test_flatten():
+    assert flatten("foo") == "3sfoo"
+    assert flatten(-10000000000000000000000000000) == \
+           "-10000000000000000000000000000i"
+    assert flatten([4, 5]) == "2l4i5i"
+    assert flatten({4: 5}) == "1d4i5i"
+    assert flatten({"foo": ("bar", "baaz")}) == "1d3sfoo2l3sbar4sbaaz"