Skip to content

bpo-32604: Improve subinterpreter tests. #6914

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 18 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
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
Next Next commit
Add InterpreterID/ChannelID.__str__().
  • Loading branch information
ericsnowcurrently committed May 16, 2018
commit c34ff9070d68deacfead36e233d357283fbb7865
56 changes: 32 additions & 24 deletions Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_subinterpreter(self):
interp = interpreters.create()
out = _run_output(interp, dedent("""
import _xxsubinterpreters as _interpreters
print(int(_interpreters.get_current()))
print(_interpreters.get_current())
"""))
cur = int(out.strip())
_, expected = interpreters.list_all()
Expand All @@ -182,7 +182,7 @@ def test_from_subinterpreter(self):
interp = interpreters.create()
out = _run_output(interp, dedent("""
import _xxsubinterpreters as _interpreters
print(int(_interpreters.get_main()))
print(_interpreters.get_main())
"""))
main = int(out.strip())
self.assertEqual(main, expected)
Expand All @@ -206,7 +206,7 @@ def test_from_subinterpreter(self):
interp = interpreters.create()
out = _run_output(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
if _interpreters.is_running({int(interp)}):
if _interpreters.is_running({interp}):
print(True)
else:
print(False)
Expand Down Expand Up @@ -266,6 +266,10 @@ def test_does_not_exist(self):
with self.assertRaises(RuntimeError):
interpreters.InterpreterID(int(id) + 1) # unforced

def test_str(self):
id = interpreters.InterpreterID(10, force=True)
self.assertEqual(str(id), '10')

def test_repr(self):
id = interpreters.InterpreterID(10, force=True)
self.assertEqual(repr(id), 'InterpreterID(10)')
Expand Down Expand Up @@ -323,7 +327,7 @@ def test_in_subinterpreter(self):
out = _run_output(id1, dedent("""
import _xxsubinterpreters as _interpreters
id = _interpreters.create()
print(int(id))
print(id)
"""))
id2 = int(out.strip())

Expand All @@ -338,7 +342,7 @@ def f():
out = _run_output(id1, dedent("""
import _xxsubinterpreters as _interpreters
id = _interpreters.create()
print(int(id))
print(id)
"""))
id2 = int(out.strip())

Expand Down Expand Up @@ -432,7 +436,7 @@ def test_from_current(self):
script = dedent(f"""
import _xxsubinterpreters as _interpreters
try:
_interpreters.destroy({int(id)})
_interpreters.destroy({id})
except RuntimeError:
pass
""")
Expand All @@ -446,7 +450,7 @@ def test_from_sibling(self):
id2 = interpreters.create()
script = dedent(f"""
import _xxsubinterpreters as _interpreters
_interpreters.destroy({int(id2)})
_interpreters.destroy({id2})
""")
interpreters.run_string(id1, script)

Expand Down Expand Up @@ -854,6 +858,10 @@ def test_does_not_exist(self):
with self.assertRaises(interpreters.ChannelNotFoundError):
interpreters._channel_id(int(cid) + 1) # unforced

def test_str(self):
cid = interpreters._channel_id(10, force=True)
self.assertEqual(str(cid), '10')

def test_repr(self):
cid = interpreters._channel_id(10, force=True)
self.assertEqual(repr(cid), 'ChannelID(10)')
Expand Down Expand Up @@ -900,15 +908,15 @@ def test_ids_global(self):
out = _run_output(id1, dedent("""
import _xxsubinterpreters as _interpreters
cid = _interpreters.channel_create()
print(int(cid))
print(cid)
"""))
cid1 = int(out.strip())

id2 = interpreters.create()
out = _run_output(id2, dedent("""
import _xxsubinterpreters as _interpreters
cid = _interpreters.channel_create()
print(int(cid))
print(cid)
"""))
cid2 = int(out.strip())

Expand Down Expand Up @@ -942,7 +950,7 @@ def test_send_recv_different_interpreters(self):
id1 = interpreters.create()
out = _run_output(id1, dedent(f"""
import _xxsubinterpreters as _interpreters
_interpreters.channel_send({int(cid)}, b'spam')
_interpreters.channel_send({cid}, b'spam')
"""))
obj = interpreters.channel_recv(cid)

Expand Down Expand Up @@ -980,12 +988,12 @@ def f():
import _xxsubinterpreters as _interpreters
while True:
try:
obj = _interpreters.channel_recv({int(cid)})
obj = _interpreters.channel_recv({cid})
break
except _interpreters.ChannelEmptyError:
time.sleep(0.1)
assert(obj == b'spam')
_interpreters.channel_send({int(cid)}, b'eggs')
_interpreters.channel_send({cid}, b'eggs')
"""))
t = threading.Thread(target=f)
t.start()
Expand Down Expand Up @@ -1103,16 +1111,16 @@ def test_multiple_users(self):
id2 = interpreters.create()
interpreters.run_string(id1, dedent(f"""
import _xxsubinterpreters as _interpreters
_interpreters.channel_send({int(cid)}, b'spam')
_interpreters.channel_send({cid}, b'spam')
"""))
out = _run_output(id2, dedent(f"""
import _xxsubinterpreters as _interpreters
obj = _interpreters.channel_recv({int(cid)})
_interpreters.channel_release({int(cid)})
obj = _interpreters.channel_recv({cid})
_interpreters.channel_release({cid})
print(repr(obj))
"""))
interpreters.run_string(id1, dedent(f"""
_interpreters.channel_release({int(cid)})
_interpreters.channel_release({cid})
"""))

self.assertEqual(out.strip(), "b'spam'")
Expand Down Expand Up @@ -1161,7 +1169,7 @@ def test_by_unassociated_interp(self):
interp = interpreters.create()
interpreters.run_string(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
_interpreters.channel_release({int(cid)})
_interpreters.channel_release({cid})
"""))
obj = interpreters.channel_recv(cid)
interpreters.channel_release(cid)
Expand All @@ -1175,8 +1183,8 @@ def test_close_if_unassociated(self):
interp = interpreters.create()
interpreters.run_string(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
obj = _interpreters.channel_send({int(cid)}, b'spam')
_interpreters.channel_release({int(cid)})
obj = _interpreters.channel_send({cid}, b'spam')
_interpreters.channel_release({cid})
"""))

with self.assertRaises(interpreters.ChannelClosedError):
Expand Down Expand Up @@ -1264,21 +1272,21 @@ def test_multiple_users(self):
id2 = interpreters.create()
interpreters.run_string(id1, dedent(f"""
import _xxsubinterpreters as _interpreters
_interpreters.channel_send({int(cid)}, b'spam')
_interpreters.channel_send({cid}, b'spam')
"""))
interpreters.run_string(id2, dedent(f"""
import _xxsubinterpreters as _interpreters
_interpreters.channel_recv({int(cid)})
_interpreters.channel_recv({cid})
"""))
interpreters.channel_close(cid)
with self.assertRaises(interpreters.RunFailedError) as cm:
interpreters.run_string(id1, dedent(f"""
_interpreters.channel_send({int(cid)}, b'spam')
_interpreters.channel_send({cid}, b'spam')
"""))
self.assertIn('ChannelClosedError', str(cm.exception))
with self.assertRaises(interpreters.RunFailedError) as cm:
interpreters.run_string(id2, dedent(f"""
_interpreters.channel_send({int(cid)}, b'spam')
_interpreters.channel_send({cid}, b'spam')
"""))
self.assertIn('ChannelClosedError', str(cm.exception))

Expand Down Expand Up @@ -1315,7 +1323,7 @@ def test_by_unassociated_interp(self):
interp = interpreters.create()
interpreters.run_string(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
_interpreters.channel_close({int(cid)})
_interpreters.channel_close({cid})
"""))
with self.assertRaises(interpreters.ChannelClosedError):
interpreters.channel_recv(cid)
Expand Down
18 changes: 16 additions & 2 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,13 @@ channelid_repr(PyObject *self)
return PyUnicode_FromFormat(fmt, name, cid->id);
}

static PyObject *
channelid_str(PyObject *self)
{
channelid *cid = (channelid *)self;
return PyUnicode_FromFormat("%d", cid->id);
}

PyObject *
channelid_int(PyObject *self)
{
Expand Down Expand Up @@ -1637,7 +1644,7 @@ static PyTypeObject ChannelIDtype = {
0, /* tp_as_mapping */
channelid_hash, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
(reprfunc)channelid_str, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Expand Down Expand Up @@ -1918,6 +1925,13 @@ interpid_repr(PyObject *self)
return PyUnicode_FromFormat("%s(%d)", name, id->id);
}

static PyObject *
interpid_str(PyObject *self)
{
interpid *id = (interpid *)self;
return PyUnicode_FromFormat("%d", id->id);
}

PyObject *
interpid_int(PyObject *self)
{
Expand Down Expand Up @@ -2039,7 +2053,7 @@ static PyTypeObject InterpreterIDtype = {
0, /* tp_as_mapping */
interpid_hash, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
(reprfunc)interpid_str, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
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