| 59 | { |
| 60 | |
| 61 | class CompoundDataFromPythonDict |
| 62 | { |
| 63 | public : |
| 64 | |
| 65 | CompoundDataFromPythonDict() |
| 66 | { |
| 67 | converter::registry::push_back( |
| 68 | &convertible, |
| 69 | &construct, |
| 70 | type_id<CompoundDataPtr> () |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | private : |
| 75 | |
| 76 | static void *convertible( PyObject *obj_ptr ) |
| 77 | { |
| 78 | if ( !PyDict_Check( obj_ptr ) ) |
| 79 | { |
| 80 | return nullptr; |
| 81 | } |
| 82 | return obj_ptr; |
| 83 | } |
| 84 | |
| 85 | static void construct( |
| 86 | PyObject *obj_ptr, |
| 87 | converter::rvalue_from_python_stage1_data *data ) |
| 88 | { |
| 89 | assert( obj_ptr ); |
| 90 | assert( PyDict_Check( obj_ptr ) ); |
| 91 | |
| 92 | handle<> h( borrowed(obj_ptr) ); |
| 93 | dict d( h ); |
| 94 | |
| 95 | void *storage = (( converter::rvalue_from_python_storage<CompoundDataPtr>* ) data )->storage.bytes; |
| 96 | new( storage ) CompoundDataPtr( compoundDataFromDict( d ) ); |
| 97 | data->convertible = storage; |
| 98 | } |
| 99 | |
| 100 | static CompoundDataPtr compoundDataFromDict( const dict &v ) |
| 101 | { |
| 102 | CompoundDataPtr x = new CompoundData(); |
| 103 | list values = v.values(); |
| 104 | list keys = v.keys(); |
| 105 | |
| 106 | for (int i = 0; i < keys.attr("__len__")(); i++) |
| 107 | { |
| 108 | object key( keys[i] ); |
| 109 | object value( values[i] ); |
| 110 | extract<const char *> keyElem( key ); |
| 111 | if( !keyElem.check() ) |
| 112 | { |
| 113 | PyErr_SetString( PyExc_TypeError, "Incompatible key type. Only strings accepted." ); |
| 114 | throw_error_already_set(); |
| 115 | } |
| 116 | |
| 117 | extract<DataPtr> valueElem( value ); |
| 118 | if( valueElem.check() ) |