bpo-32030: Add _PyImport_Fini2() (#4737) · python/cpython@92a3c6f (original) (raw)

`@@ -30,6 +30,7 @@ static PyObject *extensions = NULL;

`

30

30

`extern struct _inittab _PyImport_Inittab[];

`

31

31

``

32

32

`struct _inittab *PyImport_Inittab = _PyImport_Inittab;

`

``

33

`+

static struct _inittab *inittab_copy = NULL;

`

33

34

``

34

35

`/*[clinic input]

`

35

36

`module _imp

`

`@@ -285,6 +286,19 @@ _PyImport_Fini(void)

`

285

286

` }

`

286

287

`}

`

287

288

``

``

289

`+

void

`

``

290

`+

_PyImport_Fini2(void)

`

``

291

`+

{

`

``

292

`+

/* Use the same memory allocator than PyImport_ExtendInittab(). */

`

``

293

`+

PyMemAllocatorEx old_alloc;

`

``

294

`+

_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

``

295

+

``

296

`+

/* Free memory allocated by PyImport_ExtendInittab() */

`

``

297

`+

PyMem_RawFree(inittab_copy);

`

``

298

+

``

299

`+

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

``

300

`+

}

`

``

301

+

288

302

`/* Helper for sys */

`

289

303

``

290

304

`PyObject *

`

`@@ -2233,9 +2247,9 @@ PyInit_imp(void)

`

2233

2247

`int

`

2234

2248

`PyImport_ExtendInittab(struct _inittab *newtab)

`

2235

2249

`{

`

2236

``

`-

static struct _inittab *our_copy = NULL;

`

2237

2250

`struct _inittab *p;

`

2238

``

`-

int i, n;

`

``

2251

`+

Py_ssize_t i, n;

`

``

2252

`+

int res = 0;

`

2239

2253

``

2240

2254

`/* Count the number of entries in both tables */

`

2241

2255

`for (n = 0; newtab[n].name != NULL; n++)

`

`@@ -2245,19 +2259,35 @@ PyImport_ExtendInittab(struct _inittab *newtab)

`

2245

2259

`for (i = 0; PyImport_Inittab[i].name != NULL; i++)

`

2246

2260

` ;

`

2247

2261

``

``

2262

`+

/* Force default raw memory allocator to get a known allocator to be able

`

``

2263

`+

to release the memory in _PyImport_Fini2() */

`

``

2264

`+

PyMemAllocatorEx old_alloc;

`

``

2265

`+

_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

``

2266

+

2248

2267

`/* Allocate new memory for the combined table */

`

2249

``

`-

p = our_copy;

`

2250

``

`-

PyMem_RESIZE(p, struct _inittab, i+n+1);

`

2251

``

`-

if (p == NULL)

`

2252

``

`-

return -1;

`

``

2268

`+

if ((i + n + 1) <= PY_SSIZE_T_MAX / sizeof(struct _inittab)) {

`

``

2269

`+

size_t size = sizeof(struct _inittab) * (i + n + 1);

`

``

2270

`+

p = PyMem_RawRealloc(inittab_copy, size);

`

``

2271

`+

}

`

``

2272

`+

else {

`

``

2273

`+

p = NULL;

`

``

2274

`+

}

`

``

2275

`+

if (p == NULL) {

`

``

2276

`+

res = -1;

`

``

2277

`+

goto done;

`

``

2278

`+

}

`

2253

2279

``

2254

``

`-

/* Copy the tables into the new memory */

`

2255

``

`-

if (our_copy != PyImport_Inittab)

`

``

2280

`+

/* Copy the tables into the new memory at the first call

`

``

2281

`+

to PyImport_ExtendInittab(). */

`

``

2282

`+

if (inittab_copy != PyImport_Inittab) {

`

2256

2283

`memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));

`

2257

``

`-

PyImport_Inittab = our_copy = p;

`

2258

``

`-

memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));

`

``

2284

`+

}

`

``

2285

`+

memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab));

`

``

2286

`+

PyImport_Inittab = inittab_copy = p;

`

2259

2287

``

2260

``

`-

return 0;

`

``

2288

`+

done:

`

``

2289

`+

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

`

``

2290

`+

return res;

`

2261

2291

`}

`

2262

2292

``

2263

2293

`/* Shorthand to add a single entry given a name and a function */

`