Skip to content

Commit acf7188

Browse files
committed
Cleanup tests
1 parent d84f18e commit acf7188

2 files changed

Lines changed: 52 additions & 27 deletions

File tree

Lib/test/test_embed.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -598,38 +598,34 @@ def _nogil_filtered_err(err: str, mod_name: str) -> str:
598598
return "\n".join(filtered_err_lines)
599599

600600
def test_init_run_main_code_exitcode(self):
601-
env = dict(os.environ)
602-
env['TEST_CODE'] = CODE_EXITCODE_123
601+
code = CODE_EXITCODE_123
603602
self.run_embedded_interpreter("test_init_run_main_code_exitcode",
604-
env=env)
603+
code)
605604

606605
def test_init_run_main_script_exitcode(self):
607-
filename = os.path.abspath(os_helper.TESTFN + '.py')
608-
with open(filename, 'x', encoding='utf8') as fp:
609-
fp.write(CODE_EXITCODE_123)
610-
self.addCleanup(os_helper.unlink, filename)
606+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py') as tmp:
607+
tmp.write(CODE_EXITCODE_123)
608+
tmp.flush()
611609

612-
env = dict(os.environ)
613-
env['TEST_SCRIPT'] = filename
614-
self.run_embedded_interpreter("test_init_run_main_script_exitcode",
615-
env=env)
610+
self.run_embedded_interpreter("test_init_run_main_script_exitcode",
611+
tmp.name)
616612

617613
def test_init_run_main_interactive_exitcode(self):
618614
code = CODE_EXITCODE_123
619615
self.run_embedded_interpreter("test_init_run_main_interactive_exitcode",
620616
input=code)
621617

622618
def test_init_run_main_module_exitcode(self):
623-
modname = '_testembed_testmodule'
624-
filename = os.path.abspath(modname + '.py')
625-
with open(filename, 'x', encoding='utf8') as fp:
626-
fp.write(CODE_EXITCODE_123)
627-
self.addCleanup(os_helper.unlink, filename)
628-
629-
env = dict(os.environ)
630-
env['TEST_MODULE'] = modname
631-
self.run_embedded_interpreter("test_init_run_main_module_exitcode",
632-
env=env)
619+
with tempfile.TemporaryDirectory() as tmpdir:
620+
modname = '_testembed_testmodule'
621+
filename = os.path.join(tmpdir, modname + '.py')
622+
with open(filename, 'x', encoding='utf8') as fp:
623+
fp.write(CODE_EXITCODE_123)
624+
625+
env = dict(os.environ)
626+
env['PYTHONPATH'] = tmpdir
627+
self.run_embedded_interpreter("test_init_run_main_module_exitcode",
628+
modname, env=env)
633629

634630

635631
def config_dev_mode(preconfig, config):

Programs/_testembed.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,23 @@ static void error(const char *msg)
6464
}
6565

6666

67+
static void error_fmt(const char *format, ...)
68+
{
69+
va_list vargs;
70+
va_start(vargs, format);
71+
fprintf(stderr, "ERROR: ");
72+
vfprintf(stderr, format, vargs);
73+
fprintf(stderr, "\n");
74+
va_end(vargs);
75+
fflush(stderr);
76+
}
77+
78+
6779
static wchar_t* py_getenv(const char *name)
6880
{
6981
const char *env = getenv(name);
7082
if (env == NULL) {
71-
fprintf(stderr, "ERROR: need %s env var\n", name);
83+
error_fmt("need %s env var", name);
7284
return NULL;
7385
}
7486

@@ -81,6 +93,24 @@ static wchar_t* py_getenv(const char *name)
8193
}
8294

8395

96+
static wchar_t* get_cmdline_arg(const char *arg_name)
97+
{
98+
if (main_argc < 3) {
99+
const char *test = main_argv[1];
100+
fprintf(stderr, "usage: %s %s %s\n", PROGRAM, test, arg_name);
101+
return NULL;
102+
}
103+
const char *arg = main_argv[2];
104+
105+
wchar_t *result = Py_DecodeLocale(arg, NULL);
106+
if (result == NULL) {
107+
error_fmt("failed to decode %s command line argument", arg_name);
108+
return NULL;
109+
}
110+
return result;
111+
}
112+
113+
84114
static void config_set_string(PyConfig *config, wchar_t **config_str, const wchar_t *str)
85115
{
86116
PyStatus status = PyConfig_SetString(config, config_str, str);
@@ -2003,8 +2033,7 @@ static int test_init_run_main_exitcode(Py_ssize_t argc, wchar_t * const *argv)
20032033

20042034
int exitcode = Py_RunMain();
20052035
if (exitcode != 123) {
2006-
fprintf(stderr, "ERROR: Py_RunMain() returned %i, expected 123\n",
2007-
exitcode);
2036+
error_fmt("Py_RunMain() returned %i, expected 123", exitcode);
20082037
return 1;
20092038
}
20102039

@@ -2014,7 +2043,7 @@ static int test_init_run_main_exitcode(Py_ssize_t argc, wchar_t * const *argv)
20142043

20152044
static int test_init_run_main_script_exitcode(void)
20162045
{
2017-
wchar_t *filename = py_getenv("TEST_SCRIPT");
2046+
wchar_t *filename = get_cmdline_arg("FILENAME");
20182047
if (filename == NULL) {
20192048
return 1;
20202049
}
@@ -2029,7 +2058,7 @@ static int test_init_run_main_script_exitcode(void)
20292058

20302059
static int test_init_run_main_module_exitcode(void)
20312060
{
2032-
wchar_t *module = py_getenv("TEST_MODULE");
2061+
wchar_t *module = get_cmdline_arg("MODULE");
20332062
if (module == NULL) {
20342063
return 1;
20352064
}
@@ -2051,7 +2080,7 @@ static int test_init_run_main_interactive_exitcode(void)
20512080

20522081
static int test_init_run_main_code_exitcode(void)
20532082
{
2054-
wchar_t *code = py_getenv("TEST_CODE");
2083+
wchar_t *code = get_cmdline_arg("CODE");
20552084
if (code == NULL) {
20562085
return 1;
20572086
}

0 commit comments

Comments
 (0)