@@ -436,27 +436,9 @@ std::pair<bool, PyObject*> PyFunctionInstance::dispatchFunctionCallToNative(
436436 }
437437 }
438438
439- static PyObject* runtimeModule = ::runtimeModule ();
440-
441- if (!runtimeModule) {
442- throw std::runtime_error (" Internal error: couldn't find typed_python.compiler.runtime" );
443- }
444-
445- PyObject* runtimeClass = PyObject_GetAttrString (runtimeModule, " Runtime" );
446-
447- if (!runtimeClass) {
448- throw std::runtime_error (" Internal error: couldn't find typed_python.compiler.runtime.Runtime" );
449- }
450-
451- PyObject* singleton = PyObject_CallMethod (runtimeClass, " singleton" , " " );
452-
453- if (!singleton) {
454- if (PyErr_Occurred ()) {
455- PyErr_Clear ();
456- }
457-
458- throw std::runtime_error (" Internal error: couldn't call typed_python.compiler.runtime.Runtime.singleton" );
459- }
439+ PyObject* singleton = staticPythonInstance (
440+ " typed_python.compiler.runtime" , " Runtime.singleton()"
441+ );
460442
461443 PyObjectStealer arguments (mapper.extractFunctionArgumentValues ());
462444
@@ -575,29 +557,9 @@ std::pair<bool, PyObject*> PyFunctionInstance::dispatchFunctionCallToCompiledSpe
575557
576558// static
577559PyObject* PyFunctionInstance::createOverloadPyRepresentation (Function* f) {
578- static PyObject* internalsModule = ::internalsModule ();
579-
580- if (!internalsModule) {
581- throw std::runtime_error (" Internal error: couldn't find typed_python.internals" );
582- }
583-
584- static PyObject* funcOverload = PyObject_GetAttrString (internalsModule, " FunctionOverload" );
585-
586- if (!funcOverload) {
587- throw std::runtime_error (" Internal error: couldn't find typed_python.internals.FunctionOverload" );
588- }
589-
590- static PyObject* closureVariableCellLookupSingleton = PyObject_GetAttrString (internalsModule, " CellAccess" );
591-
592- if (!closureVariableCellLookupSingleton) {
593- throw std::runtime_error (" Internal error: couldn't find typed_python.internals.CellAccess" );
594- }
595-
596- static PyObject* funcOverloadArg = PyObject_GetAttrString (internalsModule, " FunctionOverloadArg" );
597-
598- if (!funcOverloadArg) {
599- throw std::runtime_error (" Internal error: couldn't find typed_python.internals.FunctionOverloadArg" );
600- }
560+ PyObject* funcOverload = staticPythonInstance (" typed_python.internals" , " FunctionOverload" );
561+ PyObject* closureVariableCellLookupSingleton = staticPythonInstance (" typed_python.internals" , " CellAccess" );
562+ PyObject* funcOverloadArg = staticPythonInstance (" typed_python.internals" , " FunctionOverloadArg" );
601563
602564 PyObjectStealer overloadTuple (PyTuple_New (f->getOverloads ().size ()));
603565
@@ -1011,27 +973,9 @@ PyObject* PyFunctionInstance::overload(PyObject* funcObj, PyObject* args, PyObje
1011973
1012974/* static */
1013975PyObject* PyFunctionInstance::resultTypeFor (PyObject* funcObj, PyObject* args, PyObject* kwargs) {
1014- static PyObject* runtimeModule = ::runtimeModule ();
1015-
1016- if (!runtimeModule) {
1017- throw std::runtime_error (" Internal error: couldn't find typed_python.compiler.runtime" );
1018- }
1019-
1020- static PyObject* runtimeClass = PyObject_GetAttrString (runtimeModule, " Runtime" );
1021-
1022- if (!runtimeClass) {
1023- throw std::runtime_error (" Internal error: couldn't find typed_python.compiler.runtime.Runtime" );
1024- }
1025-
1026- static PyObject* singleton = PyObject_CallMethod (runtimeClass, " singleton" , " " );
1027-
1028- if (!singleton) {
1029- if (PyErr_Occurred ()) {
1030- PyErr_Clear ();
1031- }
1032-
1033- throw std::runtime_error (" Internal error: couldn't call typed_python.compiler.runtime.Runtime.singleton" );
1034- }
976+ PyObject* singleton = staticPythonInstance (
977+ " typed_python.compiler.runtime" , " Runtime.singleton()"
978+ );
1035979
1036980 if (!kwargs) {
1037981 static PyObject* emptyDict = PyDict_New ();
@@ -1178,19 +1122,7 @@ Function* PyFunctionInstance::convertPythonObjectToFunctionType(
11781122 return memo_it->second ;
11791123 }
11801124
1181- static PyObject* internalsModule = ::internalsModule ();
1182-
1183- if (!internalsModule) {
1184- PyErr_SetString (PyExc_TypeError, " Internal error: couldn't find typed_python.internals" );
1185- return nullptr ;
1186- }
1187-
1188- static PyObject* makeFunctionType = PyObject_GetAttrString (internalsModule, " makeFunctionType" );
1189-
1190- if (!makeFunctionType) {
1191- PyErr_SetString (PyExc_TypeError, " Internal error: couldn't find typed_python.internals.makeFunctionType" );
1192- return nullptr ;
1193- }
1125+ PyObject* makeFunctionType = staticPythonInstance (" typed_python.internals" , " makeFunctionType" );
11941126
11951127 PyObjectStealer args (PyTuple_Pack (2 , name, funcObj));
11961128 PyObjectStealer kwargs (PyDict_New ());
0 commit comments