示例#1
0
        public PythonAPI(PythonVersion pyVer)
        {
            string dllName = pyVer.ToString().ToLower();
            _pythonLibrary = new UnmanagedLibrary(dllName);

            Py_Initialize = _pythonLibrary.GetUnmanagedFunction<Action>("Py_Initialize");
            Py_IsInitialized = _pythonLibrary.GetUnmanagedFunction<FuncInt>("Py_IsInitialized");
            Py_Finalize = _pythonLibrary.GetUnmanagedFunction<Action>("Py_Finalize");
            PyEval_InitThreads = _pythonLibrary.GetUnmanagedFunction<Action>("PyEval_InitThreads");
            PyGILState_Ensure = _pythonLibrary.GetUnmanagedFunction<FuncIntPtr>("PyGILState_Ensure");
            PyGILState_Release = _pythonLibrary.GetUnmanagedFunction<ActionIntPtr>("PyGILState_Release");
            PyThreadState_Get = _pythonLibrary.GetUnmanagedFunction<FuncIntPtr>("PyThreadState_Get");

            PyRun_String = _pythonLibrary.GetUnmanagedFunction<FuncStringIntIntPtrIntPtrIntPtr>("PyRun_String");

            PyErr_Print = _pythonLibrary.GetUnmanagedFunction<Action>("PyErr_Print");
            PyErr_Occurred = _pythonLibrary.GetUnmanagedFunction<FuncIntPtr>("PyErr_Occurred");
            PyErr_Clear = _pythonLibrary.GetUnmanagedFunction<Action>("PyErr_Clear");
            PyErr_Fetch = _pythonLibrary.GetUnmanagedFunction<ActionRIntPtrRIntPtrRIntPtr>("PyErr_Fetch");

            PyImport_AddModule = _pythonLibrary.GetUnmanagedFunction<FuncStringIntPtr>("PyImport_AddModule");
            PyModule_GetDict = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyModule_GetDict");

            Py_IncRef = _pythonLibrary.GetUnmanagedFunction<ActionIntPtr>("Py_IncRef");
            Py_DecRef = _pythonLibrary.GetUnmanagedFunction<ActionIntPtr>("Py_DecRef");

            PyObject_HasAttrString = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrStringInt>("PyObject_HasAttrString");
            PyObject_GetAttrString = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrStringIntPtr>("PyObject_GetAttrString");
            PyObject_SetAttrString = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrStringIntPtrInt>("PyObject_SetAttrString");
            PyObject_Str = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyObject_Str");
            PyObject_Compare = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtrInt>("PyObject_Compare");
            PyCallable_Check = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrInt>("PyCallable_Check");
            PyObject_CallObject = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtrIntPtr>("PyObject_CallObject");
            PyObject_Length = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrInt>("PyObject_Length");
            PyObject_Dir = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyObject_Dir");
            PyObject_Type = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyObject_Type");
            PyObject_IsInstance = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtrInt>("PyObject_IsInstance");

            PyString_FromString = _pythonLibrary.GetUnmanagedFunction<FuncStringIntPtr>("PyString_FromString");
            PyString_AsString = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyString_AsString");

            PySys_GetObject = _pythonLibrary.GetUnmanagedFunction<FuncStringIntPtr>("PySys_GetObject");

            PyInt_AsLong = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrInt>("PyInt_AsLong");
            PyLong_AsLongLong = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrLong>("PyLong_AsLongLong");

            PyFloat_AsDouble = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrDouble>("PyFloat_AsDouble");

            PyTuple_New = _pythonLibrary.GetUnmanagedFunction<FuncIntIntPtr>("PyTuple_New");
            PyTuple_SetItem = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntIntPtrInt>("PyTuple_SetItem");

            PyType_Type = _pythonLibrary.GetGlobalVar("PyType_Type");
            PyBaseObject_Type = _pythonLibrary.GetGlobalVar("PyBaseObject_Type");
            PyString_Type = _pythonLibrary.GetGlobalVar("PyString_Type");
            PyUnicode_Type = _pythonLibrary.GetGlobalVar("PyUnicode_Type");
            PyInt_Type = _pythonLibrary.GetGlobalVar("PyInt_Type");
            PyLong_Type = _pythonLibrary.GetGlobalVar("PyLong_Type");
            PyBool_Type = _pythonLibrary.GetGlobalVar("PyBool_Type");
            PyFloat_Type = _pythonLibrary.GetGlobalVar("PyFloat_Type");
        }
示例#2
0
        public static TestEnvironment GetOrCreate(PythonVersion pythonVersion, string testFramework, bool installFramework = true, bool installCoverage = false)
        {
            var testEnvironmentId = $"{pythonVersion.ToString().ToLower()}:{testFramework.ToLower()}:{installFramework.ToString()}:{installCoverage.ToString()}";

            if (_environmentsMap.TryGetValue(testEnvironmentId, out TestEnvironment foundEnv))
            {
                SetDirectories(foundEnv);
                return(foundEnv);
            }

            var env = new TestEnvironment();

            env.TestFramework = testFramework;

            SetDirectories(env);

            switch (testFramework)
            {
            case "Pytest": {
                var envDir   = TestData.GetTempPath();
                var packages = new List <string>();
                if (installFramework)
                {
                    packages.Add("pytest");
                }
                if (installCoverage)
                {
                    packages.Add("coverage");
                }
                pythonVersion.CreateVirtualEnv(envDir, packages);
                env.InterpreterPath = Path.Combine(envDir, "scripts", "python.exe");
            }
            break;

            default:
                if (HasPackage(pythonVersion.PrefixPath, "pytest") || installCoverage)
                {
                    // Create an empty virtual env to ensure we don't accidentally rely on pytest
                    // (which was bug https://github.com/microsoft/PTVS/issues/5454)
                    var envDir   = TestData.GetTempPath();
                    var packages = new List <string>();
                    if (installCoverage)
                    {
                        packages.Add("coverage");
                    }
                    pythonVersion.CreateVirtualEnv(envDir, packages);
                    env.InterpreterPath = Path.Combine(envDir, "scripts", "python.exe");
                }
                else
                {
                    env.InterpreterPath = pythonVersion.InterpreterPath;
                }
                break;
            }

            _environmentsMap.Add(testEnvironmentId, env);

            return(env);
        }