File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -166,6 +166,27 @@ def test_document_none_type():
166166 assert doc .as_obj == [None ]
167167
168168
169+ def test_document_dict_type ():
170+ """
171+ Ensure we can load and dump the dict type.
172+ """
173+ doc = Document ('{"a": "b"}' )
174+ assert doc .dumps () == '{"a":"b"}'
175+ assert doc .as_obj == {'a' : 'b' }
176+
177+ doc = Document ({"a" : "b" })
178+ assert doc .dumps () == '{"a":"b"}'
179+ assert doc .as_obj == {'a' : 'b' }
180+
181+ with pytest .raises (TypeError ) as exc :
182+ doc = Document ({1 : 'b' })
183+ assert exc .value .args [0 ] == 'Dictionary keys must be strings'
184+
185+ with pytest .raises (TypeError ) as exc :
186+ doc = Document ({'\ud83d \ude47 ' : 'foo' })
187+ assert exc .value .args [0 ] == 'Dictionary keys must be strings'
188+
189+
169190def test_document_get_pointer ():
170191 """
171192 Ensure JSON pointers work.
Original file line number Diff line number Diff line change @@ -364,6 +364,13 @@ static inline yyjson_mut_val *mut_primitive_to_element(
364364 while (PyDict_Next (obj , & i , & key , & value )) {
365365 Py_ssize_t str_len ;
366366 const char * str = PyUnicode_AsUTF8AndSize (key , & str_len );
367+ if (yyjson_unlikely (str == NULL )) {
368+ PyErr_Format (PyExc_TypeError ,
369+ "Dictionary keys must be strings" ,
370+ Py_TYPE (obj )-> tp_name
371+ );
372+ return NULL ;
373+ }
367374 object_value = mut_primitive_to_element (self , doc , value );
368375 if (yyjson_unlikely (object_value == NULL )) {
369376 return NULL ;
You can’t perform that action at this time.
0 commit comments