bpo-41796: Call _PyAST_Fini() earlier to fix a leak (GH-23131) · python/cpython@fd957c1 (original) (raw)

`@@ -1015,18 +1015,35 @@ def visitModule(self, mod):

`

1015

1015

``

1016

1016

`""", 0, reflow=False)

`

1017

1017

``

1018

``

`-

self.emit("static int init_types(struct ast_state *state)",0)

`

1019

``

`-

self.emit("{", 0)

`

1020

``

`-

self.emit("if (state->initialized) return 1;", 1)

`

1021

``

`-

self.emit("if (init_identifiers(state) < 0) return 0;", 1)

`

1022

``

`-

self.emit("state->AST_type = PyType_FromSpec(&AST_type_spec);", 1)

`

1023

``

`-

self.emit("if (!state->AST_type) return 0;", 1)

`

1024

``

`-

self.emit("if (add_ast_fields(state) < 0) return 0;", 1)

`

``

1018

`+

self.file.write(textwrap.dedent('''

`

``

1019

`+

static int

`

``

1020

`+

init_types(struct ast_state *state)

`

``

1021

`+

{

`

``

1022

`+

// init_types() must not be called after _PyAST_Fini()

`

``

1023

`+

// has been called

`

``

1024

`+

assert(state->initialized >= 0);

`

``

1025

+

``

1026

`+

if (state->initialized) {

`

``

1027

`+

return 1;

`

``

1028

`+

}

`

``

1029

`+

if (init_identifiers(state) < 0) {

`

``

1030

`+

return 0;

`

``

1031

`+

}

`

``

1032

`+

state->AST_type = PyType_FromSpec(&AST_type_spec);

`

``

1033

`+

if (!state->AST_type) {

`

``

1034

`+

return 0;

`

``

1035

`+

}

`

``

1036

`+

if (add_ast_fields(state) < 0) {

`

``

1037

`+

return 0;

`

``

1038

`+

}

`

``

1039

`+

'''))

`

1025

1040

`for dfn in mod.dfns:

`

1026

1041

`self.visit(dfn)

`

1027

``

`-

self.emit("state->initialized = 1;", 1)

`

1028

``

`-

self.emit("return 1;", 1);

`

1029

``

`-

self.emit("}", 0)

`

``

1042

`+

self.file.write(textwrap.dedent('''

`

``

1043

`+

state->initialized = 1;

`

``

1044

`+

return 1;

`

``

1045

`+

}

`

``

1046

`+

'''))

`

1030

1047

``

1031

1048

`def visitProduct(self, prod, name):

`

1032

1049

`if prod.fields:

`

`@@ -1353,23 +1370,27 @@ def generate_ast_state(module_state, f):

`

1353

1370

``

1354

1371

``

1355

1372

`def generate_ast_fini(module_state, f):

`

1356

``

`-

f.write("""

`

1357

``

`-

void _PyAST_Fini(PyThreadState *tstate)

`

1358

``

`-

{

`

1359

``

`-

#ifdef Py_BUILD_CORE

`

1360

``

`-

struct ast_state *state = &tstate->interp->ast;

`

1361

``

`-

#else

`

1362

``

`-

struct ast_state *state = &global_ast_state;

`

1363

``

`-

#endif

`

1364

``

-

1365

``

`-

""")

`

``

1373

`+

f.write(textwrap.dedent("""

`

``

1374

`+

void _PyAST_Fini(PyInterpreterState *interp)

`

``

1375

`+

{

`

``

1376

`+

#ifdef Py_BUILD_CORE

`

``

1377

`+

struct ast_state *state = &interp->ast;

`

``

1378

`+

#else

`

``

1379

`+

struct ast_state *state = &global_ast_state;

`

``

1380

`+

#endif

`

``

1381

+

``

1382

`+

"""))

`

1366

1383

`for s in module_state:

`

1367

1384

`f.write(" Py_CLEAR(state->" + s + ');\n')

`

1368

``

`-

f.write("""

`

1369

``

`-

state->initialized = 0;

`

1370

``

`-

}

`

``

1385

`+

f.write(textwrap.dedent("""

`

``

1386

`+

#if defined(Py_BUILD_CORE) && !defined(NDEBUG)

`

``

1387

`+

state->initialized = -1;

`

``

1388

`+

#else

`

``

1389

`+

state->initialized = 0;

`

``

1390

`+

#endif

`

``

1391

`+

}

`

1371

1392

``

1372

``

`-

""")

`

``

1393

`+

"""))

`

1373

1394

``

1374

1395

``

1375

1396

`def generate_module_def(mod, f, internal_h):

`