|
59 | 59 | if not os.path.isfile(os.path.join(STDLIB_INSTALL, 'os.py')): |
60 | 60 | STDLIB_INSTALL = None |
61 | 61 |
|
| 62 | +CODE_EXITCODE_123 = 'raise SystemExit(123)' |
| 63 | + |
| 64 | + |
62 | 65 | def debug_build(program): |
63 | 66 | program = os.path.basename(program) |
64 | 67 | name = os.path.splitext(program)[0] |
@@ -108,12 +111,16 @@ def run_embedded_interpreter(self, *args, env=None, |
108 | 111 | env = env.copy() |
109 | 112 | env['SYSTEMROOT'] = os.environ['SYSTEMROOT'] |
110 | 113 |
|
111 | | - p = subprocess.Popen(cmd, |
112 | | - stdout=subprocess.PIPE, |
113 | | - stderr=subprocess.PIPE, |
114 | | - universal_newlines=True, |
115 | | - env=env, |
116 | | - cwd=cwd) |
| 114 | + kwargs = dict( |
| 115 | + stdout=subprocess.PIPE, |
| 116 | + stderr=subprocess.PIPE, |
| 117 | + universal_newlines=True, |
| 118 | + env=env, |
| 119 | + cwd=cwd, |
| 120 | + ) |
| 121 | + if input is not None: |
| 122 | + kwargs['stdin'] = subprocess.PIPE |
| 123 | + p = subprocess.Popen(cmd, **kwargs) |
117 | 124 | try: |
118 | 125 | (out, err) = p.communicate(input=input, timeout=timeout) |
119 | 126 | except: |
@@ -530,6 +537,55 @@ def _nogil_filtered_err(err: str, mod_name: str) -> str: |
530 | 537 | ] |
531 | 538 | return "\n".join(filtered_err_lines) |
532 | 539 |
|
| 540 | + def check_program_exitcode(self, *args, check_stderr=True, **kwargs): |
| 541 | + out, err = self.run_embedded_interpreter(*args, **kwargs) |
| 542 | + self.assertEqual(out.rstrip(), 'ok! Py_RunMain() returned 123') |
| 543 | + if check_stderr: |
| 544 | + self.assertEqual(err, '') |
| 545 | + |
| 546 | + def test_init_run_main_code_exitcode(self): |
| 547 | + code = CODE_EXITCODE_123 |
| 548 | + self.check_program_exitcode("test_init_run_main_code_exitcode", code) |
| 549 | + |
| 550 | + def test_init_run_main_script_exitcode(self): |
| 551 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 552 | + filename = os.path.join(tmpdir, 'script.py') |
| 553 | + with open(filename, 'w') as fp: |
| 554 | + fp.write(CODE_EXITCODE_123) |
| 555 | + |
| 556 | + self.check_program_exitcode("test_init_run_main_script_exitcode", |
| 557 | + filename) |
| 558 | + |
| 559 | + def test_init_run_main_interactive_exitcode(self): |
| 560 | + code = CODE_EXITCODE_123 |
| 561 | + self.check_program_exitcode("test_init_run_main_interactive_exitcode", |
| 562 | + input=code, |
| 563 | + check_stderr=False) |
| 564 | + |
| 565 | + def test_init_run_main_startup_exitcode(self): |
| 566 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 567 | + filename = os.path.join(tmpdir, 'startup.py') |
| 568 | + with open(filename, 'x') as fp: |
| 569 | + fp.write(CODE_EXITCODE_123) |
| 570 | + |
| 571 | + env = dict(os.environ) |
| 572 | + env['PYTHONSTARTUP'] = filename |
| 573 | + self.check_program_exitcode("test_init_run_main_interactive_exitcode", |
| 574 | + env=env, |
| 575 | + check_stderr=False) |
| 576 | + |
| 577 | + def test_init_run_main_module_exitcode(self): |
| 578 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 579 | + modname = '_testembed_testmodule' |
| 580 | + filename = os.path.join(tmpdir, modname + '.py') |
| 581 | + with open(filename, 'x', encoding='utf8') as fp: |
| 582 | + fp.write(CODE_EXITCODE_123) |
| 583 | + |
| 584 | + env = dict(os.environ) |
| 585 | + env['PYTHONPATH'] = tmpdir |
| 586 | + self.check_program_exitcode("test_init_run_main_module_exitcode", |
| 587 | + modname, env=env) |
| 588 | + |
533 | 589 |
|
534 | 590 | @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi") |
535 | 591 | class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): |
|
0 commit comments