@@ -33,6 +33,26 @@ typedef struct {
3333
3434PyTypeObject Proxy_Type ;
3535
36+
37+ /* ------------------------------------------------------------------------- */
38+
39+ static PyObject * identity_ref = NULL ;
40+ static PyObject *
41+ identity (PyObject * self , PyObject * value )
42+ {
43+ Py_INCREF (value );
44+ return value ;
45+ }
46+
47+ /* ------------------------------------------------------------------------- */
48+
49+ PyDoc_STRVAR (identity_doc , "Indentity function: returns the single argument." );
50+
51+ static struct PyMethodDef module_functions [] = {
52+ {"identity" , identity , METH_O , identity_doc },
53+ {NULL , NULL }
54+ };
55+
3656/* ------------------------------------------------------------------------- */
3757
3858static PyObject * Proxy__ensure_wrapped (ProxyObject * self )
@@ -844,6 +864,15 @@ static PyObject *Proxy_reversed(
844864 self -> wrapped , NULL );
845865}
846866
867+ /* ------------------------------------------------------------------------- */
868+ static PyObject * Proxy_reduce (
869+ ProxyObject * self , PyObject * args )
870+ {
871+ Proxy__ENSURE_WRAPPED_OR_RETURN_NULL (self );
872+
873+ return Py_BuildValue ("(O(O))" , identity_ref , self -> wrapped );
874+ }
875+
847876/* ------------------------------------------------------------------------- */
848877
849878#if PY_MAJOR_VERSION >= 3
@@ -1227,6 +1256,8 @@ static PyMethodDef Proxy_methods[] = {
12271256 METH_VARARGS , 0 },
12281257 { "__bytes__" , (PyCFunction )Proxy_bytes , METH_NOARGS , 0 },
12291258 { "__reversed__" , (PyCFunction )Proxy_reversed , METH_NOARGS , 0 },
1259+ { "__reduce__" , (PyCFunction )Proxy_reduce , METH_NOARGS , 0 },
1260+ { "__reduce_ex__" , (PyCFunction )Proxy_reduce , METH_O , 0 },
12301261#if PY_MAJOR_VERSION >= 3
12311262 { "__round__" , (PyCFunction )Proxy_round , METH_NOARGS , 0 },
12321263#endif
@@ -1308,26 +1339,27 @@ PyTypeObject Proxy_Type = {
13081339#if PY_MAJOR_VERSION >= 3
13091340static struct PyModuleDef moduledef = {
13101341 PyModuleDef_HEAD_INIT ,
1311- "cext" , /* m_name */
1312- NULL , /* m_doc */
1313- -1 , /* m_size */
1314- NULL , /* m_methods */
1315- NULL , /* m_reload */
1316- NULL , /* m_traverse */
1317- NULL , /* m_clear */
1318- NULL , /* m_free */
1342+ "lazy_object_proxy. cext" , /* m_name */
1343+ NULL , /* m_doc */
1344+ -1 , /* m_size */
1345+ module_functions , /* m_methods */
1346+ NULL , /* m_reload */
1347+ NULL , /* m_traverse */
1348+ NULL , /* m_clear */
1349+ NULL , /* m_free */
13191350};
13201351#endif
13211352
13221353static PyObject *
13231354moduleinit (void )
13241355{
13251356 PyObject * module ;
1357+ PyObject * dict ;
13261358
13271359#if PY_MAJOR_VERSION >= 3
13281360 module = PyModule_Create (& moduledef );
13291361#else
1330- module = Py_InitModule3 ("cext" , NULL , NULL );
1362+ module = Py_InitModule3 ("lazy_object_proxy. cext" , module_functions , NULL );
13311363#endif
13321364
13331365 if (module == NULL )
@@ -1339,6 +1371,14 @@ moduleinit(void)
13391371 if (PyType_Ready (& Proxy_Type ) < 0 )
13401372 return NULL ;
13411373
1374+ dict = PyModule_GetDict (module );
1375+ if (dict == NULL )
1376+ return NULL ;
1377+ identity_ref = PyDict_GetItemString (dict , "identity" );
1378+ if (identity_ref == NULL )
1379+ return NULL ;
1380+ Py_INCREF (identity_ref );
1381+
13421382 Py_INCREF (& Proxy_Type );
13431383 PyModule_AddObject (module , "Proxy" ,
13441384 (PyObject * )& Proxy_Type );
0 commit comments