/// <summary> /// Initialize the runtime... /// </summary> internal static void Initialize() { if (Interop.Py_IsInitialized() == 0) { Interop.Py_Initialize(); } if (Interop.PyEval_ThreadsInitialized() == 0) { Interop.PyEval_InitThreads(); } IntPtr op; IntPtr dict; if (IsPython3) { op = Interop.PyImport_ImportModule("builtins"); dict = Interop.PyObject_GetAttrString(op, "__dict__"); } else // Python2 { dict = Interop.PyImport_GetModuleDict(); op = Interop.PyDict_GetItemString(dict, "__builtin__"); } PyNotImplemented = Interop.PyObject_GetAttrString(op, "NotImplemented"); PyBaseObjectType = Interop.PyObject_GetAttrString(op, "object"); PyModuleType = PyObject_Type(op); PyNone = Interop.PyObject_GetAttrString(op, "None"); PyTrue = Interop.PyObject_GetAttrString(op, "True"); PyFalse = Interop.PyObject_GetAttrString(op, "False"); PyBoolType = PyObject_Type(PyTrue); PyNoneType = PyObject_Type(PyNone); PyTypeType = PyObject_Type(PyNoneType); op = Interop.PyObject_GetAttrString(dict, "keys"); PyMethodType = PyObject_Type(op); XDecref(op); // For some arcane reason, builtins.__dict__.__setitem__ is *not* // a wrapper_descriptor, even though dict.__setitem__ is. // // object.__init__ seems safe, though. op = Interop.PyObject_GetAttrString(PyBaseObjectType, "__init__"); PyWrapperDescriptorType = PyObject_Type(op); XDecref(op); #if PYTHON3 XDecref(dict); #endif op = PyString_FromString("string"); PyStringType = PyObject_Type(op); XDecref(op); op = PyUnicode_FromString("unicode"); PyUnicodeType = PyObject_Type(op); XDecref(op); #if PYTHON3 op = Interop.PyBytes_FromString("bytes"); PyBytesType = PyObject_Type(op); XDecref(op); #endif op = Interop.PyTuple_New(0); PyTupleType = PyObject_Type(op); XDecref(op); op = Interop.PyList_New(0); PyListType = PyObject_Type(op); XDecref(op); op = Interop.PyDict_New(); PyDictType = PyObject_Type(op); XDecref(op); op = PyInt_FromInt32(0); PyIntType = PyObject_Type(op); XDecref(op); op = Interop.PyLong_FromLong(0); PyLongType = PyObject_Type(op); XDecref(op); op = Interop.PyFloat_FromDouble(0); PyFloatType = PyObject_Type(op); XDecref(op); #if PYTHON3 PyClassType = IntPtr.Zero; PyInstanceType = IntPtr.Zero; #elif PYTHON2 IntPtr s = Interop.PyString_FromString("_temp"); IntPtr d = Interop.PyDict_New(); IntPtr c = Interop.PyClass_New(IntPtr.Zero, d, s); PyClassType = Interop.PyObject_Type(c); IntPtr i = Interop.PyInstance_New(c, IntPtr.Zero, IntPtr.Zero); PyInstanceType = Interop.PyObject_Type(i); XDecref(s); XDecref(i); XDecref(c); XDecref(d); #endif Error = new IntPtr(-1); IntPtr dllLocal = IntPtr.Zero; if (Interop.GetDllName() != "__Internal") { if (OS.IsLinux) { dllLocal = NativeMethods_Linux.LoadLibrary(Interop.GetDllName()); } else if (OS.IsOSX) { dllLocal = NativeMethods_OSX.LoadLibrary(Interop.GetDllName()); } else if (OS.IsWindows) { dllLocal = NativeMethods_Windows.LoadLibrary(Interop.GetDllName()); } } if (OS.IsLinux) { _PyObject_NextNotImplemented = NativeMethods_Linux.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented"); } else if (OS.IsOSX) { _PyObject_NextNotImplemented = NativeMethods_OSX.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented"); } else if (OS.IsWindows) { _PyObject_NextNotImplemented = NativeMethods_Windows.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented"); } if (OS.IsWindows && dllLocal != IntPtr.Zero) { NativeMethods_Windows.FreeLibrary(dllLocal); } // Initialize modules that depend on the runtime class. AssemblyManager.Initialize(); PyCLRMetaType = MetaType.Initialize(); Exceptions.Initialize(); ImportHook.Initialize(); // Need to add the runtime directory to sys.path so that we // can find built-in assemblies like System.Data, et. al. string rtdir = RuntimeEnvironment.GetRuntimeDirectory(); IntPtr path = Interop.PySys_GetObject("path"); IntPtr item = PyString_FromString(rtdir); Interop.PyList_Append(path, item); XDecref(item); AssemblyManager.UpdatePath(); }