Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replace add_type_to_module with ADD_TYPE
  • Loading branch information
AA-Turner committed May 9, 2025
commit fa9b12b6edee0a0a33f2d33741a4a3a272ddbe32
72 changes: 24 additions & 48 deletions Modules/_zstd/_zstdmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,22 +642,6 @@ add_vars_to_module(PyObject *m)
} \
} while(0)

static inline int
add_type_to_module(PyObject *module, const char *name,
PyType_Spec *type_spec, PyTypeObject **dest)
{
PyObject *temp = PyType_FromModuleAndSpec(module, type_spec, NULL);

if (PyModule_AddObjectRef(module, name, temp) < 0) {
Py_XDECREF(temp);
return -1;
}

*dest = (PyTypeObject*) temp;

return 0;
}

static inline int
add_constant_to_type(PyTypeObject *type, const char *name, long value)
{
Expand All @@ -673,8 +657,20 @@ add_constant_to_type(PyTypeObject *type, const char *name, long value)
return rc;
}

static int _zstd_exec(PyObject *module) {
_zstd_state* const mod_state = get_zstd_state(module);
static int _zstd_exec(PyObject *m)
{
#define ADD_TYPE(TYPE, SPEC) \
do { \
TYPE = (PyTypeObject *)PyType_FromModuleAndSpec(m, &(SPEC), NULL); \
if (TYPE == NULL) { \
return -1; \
} \
if (PyModule_AddType(m, TYPE) < 0) { \
return -1; \
} \
} while (0)

_zstd_state* const mod_state = get_zstd_state(m);

/* Reusable objects & variables */
mod_state->empty_bytes = PyBytes_FromStringAndSize(NULL, 0);
Expand All @@ -698,40 +694,26 @@ static int _zstd_exec(PyObject *module) {
mod_state->DParameter_type = NULL;

/* Add variables to module */
if (add_vars_to_module(module) < 0) {
if (add_vars_to_module(m) < 0) {
return -1;
}

/* ZstdError */
/* Create and add heap types */
ADD_TYPE(mod_state->ZstdDict_type, zstd_dict_type_spec);
ADD_TYPE(mod_state->ZstdCompressor_type, zstd_compressor_type_spec);
ADD_TYPE(mod_state->ZstdDecompressor_type, zstd_decompressor_type_spec);
mod_state->ZstdError = PyErr_NewExceptionWithDoc(
"_zstd.ZstdError",
"Call to the underlying zstd library failed.",
NULL, NULL);
"_zstd.ZstdError",
"An error occurred in the zstd library.",
NULL, NULL);
if (mod_state->ZstdError == NULL) {
return -1;
}

if (PyModule_AddObjectRef(module, "ZstdError", mod_state->ZstdError) < 0) {
if (PyModule_AddType(m, (PyTypeObject *)mod_state->ZstdError) < 0) {
Py_DECREF(mod_state->ZstdError);
return -1;
}

/* ZstdDict */
if (add_type_to_module(module,
"ZstdDict",
&zstddict_type_spec,
&mod_state->ZstdDict_type) < 0) {
return -1;
}

// ZstdCompressor
if (add_type_to_module(module,
"ZstdCompressor",
&zstdcompressor_type_spec,
&mod_state->ZstdCompressor_type) < 0) {
return -1;
}

// Add EndDirective enum to ZstdCompressor
if (add_constant_to_type(mod_state->ZstdCompressor_type,
"CONTINUE",
Expand All @@ -751,13 +733,7 @@ static int _zstd_exec(PyObject *module) {
return -1;
}

// ZstdDecompressor
if (add_type_to_module(module,
"ZstdDecompressor",
&zstddecompressor_type_spec,
&mod_state->ZstdDecompressor_type) < 0) {
return -1;
}
#undef ADD_TYPE

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions Modules/_zstd/_zstdmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ get_zstd_state_from_type(PyTypeObject *type) {
return (_zstd_state *)state;
}

extern PyType_Spec zstddict_type_spec;
extern PyType_Spec zstdcompressor_type_spec;
extern PyType_Spec zstddecompressor_type_spec;
extern PyType_Spec zstd_dict_type_spec;
extern PyType_Spec zstd_compressor_type_spec;
extern PyType_Spec zstd_decompressor_type_spec;

struct _zstd_state {
PyObject *empty_bytes;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_zstd/compressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ static PyType_Slot zstdcompressor_slots[] = {
{0}
};

PyType_Spec zstdcompressor_type_spec = {
PyType_Spec zstd_compressor_type_spec = {
.name = "_zstd.ZstdCompressor",
.basicsize = sizeof(ZstdCompressor),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
Comment thread
AA-Turner marked this conversation as resolved.
Expand Down
2 changes: 1 addition & 1 deletion Modules/_zstd/decompressor.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ static PyType_Slot ZstdDecompressor_slots[] = {
{0}
};

PyType_Spec zstddecompressor_type_spec = {
PyType_Spec zstd_decompressor_type_spec = {
.name = "_zstd.ZstdDecompressor",
.basicsize = sizeof(ZstdDecompressor),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
Expand Down
2 changes: 1 addition & 1 deletion Modules/_zstd/zstddict.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static PyType_Slot zstddict_slots[] = {
{0}
};

PyType_Spec zstddict_type_spec = {
PyType_Spec zstd_dict_type_spec = {
.name = "_zstd.ZstdDict",
.basicsize = sizeof(ZstdDict),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
Expand Down