FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.10] gh-99578: Fix refleak in _imp.create_builtin() (GH-99642) by miss-islington · Pull Request #99644 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
30 changes: 30 additions & 0 deletions Lib/test/test_imp.py
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
@@ -1,3 +1,4 @@
import gc
import importlib
import importlib.util
import os
Expand Down Expand Up @@ -379,6 +380,35 @@ def test_find_and_load_checked_pyc(self):
self.assertEqual(mod.x, 42)


@support.cpython_only
def test_create_builtin_subinterp(self):
# gh-99578: create_builtin() behavior changes after the creation of the
# first sub-interpreter. Test both code paths, before and after the
# creation of a sub-interpreter. Previously, create_builtin() had
# a reference leak after the creation of the first sub-interpreter.

import builtins
create_builtin = support.get_attribute(_imp, "create_builtin")
class Spec:
name = "builtins"
spec = Spec()

def check_get_builtins():
refcnt = sys.getrefcount(builtins)
mod = _imp.create_builtin(spec)
self.assertIs(mod, builtins)
self.assertEqual(sys.getrefcount(builtins), refcnt + 1)
# Check that a GC collection doesn't crash
gc.collect()

check_get_builtins()

ret = support.run_in_subinterp("import builtins")
self.assertEqual(ret, 0)

check_get_builtins()


class ReloadTests(unittest.TestCase):

"""Very basic tests to make sure that imp.reload() operates just like
Expand Down
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
@@ -0,0 +1,3 @@
Fix a reference bug in :func:`_imp.create_builtin()` after the creation of the
first sub-interpreter for modules ``builtins`` and ``sys``. Patch by Victor
Stinner.
3 changes: 2 additions & 1 deletion Python/import.c
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
Expand Up @@ -1006,7 +1006,8 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
if (_PyUnicode_EqualToASCIIString(name, p->name)) {
if (p->initfunc == NULL) {
/* Cannot re-init internal module ("sys" or "builtins") */
return PyImport_AddModuleObject(name);
mod = PyImport_AddModuleObject(name);
return Py_XNewRef(mod);
}

mod = (*p->initfunc)();
Expand Down

Back | FazBrowse Home | New Git URL