Turn rapidjson.Decoder into heap type#227
Turn rapidjson.Decoder into heap type#227VestniK wants to merge 1 commit intopython-rapidjson:masterfrom
Conversation
Each static type is uniq and global for all subinterpreters and can't access cached PyObjects. Decoder do use cached PyObjecs and must access cache stored in module rather than in static variables. Static type it can't access "proper module" due to its "per process singleton nature". The recomended way is to turn static types into heap types in order to use cached PyObjects.
|
This PR is a first step of work on the issue 226. Other types declared by the module must be converted to heap types as it's recommended in the cpython docs
The main purpose of having this small step as individual PR is getting early feedback on coding style issues or getting other comments which should be taken into account on further work. |
| "rapidjson.Decoder", /* name */ | ||
| sizeof(DecoderObject), /* basicsize */ | ||
| 0, /* itemsize */ | ||
| Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE, /* flags */ |
There was a problem hiding this comment.
The type is flagged as Py_TPFLAGS_IMMUTABLETYPE to make this change more "noop-like". All static types are immutable. Decoder type remains immutable after it's transformation into heap-type. This flag can be dropped if it's ok to make Decoder to be closer to regular types defined in python.
| Py_INCREF(&Decoder_Type); | ||
| if (PyModule_AddObject(m, "Decoder", (PyObject*) &Decoder_Type) < 0) { | ||
| Py_DECREF(&Decoder_Type); | ||
| if (PyModule_AddObject(m, "Decoder", decoder_type.get()) < 0) |
There was a problem hiding this comment.
Using RAII here prolongates lifetime of a strong reference to decoder type to the end of the module exec function. It shouldn't affect type object lifetime since there are other strong references to it. But using RAII make the code smaller and less error prone.
Each static type is unique and global for all subinterpreters and can't access cached PyObjects. Decoder do use cached PyObjecs and must access cache stored in module rather than in static variables.
Static type can't access "proper module" due to its "per process singleton nature". The recommended way is to turn static types into heap types in order to use cached PyObjects.