@@ -186,6 +186,10 @@ type_id py_loader_impl_get_return_type(PyObject * result)
186186 {
187187 return TYPE_ARRAY ;
188188 }
189+ else if (PyDict_Check (result ))
190+ {
191+ return TYPE_MAP ;
192+ }
189193 else if (PyCapsule_CheckExact (result ))
190194 {
191195 return TYPE_PTR ;
@@ -297,6 +301,86 @@ value py_loader_impl_return(PyObject * result, type_id id)
297301 array_value [iterator ] = py_loader_impl_return (element , py_loader_impl_get_return_type (element ));
298302 }
299303 }
304+ else if (id == TYPE_MAP )
305+ {
306+ Py_ssize_t key_iterator , iterator , keys_size , length = 0 ;
307+ value * map_value ;
308+ PyObject * keys ;
309+
310+ keys = PyDict_Keys (result );
311+ keys_size = PyList_Size (keys );
312+
313+ for (iterator = 0 ; iterator < keys_size ; ++ iterator )
314+ {
315+ PyObject * key = PyList_GetItem (keys , iterator );
316+
317+ #if PY_MAJOR_VERSION == 2
318+ if (PyString_Check (key ))
319+ {
320+ ++ length ;
321+ }
322+ #elif PY_MAJOR_VERSION == 3
323+ if (PyUnicode_Check (key ))
324+ {
325+ ++ length ;
326+ }
327+ #endif
328+ }
329+
330+ v = value_create_map (NULL , (size_t )length );
331+
332+ map_value = value_to_map (v );
333+
334+ for (iterator = 0 , key_iterator = 0 ; iterator < keys_size ; ++ iterator )
335+ {
336+ char * key_str = NULL ;
337+
338+ Py_ssize_t key_length = 0 ;
339+
340+ PyObject * element , * key ;
341+
342+ value * array_value ;
343+
344+ key = PyList_GetItem (keys , iterator );
345+
346+ #if PY_MAJOR_VERSION == 2
347+ if (PyString_Check (key ))
348+ {
349+ if (PyString_AsStringAndSize (key , & key_str , & key_length ) == -1 )
350+ {
351+ if (PyErr_Occurred () != NULL )
352+ {
353+ loader_impl_py py_impl = loader_impl_get (py_func -> impl );
354+
355+ py_loader_impl_error_print (py_impl );
356+ }
357+ }
358+ }
359+ #elif PY_MAJOR_VERSION == 3
360+ if (PyUnicode_Check (key ))
361+ {
362+ key_str = PyUnicode_AsUTF8AndSize (key , & key_length );
363+ }
364+ #endif
365+
366+ /* Allow only string keys by the moment */
367+ if (key_str != NULL )
368+ {
369+ element = PyDict_GetItem (result , key );
370+
371+ map_value [key_iterator ] = value_create_array (NULL , 2 );
372+
373+ array_value = value_to_array (map_value [key_iterator ]);
374+
375+ array_value [0 ] = value_create_string (key_str , (size_t )key_length );
376+
377+ /* TODO: Review recursion overflow */
378+ array_value [1 ] = py_loader_impl_return (element , py_loader_impl_get_return_type (element ));
379+
380+ ++ key_iterator ;
381+ }
382+ }
383+ }
300384 else if (id == TYPE_PTR )
301385 {
302386 void * ptr = NULL ;
@@ -643,7 +727,8 @@ int py_loader_impl_initialize_inspect_types(loader_impl impl, loader_impl_py py_
643727
644728 { TYPE_STRING , "str" },
645729 { TYPE_BUFFER , "bytes" },
646- { TYPE_ARRAY , "list" }
730+ { TYPE_ARRAY , "list" },
731+ { TYPE_MAP , "dict" }
647732 };
648733
649734 size_t index , size = sizeof (type_id_name_pair ) / sizeof (type_id_name_pair [0 ]);
0 commit comments