@@ -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 ;
318318 if (PySys_Audit ("cpython.run_module" , "u" , modname ) < 0 ) {
319319 return 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" );
327323 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" );
330325 return pymain_exit_err_print ();
331326 }
332327 module = PyUnicode_FromWideChar (modname , wcslen (modname ));
333328 if (module == NULL ) {
334329 fprintf (stderr , "Could not convert module name to unicode\n" );
335- Py_DECREF (runpy );
336330 Py_DECREF (runmodule );
337331 return pymain_exit_err_print ();
338332 }
339333 runargs = PyTuple_Pack (2 , module , set_argv0 ? Py_True : Py_False );
340334 if (runargs == NULL ) {
341335 fprintf (stderr ,
342336 "Could not create arguments for runpy._run_module_as_main\n" );
343- Py_DECREF (runpy );
344337 Py_DECREF (runmodule );
345338 Py_DECREF (module );
346339 return pymain_exit_err_print ();
@@ -350,7 +343,6 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
350343 if (!result && PyErr_Occurred () == PyExc_KeyboardInterrupt ) {
351344 _PyRuntime .signals .unhandled_keyboard_interrupt = 1 ;
352345 }
353- Py_DECREF (runpy );
354346 Py_DECREF (runmodule );
355347 Py_DECREF (module );
356348 Py_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- goto error ;
504- }
505-
506- hook = PyObject_GetAttrString (sys , "__interactivehook__" );
507- Py_DECREF (sys );
492+ PyObject * hook = PyImport_ImportModuleAttrString ("sys" ,
493+ "__interactivehook__" );
508494 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 ;
511501 }
512502
513503 if (PySys_Audit ("cpython.run_interactivehook" , "O" , hook ) < 0 ) {
514504 goto error ;
515505 }
516506
517- result = _PyObject_CallNoArgs (hook );
507+ PyObject * result = _PyObject_CallNoArgs (hook );
518508 Py_DECREF (hook );
519509 if (result == NULL ) {
520510 goto error ;
0 commit comments