Author: Matti Picus <[email protected]>
Branch: matplotlib
Changeset: r92702:296db0043bc2
Date: 2017-10-10 14:47 +1100
http://bitbucket.org/pypy/pypy/changeset/296db0043bc2/

Log:    together with branch of matlab. tkinter plots begin to work

        This converts the ndarray data to a string, the non-CFFI version
        passes a pointer to the data

diff --git a/lib_pypy/_tkinter/app.py b/lib_pypy/_tkinter/app.py
--- a/lib_pypy/_tkinter/app.py
+++ b/lib_pypy/_tkinter/app.py
@@ -119,7 +119,7 @@
                              tklib.TCL_GLOBAL_ONLY)
 
         # This is used to get the application class for Tk 4.1 and up
-        argv0 = className.lower()
+        argv0 = className.lower().encode('ascii')
         tklib.Tcl_SetVar(self.interp, "argv0", argv0,
                          tklib.TCL_GLOBAL_ONLY)
 
diff --git a/lib_pypy/_tkinter/tclobj.py b/lib_pypy/_tkinter/tclobj.py
--- a/lib_pypy/_tkinter/tclobj.py
+++ b/lib_pypy/_tkinter/tclobj.py
@@ -2,6 +2,11 @@
 
 from .tklib_cffi import ffi as tkffi, lib as tklib
 import binascii
+try:
+    import numpy as np
+    hasNumpy = True
+except ImportError:
+    hasNumpy = False
 
 class TypeCache(object):
     def __init__(self):
@@ -86,6 +91,28 @@
     finally:
         tklib.mp_clear(bigValue)
 
+def AsObjNDArray(value):
+    # XXX there must be a better way
+    argv = tkffi.new("Tcl_Obj*[]", 3)
+    argv[0] = AsObj(' '.join([str(x) for x in value.shape]))
+    argv[1] = AsObj(value.dtype.str)
+    asstr = value.tostring()
+    argv[2] = AsObj(binascii.b2a_hex(asstr))
+    return tklib.Tcl_NewListObj(3, argv)
+
+def FromTclStringNDArray(data):
+    # unconvert data, assuming it is stringified from AsObjNDArray
+    indx1 = data.find(b'}')
+    shape = map(int, data[1:indx1].split())
+    size = np.prod(shape)
+    indx2 = data.find(b' ', indx1 + 2)
+    dtype = np.dtype(data[indx1 + 2:indx2])
+    start = indx2+1
+    stop = start + size * dtype.itemsize * 2
+    if stop > len(data):
+        raise ValueError('data too short')
+    vals = binascii.a2b_hex(data[start:stop])
+    return np.fromstring(vals, dtype=dtype).reshape(shape)
 
 def FromObj(app, value):
     """Convert a TclObj pointer into a Python object."""
@@ -175,6 +202,8 @@
     if isinstance(value, TclObject):
         tklib.Tcl_IncrRefCount(value._value)
         return value._value
+    if hasNumpy and isinstance(value, np.ndarray):
+        return AsObjNDArray(value)
 
     return AsObj(str(value))
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to