@@ -314,33 +314,26 @@ pymain_start_pyrepl_no_main(void)
314
314
static int
315
315
pymain_run_module (const wchar_t * modname , int set_argv0 )
316
316
{
317
- PyObject * module , * runpy , * runmodule , * runargs , * result ;
317
+ PyObject * module , * runmodule , * runargs , * result ;
318
318
if (PySys_Audit ("cpython.run_module" , "u" , modname ) < 0 ) {
319
319
return pymain_exit_err_print ();
320
320
}
321
- runpy = PyImport_ImportModule ("runpy" );
322
- if (runpy == NULL ) {
323
- fprintf (stderr , "Could not import runpy module\n" );
324
- return pymain_exit_err_print ();
325
- }
326
- runmodule = PyObject_GetAttrString (runpy , "_run_module_as_main" );
321
+ runmodule = PyImport_ImportModuleAttrString ("runpy" ,
322
+ "_run_module_as_main" );
327
323
if (runmodule == NULL ) {
328
- fprintf (stderr , "Could not access runpy._run_module_as_main\n" );
329
- Py_DECREF (runpy );
324
+ fprintf (stderr , "Could not import runpy._run_module_as_main\n" );
330
325
return pymain_exit_err_print ();
331
326
}
332
327
module = PyUnicode_FromWideChar (modname , wcslen (modname ));
333
328
if (module == NULL ) {
334
329
fprintf (stderr , "Could not convert module name to unicode\n" );
335
- Py_DECREF (runpy );
336
330
Py_DECREF (runmodule );
337
331
return pymain_exit_err_print ();
338
332
}
339
333
runargs = PyTuple_Pack (2 , module , set_argv0 ? Py_True : Py_False );
340
334
if (runargs == NULL ) {
341
335
fprintf (stderr ,
342
336
"Could not create arguments for runpy._run_module_as_main\n" );
343
- Py_DECREF (runpy );
344
337
Py_DECREF (runmodule );
345
338
Py_DECREF (module );
346
339
return pymain_exit_err_print ();
@@ -350,7 +343,6 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
350
343
if (!result && PyErr_Occurred () == PyExc_KeyboardInterrupt ) {
351
344
_PyRuntime .signals .unhandled_keyboard_interrupt = 1 ;
352
345
}
353
- Py_DECREF (runpy );
354
346
Py_DECREF (runmodule );
355
347
Py_DECREF (module );
356
348
Py_DECREF (runargs );
@@ -497,24 +489,22 @@ pymain_run_startup(PyConfig *config, int *exitcode)
497
489
static int
498
490
pymain_run_interactive_hook (int * exitcode )
499
491
{
500
- PyObject * sys , * hook , * result ;
501
- sys = PyImport_ImportModule ("sys" );
502
- if (sys == NULL ) {
503
- goto error ;
504
- }
505
-
506
- hook = PyObject_GetAttrString (sys , "__interactivehook__" );
507
- Py_DECREF (sys );
492
+ PyObject * hook = PyImport_ImportModuleAttrString ("sys" ,
493
+ "__interactivehook__" );
508
494
if (hook == NULL ) {
509
- PyErr_Clear ();
510
- return 0 ;
495
+ if (PyErr_ExceptionMatches (PyExc_AttributeError )) {
496
+ // no sys.__interactivehook__ attribute
497
+ PyErr_Clear ();
498
+ return 0 ;
499
+ }
500
+ goto error ;
511
501
}
512
502
513
503
if (PySys_Audit ("cpython.run_interactivehook" , "O" , hook ) < 0 ) {
514
504
goto error ;
515
505
}
516
506
517
- result = _PyObject_CallNoArgs (hook );
507
+ PyObject * result = _PyObject_CallNoArgs (hook );
518
508
Py_DECREF (hook );
519
509
if (result == NULL ) {
520
510
goto error ;
0 commit comments