|
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: |
@@ -554,6 +561,55 @@ def _nogil_filtered_err(err: str, mod_name: str) -> str: |
554 | 561 | ] |
555 | 562 | return "\n".join(filtered_err_lines) |
556 | 563 |
|
| 564 | + def check_program_exitcode(self, *args, check_stderr=True, **kwargs): |
| 565 | + out, err = self.run_embedded_interpreter(*args, **kwargs) |
| 566 | + self.assertEqual(out.rstrip(), 'ok! Py_RunMain() returned 123') |
| 567 | + if check_stderr: |
| 568 | + self.assertEqual(err, '') |
| 569 | + |
| 570 | + def test_init_run_main_code_exitcode(self): |
| 571 | + code = CODE_EXITCODE_123 |
| 572 | + self.check_program_exitcode("test_init_run_main_code_exitcode", code) |
| 573 | + |
| 574 | + def test_init_run_main_script_exitcode(self): |
| 575 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 576 | + filename = os.path.join(tmpdir, 'script.py') |
| 577 | + with open(filename, 'w') as fp: |
| 578 | + fp.write(CODE_EXITCODE_123) |
| 579 | + |
| 580 | + self.check_program_exitcode("test_init_run_main_script_exitcode", |
| 581 | + filename) |
| 582 | + |
| 583 | + def test_init_run_main_interactive_exitcode(self): |
| 584 | + code = CODE_EXITCODE_123 |
| 585 | + self.check_program_exitcode("test_init_run_main_interactive_exitcode", |
| 586 | + input=code, |
| 587 | + check_stderr=False) |
| 588 | + |
| 589 | + def test_init_run_main_startup_exitcode(self): |
| 590 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 591 | + filename = os.path.join(tmpdir, 'startup.py') |
| 592 | + with open(filename, 'x') as fp: |
| 593 | + fp.write(CODE_EXITCODE_123) |
| 594 | + |
| 595 | + env = dict(os.environ) |
| 596 | + env['PYTHONSTARTUP'] = filename |
| 597 | + self.check_program_exitcode("test_init_run_main_interactive_exitcode", |
| 598 | + env=env, |
| 599 | + check_stderr=False) |
| 600 | + |
| 601 | + def test_init_run_main_module_exitcode(self): |
| 602 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 603 | + modname = '_testembed_testmodule' |
| 604 | + filename = os.path.join(tmpdir, modname + '.py') |
| 605 | + with open(filename, 'x', encoding='utf8') as fp: |
| 606 | + fp.write(CODE_EXITCODE_123) |
| 607 | + |
| 608 | + env = dict(os.environ) |
| 609 | + env['PYTHONPATH'] = tmpdir |
| 610 | + self.check_program_exitcode("test_init_run_main_module_exitcode", |
| 611 | + modname, env=env) |
| 612 | + |
557 | 613 |
|
558 | 614 | def config_dev_mode(preconfig, config): |
559 | 615 | preconfig['allocator'] = PYMEM_ALLOCATOR_DEBUG |
|
0 commit comments