@@ -314,33 +314,26 @@ pymain_start_pyrepl_no_main(void)
314314static int
315315pymain_run_module (const wchar_t * modname ,int set_argv0 )
316316{
317- PyObject * module ,* runpy , * runmodule ,* runargs ,* result ;
317+ PyObject * module ,* runmodule ,* runargs ,* result ;
318318if (PySys_Audit ("cpython.run_module" ,"u" ,modname )< 0 ) {
319319return pymain_exit_err_print ();
320320 }
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" );
327323if (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" );
330325return pymain_exit_err_print ();
331326 }
332327module = PyUnicode_FromWideChar (modname ,wcslen (modname ));
333328if (module == NULL ) {
334329fprintf (stderr ,"Could not convert module name to unicode\n" );
335- Py_DECREF (runpy );
336330Py_DECREF (runmodule );
337331return pymain_exit_err_print ();
338332 }
339333runargs = PyTuple_Pack (2 ,module ,set_argv0 ?Py_True :Py_False );
340334if (runargs == NULL ) {
341335fprintf (stderr ,
342336"Could not create arguments for runpy._run_module_as_main\n" );
343- Py_DECREF (runpy );
344337Py_DECREF (runmodule );
345338Py_DECREF (module );
346339return pymain_exit_err_print ();
@@ -350,7 +343,6 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
350343if (!result && PyErr_Occurred ()== PyExc_KeyboardInterrupt ) {
351344_PyRuntime .signals .unhandled_keyboard_interrupt = 1 ;
352345 }
353- Py_DECREF (runpy );
354346Py_DECREF (runmodule );
355347Py_DECREF (module );
356348Py_DECREF (runargs );
@@ -497,24 +489,22 @@ pymain_run_startup(PyConfig *config, int *exitcode)
497489static int
498490pymain_run_interactive_hook (int * exitcode )
499491{
500- PyObject * sys ,* hook ,* result ;
501- sys = PyImport_ImportModule ("sys" );
502- if (sys == NULL ) {
503- gotoerror ;
504- }
505-
506- hook = PyObject_GetAttrString (sys ,"__interactivehook__" );
507- Py_DECREF (sys );
492+ PyObject * hook = PyImport_ImportModuleAttrString ("sys" ,
493+ "__interactivehook__" );
508494if (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+ gotoerror ;
511501 }
512502
513503if (PySys_Audit ("cpython.run_interactivehook" ,"O" ,hook )< 0 ) {
514504 gotoerror ;
515505 }
516506
517- result = _PyObject_CallNoArgs (hook );
507+ PyObject * result = _PyObject_CallNoArgs (hook );
518508Py_DECREF (hook );
519509if (result == NULL ) {
520510 gotoerror ;