Skip to content

gh-76785: Drop PyInterpreterID_Type #117101

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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Change nearly all the subtests into separate tests.
  • Loading branch information
ericsnowcurrently committed Mar 21, 2024
commit 49793f2fc99c3d9ccfb91ef501feab813dc70cb8
258 changes: 152 additions & 106 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2220,20 +2220,27 @@ def new_interpreter(self):
self.add_interp_cleanup(id)
return id

def test_conversion(self):
def test_conversion_int(self):
convert = _testinternalcapi.normalize_interp_id
interpid = convert(10)
self.assertEqual(interpid, 10)

with self.subTest('int'):
interpid = convert(10)
self.assertEqual(interpid, 10)
def test_conversion_coerced(self):
convert = _testinternalcapi.normalize_interp_id
class MyInt(str):
def __index__(self):
return 10
interpid = convert(MyInt())
self.assertEqual(interpid, 10)

with self.subTest('coerced'):
class MyInt(str):
def __index__(self):
return 10
def test_conversion_from_interpreter(self):
convert = _testinternalcapi.normalize_interp_id
interpid = self.new_interpreter()
converted = convert(interpid)
self.assertEqual(converted, interpid)

interpid = convert(MyInt())
self.assertEqual(interpid, 10)
def test_conversion_bad(self):
convert = _testinternalcapi.normalize_interp_id

for badid in [
object(),
Expand All @@ -2255,34 +2262,23 @@ def __index__(self):
with self.assertRaises(OverflowError):
convert(badid)

with self.subTest('from interpreter'):
interpid = self.new_interpreter()
converted = convert(interpid)
self.assertEqual(converted, interpid)

def test_lookup(self):
with self.subTest('exists'):
interpid = self.new_interpreter()
self.assertTrue(
_testinternalcapi.interpreter_exists(interpid))
def test_lookup_exists(self):
interpid = self.new_interpreter()
self.assertTrue(
_testinternalcapi.interpreter_exists(interpid))

with self.subTest('does not exist'):
interpid = _testinternalcapi.unused_interpreter_id()
self.assertFalse(
_testinternalcapi.interpreter_exists(interpid))

with self.subTest('destroyed'):
interpid = _interpreters.create()
_interpreters.destroy(interpid)
self.assertFalse(
_testinternalcapi.interpreter_exists(interpid))
def test_lookup_does_not_exist(self):
interpid = _testinternalcapi.unused_interpreter_id()
self.assertFalse(
_testinternalcapi.interpreter_exists(interpid))

def test_linked_lifecycle(self):
def create():
interpid = _testinternalcapi.new_interpreter()
self.add_interp_cleanup(interpid)
return interpid
def test_lookup_destroyed(self):
interpid = _interpreters.create()
_interpreters.destroy(interpid)
self.assertFalse(
_testinternalcapi.interpreter_exists(interpid))

def test_linked_lifecycle_does_not_exist(self):
exists = _testinternalcapi.interpreter_exists
is_linked = _testinternalcapi.interpreter_refcount_linked
link = _testinternalcapi.link_interpreter_refcount
Expand All @@ -2291,7 +2287,7 @@ def create():
incref = _testinternalcapi.interpreter_incref
decref = _testinternalcapi.interpreter_decref

with self.subTest('does not exist'):
with self.subTest('never existed'):
interpid = _testinternalcapi.unused_interpreter_id()
self.assertFalse(
exists(interpid))
Expand Down Expand Up @@ -2326,99 +2322,149 @@ def create():
with self.assertRaises(_interpreters.InterpreterNotFoundError):
decref(interpid)

def test_linked_lifecycle_initial(self):
is_linked = _testinternalcapi.interpreter_refcount_linked
get_refcount = _testinternalcapi.get_interpreter_refcount

# A new interpreter will start out not linked, with a refcount of 0.
interpid = create()
interpid = _testinternalcapi.new_interpreter()
self.add_interp_cleanup(interpid)
linked = is_linked(interpid)
refcount = get_refcount(interpid)

self.assertFalse(linked)
self.assertEqual(refcount, 0)

def test_linked_lifecycle_never_linked(self):
exists = _testinternalcapi.interpreter_exists
is_linked = _testinternalcapi.interpreter_refcount_linked
get_refcount = _testinternalcapi.get_interpreter_refcount
incref = _testinternalcapi.interpreter_incref
decref = _testinternalcapi.interpreter_decref

interpid = _testinternalcapi.new_interpreter()
self.add_interp_cleanup(interpid)

# Incref will not automatically link it.
incref(interpid)
self.assertFalse(
is_linked(interpid))
self.assertEqual(
1, get_refcount(interpid))

# It isn't linked so it isn't destroyed.
decref(interpid)
self.assertTrue(
exists(interpid))
self.assertFalse(
is_linked(interpid))
self.assertEqual(
0, get_refcount(interpid))

with self.subTest('never linked'):
interpid = create()
def test_linked_lifecycle_link_unlink(self):
exists = _testinternalcapi.interpreter_exists
is_linked = _testinternalcapi.interpreter_refcount_linked
link = _testinternalcapi.link_interpreter_refcount
unlink = _testinternalcapi.unlink_interpreter_refcount

interpid = _testinternalcapi.new_interpreter()
self.add_interp_cleanup(interpid)

# Incref will not automatically link it.
incref(interpid)
self.assertFalse(
is_linked(interpid))
self.assertEqual(
1, get_refcount(interpid))
# Linking at refcount 0 does not destroy the interpreter.
link(interpid)
self.assertTrue(
exists(interpid))
self.assertTrue(
is_linked(interpid))

# It isn't linked so it isn't destroyed.
decref(interpid)
self.assertTrue(
exists(interpid))
self.assertFalse(
is_linked(interpid))
self.assertEqual(
0, get_refcount(interpid))
# Unlinking at refcount 0 does not destroy the interpreter.
unlink(interpid)
self.assertTrue(
exists(interpid))
self.assertFalse(
is_linked(interpid))

with self.subTest('linking/unlinking at refcount 0 does not destroy'):
interpid = create()
def test_linked_lifecycle_link_incref_decref(self):
exists = _testinternalcapi.interpreter_exists
is_linked = _testinternalcapi.interpreter_refcount_linked
link = _testinternalcapi.link_interpreter_refcount
get_refcount = _testinternalcapi.get_interpreter_refcount
incref = _testinternalcapi.interpreter_incref
decref = _testinternalcapi.interpreter_decref

link(interpid)
self.assertTrue(
exists(interpid))
interpid = _testinternalcapi.new_interpreter()
self.add_interp_cleanup(interpid)

unlink(interpid)
self.assertTrue(
exists(interpid))
# Linking it will not change the refcount.
link(interpid)
self.assertTrue(
is_linked(interpid))
self.assertEqual(
0, get_refcount(interpid))

with self.subTest('link -> incref -> decref => destroyed'):
interpid = create()
# Decref with a refcount of 0 is not allowed.
incref(interpid)
self.assertEqual(
1, get_refcount(interpid))

# Linking it will not change the refcount.
link(interpid)
self.assertTrue(
is_linked(interpid))
self.assertEqual(
0, get_refcount(interpid))
# When linked, decref back to 0 destroys the interpreter.
decref(interpid)
self.assertFalse(
exists(interpid))

# Decref with a refcount of 0 is not allowed.
incref(interpid)
self.assertEqual(
1, get_refcount(interpid))
def test_linked_lifecycle_incref_link(self):
is_linked = _testinternalcapi.interpreter_refcount_linked
link = _testinternalcapi.link_interpreter_refcount
get_refcount = _testinternalcapi.get_interpreter_refcount
incref = _testinternalcapi.interpreter_incref

# When linked, decref back to 0 destroys the interpreter.
decref(interpid)
self.assertFalse(
exists(interpid))
interpid = _testinternalcapi.new_interpreter()
self.add_interp_cleanup(interpid)

with self.subTest('linked after incref'):
interpid = create()
incref(interpid)
self.assertEqual(
1, get_refcount(interpid))

incref(interpid)
self.assertEqual(
1, get_refcount(interpid))
# Linking it will not reset the refcount.
link(interpid)
self.assertTrue(
is_linked(interpid))
self.assertEqual(
1, get_refcount(interpid))

# Linking it will not reset the refcount.
link(interpid)
self.assertEqual(
1, get_refcount(interpid))
def test_linked_lifecycle_link_incref_unlink_decref(self):
exists = _testinternalcapi.interpreter_exists
is_linked = _testinternalcapi.interpreter_refcount_linked
link = _testinternalcapi.link_interpreter_refcount
unlink = _testinternalcapi.unlink_interpreter_refcount
get_refcount = _testinternalcapi.get_interpreter_refcount
incref = _testinternalcapi.interpreter_incref
decref = _testinternalcapi.interpreter_decref

with self.subTest('decref to 0 after unlink does not destroy'):
interpid = create()
interpid = _testinternalcapi.new_interpreter()
self.add_interp_cleanup(interpid)

link(interpid)
self.assertTrue(
is_linked(interpid))
link(interpid)
self.assertTrue(
is_linked(interpid))

incref(interpid)
self.assertEqual(
1, get_refcount(interpid))
incref(interpid)
self.assertEqual(
1, get_refcount(interpid))

# Unlinking it will not change the refcount.
unlink(interpid)
self.assertFalse(
is_linked(interpid))
self.assertEqual(
1, get_refcount(interpid))
# Unlinking it will not change the refcount.
unlink(interpid)
self.assertFalse(
is_linked(interpid))
self.assertEqual(
1, get_refcount(interpid))

# When linked, decref back to 0 destroys the interpreter.
decref(interpid)
self.assertTrue(
exists(interpid))
self.assertEqual(
0, get_refcount(interpid))
# Unlinked: decref back to 0 does not destroys the interpreter.
decref(interpid)
self.assertTrue(
exists(interpid))
self.assertEqual(
0, get_refcount(interpid))


class BuiltinStaticTypesTests(unittest.TestCase):
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy