Skip to content

Commit

Permalink
Add pickle support for enaml's Color (#316)
Browse files Browse the repository at this point in the history
* Add pickle support for enaml's Color
* Use 0xFF for bitmask
* Update changelog
  • Loading branch information
frmdstryr authored and MatthieuDartiailh committed Oct 2, 2018
1 parent cbfa82b commit 1b8a06f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
23 changes: 21 additions & 2 deletions enaml/src/colorext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,28 @@ Color_getset[] = {
};


static PyObject*
Color__reduce__( Color* self, void* context )
{
PyObject* obj = reinterpret_cast<PyObject*>( self );
uint32_t a = ( self->argb >> 24 ) & 0xFF;
uint32_t r = ( self->argb >> 16 ) & 0xFF;
uint32_t g = ( self->argb >> 8 ) & 0xFF;
uint32_t b = self->argb & 0xFF;
return Py_BuildValue( "O (iiii)", obj->ob_type, r, g, b, a );
}


static PyMethodDef
Color_methods[] = {
{ "__reduce__", ( PyCFunction )Color__reduce__, METH_NOARGS, "" },
{ 0 } // Sentinel
};


PyTypeObject Color_Type = {
PyVarObject_HEAD_INIT( &PyType_Type, 0 )
"colorext.Color", /* tp_name */
"enaml.colorext.Color", /* tp_name */
sizeof( Color ), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)Color_dealloc, /* tp_dealloc */
Expand Down Expand Up @@ -194,7 +213,7 @@ PyTypeObject Color_Type = {
0, /* tp_weaklistoffset */
(getiterfunc)0, /* tp_iter */
(iternextfunc)0, /* tp_iternext */
(struct PyMethodDef*)0, /* tp_methods */
Color_methods, /* tp_methods */
(struct PyMemberDef*)0, /* tp_members */
Color_getset, /* tp_getset */
0, /* tp_base */
Expand Down
1 change: 1 addition & 0 deletions releasenotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Enaml Release Notes

0.10.3 - unreleased
-------------------
- add pickle support for enaml's Color - #316
- add support for tiling and cascading to MdiArea PR # 259
- fix issue # 174 (MdiWindow not automatically shown when added) PR # 259
- add minimal workbench documentation PR # 258
Expand Down
10 changes: 10 additions & 0 deletions tests/test_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pickle

def test_color_pickle():
from enaml.colors import Color
rgba = 1, 2, 3, 4
c = Color(*rgba)
print(c.__reduce__())
blob = pickle.dumps(c)
c = pickle.loads(blob)
assert (c.red, c.green, c.blue, c.alpha) == rgba

0 comments on commit 1b8a06f

Please sign in to comment.