Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-103583: Add codecs and maps to _codecs_* module state#103540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
erlend-aasland merged 5 commits into python:main from erlend-aasland:isolate-multibytecodecApr 17, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits Select commit Hold shift + click to select a range
ed80fe3 Rename module state struct
erlend-aasland f5b906f Add codec and mapping lists to codec module states
erlend-aasland 9973a7d Silence unused variable warnings
erlend-aasland 605c4fb Pull in main
erlend-aasland 65af110 Hide ugly code in macro
erlend-aasland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -60,8 +60,20 @@ struct pair_encodemap{ | ||
| DBCHAR code; | ||
| }; | ||
| static const MultibyteCodec *codec_list; | ||
| static const struct dbcs_map *mapping_list; | ||
| typedef struct{ | ||
| int num_mappings; | ||
| int num_codecs; | ||
| struct dbcs_map *mapping_list; | ||
| MultibyteCodec *codec_list; | ||
| } cjkcodecs_module_state; | ||
| static inline cjkcodecs_module_state * | ||
| get_module_state(PyObject *mod) | ||
| { | ||
| void *state = PyModule_GetState(mod); | ||
| assert(state != NULL); | ||
| return (cjkcodecs_module_state *)state; | ||
| } | ||
| #define CODEC_INIT(encoding) \ | ||
| static int encoding##_codec_init(const void *config) | ||
| @@ -202,16 +214,42 @@ static const struct dbcs_map *mapping_list; | ||
| #define TRYMAP_DEC(charset, assi, c1, c2) \ | ||
| _TRYMAP_DEC(&charset##_decmap[c1], assi, c2) | ||
| #define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] ={ | ||
| #define MAPPING_ENCONLY(enc){#enc, (void*)enc##_encmap, NULL}, | ||
| #define MAPPING_DECONLY(enc){#enc, NULL, (void*)enc##_decmap}, | ||
| #define MAPPING_ENCDEC(enc){#enc, (void*)enc##_encmap, (void*)enc##_decmap}, | ||
| #define END_MAPPINGS_LIST \ | ||
| {"", NULL, NULL} }; \ | ||
| static const struct dbcs_map *mapping_list = \ | ||
| (const struct dbcs_map *)_mapping_list; | ||
| #define BEGIN_MAPPINGS_LIST(NUM) \ | ||
| static int \ | ||
| add_mappings(cjkcodecs_module_state *st) \ | ||
| {\ | ||
| int idx = 0; \ | ||
| (void)idx; \ | ||
| st->num_mappings = NUM; \ | ||
| st->mapping_list = PyMem_Calloc(NUM, sizeof(struct dbcs_map)); \ | ||
| if (st->mapping_list == NULL){\ | ||
| return -1; \ | ||
| } | ||
| #define MAPPING_ENCONLY(enc) \ | ||
| st->mapping_list[idx++] = (struct dbcs_map){#enc, (void*)enc##_encmap, NULL}; | ||
| #define MAPPING_DECONLY(enc) \ | ||
| st->mapping_list[idx++] = (struct dbcs_map){#enc, NULL, (void*)enc##_decmap}; | ||
| #define MAPPING_ENCDEC(enc) \ | ||
| st->mapping_list[idx++] = (struct dbcs_map){#enc, (void*)enc##_encmap, (void*)enc##_decmap}; | ||
| #define END_MAPPINGS_LIST \ | ||
| assert(st->num_mappings == idx); \ | ||
| return 0; \ | ||
| } | ||
| #define BEGIN_CODECS_LIST(NUM) \ | ||
| static int \ | ||
| add_codecs(cjkcodecs_module_state *st) \ | ||
| {\ | ||
| int idx = 0; \ | ||
| (void)idx; \ | ||
| st->num_codecs = NUM; \ | ||
| st->codec_list = PyMem_Calloc(NUM, sizeof(MultibyteCodec)); \ | ||
| if (st->codec_list == NULL){\ | ||
| return -1; \ | ||
| } | ||
| #define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] ={ | ||
| #define _STATEFUL_METHODS(enc) \ | ||
| enc##_encode, \ | ||
| enc##_encode_init, \ | ||
| @@ -222,23 +260,21 @@ static const struct dbcs_map *mapping_list; | ||
| #define _STATELESS_METHODS(enc) \ | ||
| enc##_encode, NULL, NULL, \ | ||
| enc##_decode, NULL, NULL, | ||
| #define CODEC_STATEFUL(enc){\ | ||
| #enc, NULL, NULL, \ | ||
| _STATEFUL_METHODS(enc) \ | ||
| }, | ||
| #define CODEC_STATELESS(enc){\ | ||
| #enc, NULL, NULL, \ | ||
| _STATELESS_METHODS(enc) \ | ||
| }, | ||
| #define CODEC_STATELESS_WINIT(enc){\ | ||
| #enc, NULL, \ | ||
| enc##_codec_init, \ | ||
| _STATELESS_METHODS(enc) \ | ||
| }, | ||
| #define END_CODECS_LIST \ | ||
| {"", NULL,} }; \ | ||
| static const MultibyteCodec *codec_list = \ | ||
| (const MultibyteCodec *)_codec_list; | ||
| #define NEXT_CODEC \ | ||
| st->codec_list[idx++] | ||
| #define CODEC_STATEFUL(enc) \ | ||
| NEXT_CODEC = (MultibyteCodec){#enc, NULL, NULL, _STATEFUL_METHODS(enc)}; | ||
| #define CODEC_STATELESS(enc) \ | ||
| NEXT_CODEC = (MultibyteCodec){#enc, NULL, NULL, _STATELESS_METHODS(enc)}; | ||
| #define CODEC_STATELESS_WINIT(enc) \ | ||
| NEXT_CODEC = (MultibyteCodec){#enc, NULL, enc##_codec_init, _STATELESS_METHODS(enc)}; | ||
| #define END_CODECS_LIST \ | ||
| assert(st->num_codecs == idx); \ | ||
| return 0; \ | ||
| } | ||
| @@ -249,53 +285,70 @@ getmultibytecodec(void) | ||
| } | ||
| static PyObject * | ||
| getcodec(PyObject *self, PyObject *encoding) | ||
| _getcodec(const MultibyteCodec *codec) | ||
| { | ||
| PyObject *codecobj, *r, *cofunc; | ||
| const MultibyteCodec *codec; | ||
| const char *enc; | ||
| if (!PyUnicode_Check(encoding)){ | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "encoding name must be a string."); | ||
| PyObject *cofunc = getmultibytecodec(); | ||
| if (cofunc == NULL){ | ||
| return NULL; | ||
| } | ||
| enc = PyUnicode_AsUTF8(encoding); | ||
| if (enc == NULL) | ||
| return NULL; | ||
| cofunc = getmultibytecodec(); | ||
| if (cofunc == NULL) | ||
| PyObject *codecobj = PyCapsule_New((void *)codec, | ||
| PyMultibyteCodec_CAPSULE_NAME, | ||
| NULL); | ||
| if (codecobj == NULL){ | ||
| Py_DECREF(cofunc); | ||
| return NULL; | ||
| } | ||
| for (codec = codec_list; codec->encoding[0]; codec++) | ||
| if (strcmp(codec->encoding, enc) == 0) | ||
| break; | ||
| PyObject *res = PyObject_CallOneArg(cofunc, codecobj); | ||
erlend-aasland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| Py_DECREF(codecobj); | ||
| Py_DECREF(cofunc); | ||
| return res; | ||
| } | ||
| if (codec->encoding[0] == '\0'){ | ||
| PyErr_SetString(PyExc_LookupError, | ||
| "no such codec is supported."); | ||
| static PyObject * | ||
| getcodec(PyObject *self, PyObject *encoding) | ||
| { | ||
| if (!PyUnicode_Check(encoding)){ | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "encoding name must be a string."); | ||
| return NULL; | ||
| } | ||
| codecobj = PyCapsule_New((void *)codec, PyMultibyteCodec_CAPSULE_NAME, NULL); | ||
| if (codecobj == NULL) | ||
| const char *enc = PyUnicode_AsUTF8(encoding); | ||
| if (enc == NULL){ | ||
| return NULL; | ||
| } | ||
| r = PyObject_CallOneArg(cofunc, codecobj); | ||
| Py_DECREF(codecobj); | ||
| Py_DECREF(cofunc); | ||
| cjkcodecs_module_state *st = get_module_state(self); | ||
| for (int i = 0; i < st->num_codecs; i++){ | ||
| const MultibyteCodec *codec = &st->codec_list[i]; | ||
| if (strcmp(codec->encoding, enc) == 0){ | ||
| return _getcodec(codec); | ||
| } | ||
| } | ||
| return r; | ||
| PyErr_SetString(PyExc_LookupError, | ||
| "no such codec is supported."); | ||
| return NULL; | ||
| } | ||
| static int add_mappings(cjkcodecs_module_state *); | ||
| static int add_codecs(cjkcodecs_module_state *); | ||
| static int | ||
| register_maps(PyObject *module) | ||
| { | ||
| const struct dbcs_map *h; | ||
| // Init module state. | ||
| cjkcodecs_module_state *st = get_module_state(module); | ||
| if (add_mappings(st) < 0){ | ||
| return -1; | ||
| } | ||
| if (add_codecs(st) < 0){ | ||
| return -1; | ||
| } | ||
| for (h = mapping_list; h->charset[0] != '\0' h++){ | ||
| for (int i = 0; i < st->num_mappings; i++){ | ||
| const struct dbcs_map *h = &st->mapping_list[i]; | ||
| char mhname[256] = "__map_" | ||
| strcpy(mhname + sizeof("__map_") - 1, h->charset); | ||
| @@ -394,6 +447,13 @@ _cjk_exec(PyObject *module) | ||
| return register_maps(module); | ||
| } | ||
| static void | ||
| _cjk_free(void *mod) | ||
| { | ||
| cjkcodecs_module_state *st = get_module_state((PyObject *)mod); | ||
| PyMem_Free(st->mapping_list); | ||
| PyMem_Free(st->codec_list); | ||
| } | ||
| static struct PyMethodDef _cjk_methods[] ={ | ||
| {"getcodec", (PyCFunction)getcodec, METH_O, ""}, | ||
| @@ -409,9 +469,10 @@ static PyModuleDef_Slot _cjk_slots[] ={ | ||
| static struct PyModuleDef _cjk_module ={\ | ||
| PyModuleDef_HEAD_INIT, \ | ||
| .m_name = "_codecs_"#loc, \ | ||
| .m_size = 0, \ | ||
| .m_size = sizeof(cjkcodecs_module_state), \ | ||
| .m_methods = _cjk_methods, \ | ||
| .m_slots = _cjk_slots, \ | ||
| .m_free = _cjk_free, \ | ||
| }; \ | ||
| \ | ||
| PyMODINIT_FUNC \ | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, readability will be frustrating (it's not your fault), I will think about a better way.. :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, readability is already frustrating with these modules. If we are to keep the diff down, we just have to go with the macro template style already used. An alternative is to refactor the modules first, but that bears a risk.