Skip to content

Commit 39fc2a4

Browse files
committed
Add pymain_error()
1 parent afc9b24 commit 39fc2a4

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

Modules/main.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ pymain_init(const _PyArgv *args)
8080

8181
/* --- pymain_run_python() ---------------------------------------- */
8282

83+
// See also pyrun_error()
84+
static void
85+
pymain_error(const char *msg)
86+
{
87+
assert(strchr(msg, '\n') == NULL);
88+
fprintf(stderr, "python: %s\n", msg);
89+
}
90+
8391
static int
8492
pymain_check_signals(void)
8593
{
@@ -312,13 +320,13 @@ pymain_run_pyrepl(int pythonstartup)
312320

313321
PyObject *pyrepl = PyImport_ImportModule("_pyrepl.main");
314322
if (pyrepl == NULL) {
315-
fprintf(stderr, "Could not import _pyrepl.main\n");
323+
pymain_error("Could not import _pyrepl.main");
316324
goto error;
317325
}
318326

319327
console = PyObject_GetAttrString(pyrepl, "interactive_console");
320328
if (console == NULL) {
321-
fprintf(stderr, "Could not access _pyrepl.main.interactive_console\n");
329+
pymain_error("Could not access _pyrepl.main.interactive_console");
322330
goto error;
323331
}
324332

@@ -379,20 +387,20 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
379387
runmodule = PyImport_ImportModuleAttrString("runpy",
380388
"_run_module_as_main");
381389
if (runmodule == NULL) {
382-
fprintf(stderr, "Could not import runpy._run_module_as_main\n");
390+
pymain_error("Could not import runpy._run_module_as_main");
383391
goto error;
384392
}
385393

386394
module = PyUnicode_FromWideChar(modname, -1);
387395
if (module == NULL) {
388-
fprintf(stderr, "Could not convert module name to unicode\n");
396+
pymain_error("Could not convert module name to unicode");
389397
goto error;
390398
}
391399

392400
runargs = _PyTuple_FromPair(module, set_argv0 ? Py_True : Py_False);
393401
if (runargs == NULL) {
394-
fprintf(stderr,
395-
"Could not create arguments for runpy._run_module_as_main\n");
402+
pymain_error("Could not create arguments "
403+
"for runpy._run_module_as_main");
396404
goto error;
397405
}
398406

@@ -593,7 +601,7 @@ pymain_set_inspect(PyConfig *config, int inspect, int *exitcode)
593601
PyObject *value = PyLong_FromLong(inspect);
594602
if (value == NULL || PyConfig_Set("inspect", value) < 0) {
595603
Py_XDECREF(value);
596-
fprintf(stderr, "Could not set the inspect flag\n");
604+
pymain_error("Could not set the inspect flag");
597605
return pymain_err_print(exitcode);
598606
}
599607
else {
@@ -626,7 +634,7 @@ _pymain_run_repl(PyConfig *config, int startup)
626634
}
627635

628636
if (!PyErr_ExceptionMatches(PyExc_ModuleNotFoundError)) {
629-
fprintf(stderr, "Could not import _pyrepl.main\n");
637+
pymain_error("Could not import _pyrepl.main");
630638
goto error;
631639
}
632640
PyErr_Clear();

Python/pythonrun.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ _PyRun_File(FILE *fp, PyObject *filename, int start, PyObject *globals,
6060
PyObject *locals, int closeit, PyCompilerFlags *flags);
6161

6262

63+
// See also pymain_error()
64+
static void
65+
pyrun_error(const char *msg)
66+
{
67+
assert(strchr(msg, '\n') == NULL);
68+
fprintf(stderr, "python: %s\n", msg);
69+
}
70+
71+
6372
PyObject*
6473
_PyRun_AnyFile(FILE *fp, PyObject *filename, int closeit,
6574
PyCompilerFlags *flags)
@@ -496,7 +505,7 @@ _PyRun_SimpleFile(FILE *fp, PyObject *filename, int closeit,
496505
}
497506
if (!has_file) {
498507
if (PyDict_SetItemString(dict, "__file__", filename) < 0) {
499-
fprintf(stderr, "python: failed to set __main__.__file__\n");
508+
pyrun_error("failed to set __main__.__file__");
500509
goto done;
501510
}
502511
set_file_name = 1;
@@ -516,12 +525,12 @@ _PyRun_SimpleFile(FILE *fp, PyObject *filename, int closeit,
516525

517526
pyc_fp = Py_fopen(filename, "rb");
518527
if (pyc_fp == NULL) {
519-
fprintf(stderr, "python: Can't reopen .pyc file\n");
528+
pyrun_error("Can't reopen .pyc file");
520529
goto done;
521530
}
522531

523532
if (set_main_loader(dict, filename, "SourcelessFileLoader") < 0) {
524-
fprintf(stderr, "python: failed to set __main__.__loader__\n");
533+
pyrun_error("failed to set __main__.__loader__");
525534
fclose(pyc_fp);
526535
goto done;
527536
}
@@ -530,7 +539,7 @@ _PyRun_SimpleFile(FILE *fp, PyObject *filename, int closeit,
530539
/* When running from stdin, leave __main__.__loader__ alone */
531540
if ((!PyUnicode_Check(filename) || !PyUnicode_EqualToUTF8(filename, "<stdin>")) &&
532541
set_main_loader(dict, filename, "SourceFileLoader") < 0) {
533-
fprintf(stderr, "python: failed to set __main__.__loader__\n");
542+
pyrun_error("failed to set __main__.__loader__");
534543
goto done;
535544
}
536545
res = _PyRun_File(fp, filename, Py_file_input, dict, dict,
@@ -541,7 +550,7 @@ _PyRun_SimpleFile(FILE *fp, PyObject *filename, int closeit,
541550
done:
542551
if (set_file_name) {
543552
if (PyDict_PopString(dict, "__file__", NULL) < 0) {
544-
fprintf(stderr, "python: failed to delete __main__.__file__\n");
553+
pyrun_error("failed to delete __main__.__file__");
545554
Py_CLEAR(res);
546555
}
547556
}

0 commit comments

Comments
 (0)