|
68 | 68 | if not os.path.isfile(os.path.join(STDLIB_INSTALL, 'os.py')): |
69 | 69 | STDLIB_INSTALL = None |
70 | 70 |
|
| 71 | +CODE_EXITCODE_123 = 'raise SystemExit(123)' |
| 72 | + |
| 73 | + |
71 | 74 | def debug_build(program): |
72 | 75 | program = os.path.basename(program) |
73 | 76 | name = os.path.splitext(program)[0] |
@@ -117,12 +120,16 @@ def run_embedded_interpreter(self, *args, env=None, |
117 | 120 | env = env.copy() |
118 | 121 | env['SYSTEMROOT'] = os.environ['SYSTEMROOT'] |
119 | 122 |
|
120 | | - p = subprocess.Popen(cmd, |
121 | | - stdout=subprocess.PIPE, |
122 | | - stderr=subprocess.PIPE, |
123 | | - universal_newlines=True, |
124 | | - env=env, |
125 | | - cwd=cwd) |
| 123 | + kwargs = dict( |
| 124 | + stdout=subprocess.PIPE, |
| 125 | + stderr=subprocess.PIPE, |
| 126 | + universal_newlines=True, |
| 127 | + env=env, |
| 128 | + cwd=cwd, |
| 129 | + ) |
| 130 | + if input is not None: |
| 131 | + kwargs['stdin'] = subprocess.PIPE |
| 132 | + p = subprocess.Popen(cmd, **kwargs) |
126 | 133 | try: |
127 | 134 | (out, err) = p.communicate(input=input, timeout=timeout) |
128 | 135 | except: |
@@ -565,6 +572,55 @@ def _nogil_filtered_err(err: str, mod_name: str) -> str: |
565 | 572 | ] |
566 | 573 | return "\n".join(filtered_err_lines) |
567 | 574 |
|
| 575 | + def check_program_exitcode(self, *args, check_stderr=True, **kwargs): |
| 576 | + out, err = self.run_embedded_interpreter(*args, **kwargs) |
| 577 | + self.assertEqual(out.rstrip(), 'ok! Py_RunMain() returned 123') |
| 578 | + if check_stderr: |
| 579 | + self.assertEqual(err, '') |
| 580 | + |
| 581 | + def test_init_run_main_code_exitcode(self): |
| 582 | + code = CODE_EXITCODE_123 |
| 583 | + self.check_program_exitcode("test_init_run_main_code_exitcode", code) |
| 584 | + |
| 585 | + def test_init_run_main_script_exitcode(self): |
| 586 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 587 | + filename = os.path.join(tmpdir, 'script.py') |
| 588 | + with open(filename, 'w') as fp: |
| 589 | + fp.write(CODE_EXITCODE_123) |
| 590 | + |
| 591 | + self.check_program_exitcode("test_init_run_main_script_exitcode", |
| 592 | + filename) |
| 593 | + |
| 594 | + def test_init_run_main_interactive_exitcode(self): |
| 595 | + code = CODE_EXITCODE_123 |
| 596 | + self.check_program_exitcode("test_init_run_main_interactive_exitcode", |
| 597 | + input=code, |
| 598 | + check_stderr=False) |
| 599 | + |
| 600 | + def test_init_run_main_startup_exitcode(self): |
| 601 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 602 | + filename = os.path.join(tmpdir, 'startup.py') |
| 603 | + with open(filename, 'x') as fp: |
| 604 | + fp.write(CODE_EXITCODE_123) |
| 605 | + |
| 606 | + env = dict(os.environ) |
| 607 | + env['PYTHONSTARTUP'] = filename |
| 608 | + self.check_program_exitcode("test_init_run_main_interactive_exitcode", |
| 609 | + env=env, |
| 610 | + check_stderr=False) |
| 611 | + |
| 612 | + def test_init_run_main_module_exitcode(self): |
| 613 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 614 | + modname = '_testembed_testmodule' |
| 615 | + filename = os.path.join(tmpdir, modname + '.py') |
| 616 | + with open(filename, 'x', encoding='utf8') as fp: |
| 617 | + fp.write(CODE_EXITCODE_123) |
| 618 | + |
| 619 | + env = dict(os.environ) |
| 620 | + env['PYTHONPATH'] = tmpdir |
| 621 | + self.check_program_exitcode("test_init_run_main_module_exitcode", |
| 622 | + modname, env=env) |
| 623 | + |
568 | 624 |
|
569 | 625 | def config_dev_mode(preconfig, config): |
570 | 626 | preconfig['allocator'] = PYMEM_ALLOCATOR_DEBUG |
|
0 commit comments