Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita6b07b5

Browse files
gh-103583: Add ref. dependency between multibytecodec modules (#103589)
1 parentbd2ed06 commita6b07b5

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

‎Modules/cjkcodecs/cjkcodecs.h

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,45 @@ getmultibytecodec(void)
284284
return_PyImport_GetModuleAttrString("_multibytecodec","__create_codec");
285285
}
286286

287+
staticvoid
288+
destroy_codec_capsule(PyObject*capsule)
289+
{
290+
void*ptr=PyCapsule_GetPointer(capsule,CODEC_CAPSULE);
291+
codec_capsule*data= (codec_capsule*)ptr;
292+
Py_DECREF(data->cjk_module);
293+
PyMem_Free(ptr);
294+
}
295+
296+
staticcodec_capsule*
297+
capsulate_codec(PyObject*mod,constMultibyteCodec*codec)
298+
{
299+
codec_capsule*data=PyMem_Malloc(sizeof(codec_capsule));
300+
if (data==NULL) {
301+
PyErr_NoMemory();
302+
returnNULL;
303+
}
304+
data->codec=codec;
305+
data->cjk_module=Py_NewRef(mod);
306+
returndata;
307+
}
308+
287309
staticPyObject*
288-
_getcodec(constMultibyteCodec*codec)
310+
_getcodec(PyObject*self,constMultibyteCodec*codec)
289311
{
290312
PyObject*cofunc=getmultibytecodec();
291313
if (cofunc==NULL) {
292314
returnNULL;
293315
}
294316

295-
PyObject*codecobj=PyCapsule_New((void*)codec,
296-
PyMultibyteCodec_CAPSULE_NAME,
297-
NULL);
317+
codec_capsule*data=capsulate_codec(self,codec);
318+
if (data==NULL) {
319+
Py_DECREF(cofunc);
320+
returnNULL;
321+
}
322+
PyObject*codecobj=PyCapsule_New(data,CODEC_CAPSULE,
323+
destroy_codec_capsule);
298324
if (codecobj==NULL) {
325+
PyMem_Free(data);
299326
Py_DECREF(cofunc);
300327
returnNULL;
301328
}
@@ -323,7 +350,7 @@ getcodec(PyObject *self, PyObject *encoding)
323350
for (inti=0;i<st->num_codecs;i++) {
324351
constMultibyteCodec*codec=&st->codec_list[i];
325352
if (strcmp(codec->encoding,enc)==0) {
326-
return_getcodec(codec);
353+
return_getcodec(self,codec);
327354
}
328355
}
329356

@@ -352,8 +379,7 @@ register_maps(PyObject *module)
352379
charmhname[256]="__map_";
353380
strcpy(mhname+sizeof("__map_")-1,h->charset);
354381

355-
PyObject*capsule=PyCapsule_New((void*)h,
356-
PyMultibyteCodec_CAPSULE_NAME,NULL);
382+
PyObject*capsule=PyCapsule_New((void*)h,MAP_CAPSULE,NULL);
357383
if (capsule==NULL) {
358384
return-1;
359385
}
@@ -417,14 +443,14 @@ importmap(const char *modname, const char *symbol,
417443
o=PyObject_GetAttrString(mod,symbol);
418444
if (o==NULL)
419445
gotoerrorexit;
420-
elseif (!PyCapsule_IsValid(o,PyMultibyteCodec_CAPSULE_NAME)) {
446+
elseif (!PyCapsule_IsValid(o,MAP_CAPSULE)) {
421447
PyErr_SetString(PyExc_ValueError,
422448
"map data must be a Capsule.");
423449
gotoerrorexit;
424450
}
425451
else {
426452
structdbcs_map*map;
427-
map=PyCapsule_GetPointer(o,PyMultibyteCodec_CAPSULE_NAME);
453+
map=PyCapsule_GetPointer(o,MAP_CAPSULE);
428454
if (encmap!=NULL)
429455
*encmap=map->encmap;
430456
if (decmap!=NULL)

‎Modules/cjkcodecs/multibytecodec.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,17 @@ static struct PyMethodDef multibytecodec_methods[] = {
720720
};
721721

722722
staticint
723-
multibytecodec_traverse(PyObject*self,visitprocvisit,void*arg)
723+
multibytecodec_clear(MultibyteCodecObject*self)
724+
{
725+
Py_CLEAR(self->cjk_module);
726+
return0;
727+
}
728+
729+
staticint
730+
multibytecodec_traverse(MultibyteCodecObject*self,visitprocvisit,void*arg)
724731
{
725732
Py_VISIT(Py_TYPE(self));
733+
Py_VISIT(self->cjk_module);
726734
return0;
727735
}
728736

@@ -731,6 +739,7 @@ multibytecodec_dealloc(MultibyteCodecObject *self)
731739
{
732740
PyObject_GC_UnTrack(self);
733741
PyTypeObject*tp=Py_TYPE(self);
742+
(void)multibytecodec_clear(self);
734743
tp->tp_free(self);
735744
Py_DECREF(tp);
736745
}
@@ -740,6 +749,7 @@ static PyType_Slot multibytecodec_slots[] = {
740749
{Py_tp_getattro,PyObject_GenericGetAttr},
741750
{Py_tp_methods,multibytecodec_methods},
742751
{Py_tp_traverse,multibytecodec_traverse},
752+
{Py_tp_clear,multibytecodec_clear},
743753
{0,NULL},
744754
};
745755

@@ -1953,14 +1963,14 @@ _multibytecodec___create_codec(PyObject *module, PyObject *arg)
19531963
/*[clinic end generated code: output=cfa3dce8260e809d input=6840b2a6b183fcfa]*/
19541964
{
19551965
MultibyteCodecObject*self;
1956-
constMultibyteCodec*codec;
19571966

1958-
if (!PyCapsule_IsValid(arg,PyMultibyteCodec_CAPSULE_NAME)) {
1967+
if (!PyCapsule_IsValid(arg,CODEC_CAPSULE)) {
19591968
PyErr_SetString(PyExc_ValueError,"argument type invalid");
19601969
returnNULL;
19611970
}
19621971

1963-
codec=PyCapsule_GetPointer(arg,PyMultibyteCodec_CAPSULE_NAME);
1972+
codec_capsule*data=PyCapsule_GetPointer(arg,CODEC_CAPSULE);
1973+
constMultibyteCodec*codec=data->codec;
19641974
if (codec->codecinit!=NULL&&codec->codecinit(codec->config)!=0)
19651975
returnNULL;
19661976

@@ -1969,6 +1979,7 @@ _multibytecodec___create_codec(PyObject *module, PyObject *arg)
19691979
if (self==NULL)
19701980
returnNULL;
19711981
self->codec=codec;
1982+
self->cjk_module=Py_NewRef(data->cjk_module);
19721983

19731984
PyObject_GC_Track(self);
19741985
return (PyObject*)self;

‎Modules/cjkcodecs/multibytecodec.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typedef struct {
6363
typedefstruct {
6464
PyObject_HEAD
6565
constMultibyteCodec*codec;
66+
PyObject*cjk_module;
6667
}MultibyteCodecObject;
6768

6869
#defineMultibyteCodec_Check(state,op) Py_IS_TYPE((op), state->multibytecodec_type)
@@ -130,7 +131,13 @@ typedef struct {
130131
#defineMBENC_FLUSH 0x0001/* encode all characters encodable */
131132
#defineMBENC_MAX MBENC_FLUSH
132133

133-
#definePyMultibyteCodec_CAPSULE_NAME "multibytecodec.__map_*"
134+
typedefstruct {
135+
constMultibyteCodec*codec;
136+
PyObject*cjk_module;
137+
}codec_capsule;
138+
139+
#defineMAP_CAPSULE "multibytecodec.map"
140+
#defineCODEC_CAPSULE "multibytecodec.codec"
134141

135142

136143
#ifdef__cplusplus

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp