Skip to content

Commit a60d4e5

Browse files
committed
Enhance tests
Make sure that Py_RunMain() didn't call Py_Exit()
1 parent aa83a6a commit a60d4e5

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

Include/errcode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// the parser only returns E_EOF when it hits EOF immediately, and it
77
// never returns E_OK.
88
//
9-
// The public PyRun_InteractiveOneObjectEx() function can return E_EOF,
9+
// The public PyRun_InteractiveOneObject() function can return E_EOF,
1010
// same as its variants:
1111
//
1212
// * PyRun_InteractiveOneObject()

Lib/test/test_embed.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -597,24 +597,30 @@ def _nogil_filtered_err(err: str, mod_name: str) -> str:
597597
]
598598
return "\n".join(filtered_err_lines)
599599

600+
def check_program_exitcode(self, *args, check_stderr=True, **kwargs):
601+
out, err = self.run_embedded_interpreter(*args, **kwargs)
602+
self.assertEqual(out.rstrip(), 'ok! Py_RunMain() returned 123')
603+
if check_stderr:
604+
self.assertEqual(err, '')
605+
600606
def test_init_run_main_code_exitcode(self):
601607
code = CODE_EXITCODE_123
602-
self.run_embedded_interpreter("test_init_run_main_code_exitcode",
603-
code)
608+
self.check_program_exitcode("test_init_run_main_code_exitcode", code)
604609

605610
def test_init_run_main_script_exitcode(self):
606611
with tempfile.TemporaryDirectory() as tmpdir:
607612
filename = os.path.join(tmpdir, 'script.py')
608613
with open(filename, 'w') as fp:
609614
fp.write(CODE_EXITCODE_123)
610615

611-
self.run_embedded_interpreter("test_init_run_main_script_exitcode",
612-
filename)
616+
self.check_program_exitcode("test_init_run_main_script_exitcode",
617+
filename)
613618

614619
def test_init_run_main_interactive_exitcode(self):
615620
code = CODE_EXITCODE_123
616-
self.run_embedded_interpreter("test_init_run_main_interactive_exitcode",
617-
input=code)
621+
self.check_program_exitcode("test_init_run_main_interactive_exitcode",
622+
input=code,
623+
check_stderr=False)
618624

619625
def test_init_run_main_module_exitcode(self):
620626
with tempfile.TemporaryDirectory() as tmpdir:
@@ -625,8 +631,8 @@ def test_init_run_main_module_exitcode(self):
625631

626632
env = dict(os.environ)
627633
env['PYTHONPATH'] = tmpdir
628-
self.run_embedded_interpreter("test_init_run_main_module_exitcode",
629-
modname, env=env)
634+
self.check_program_exitcode("test_init_run_main_module_exitcode",
635+
modname, env=env)
630636

631637

632638
def config_dev_mode(preconfig, config):

Programs/_testembed.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,9 @@ static int test_init_run_main_exitcode(Py_ssize_t argc, wchar_t * const *argv)
20372037
return 1;
20382038
}
20392039

2040+
// If Py_RunMain() calls Py_Exit(), this message is not written to stdout
2041+
printf("ok! Py_RunMain() returned 123\n");
2042+
20402043
return 0;
20412044
}
20422045

0 commit comments

Comments
 (0)