public static GILState GIL() { if (!PythonEngine.IsInitialized) { PythonEngine.Initialize(); } return(new GILState()); }
public static int Main(string[] args) { string [] cmd = Environment.GetCommandLineArgs(); PythonEngine.Initialize(); int i = Runtime.Py_Main(cmd.Length, cmd); PythonEngine.Shutdown(); return(i); }
public static int Main(string[] args) { // reference the static assemblyLoader to stop it being optimized away AssemblyLoader a = assemblyLoader; string[] cmd = Environment.GetCommandLineArgs(); PythonEngine.Initialize(); int i = Runtime.Py_Main(cmd.Length, cmd); PythonEngine.Shutdown(); return(i); }
public static int Main(string[] args) { // Only net40 is capable to safely inject python.runtime.dll into resources. #if NET40 // reference the static assemblyLoader to stop it being optimized away AssemblyLoader a = assemblyLoader; #endif string[] cmd = Environment.GetCommandLineArgs(); PythonEngine.Initialize(); int i = Runtime.Py_Main(cmd.Length, cmd); PythonEngine.Shutdown(); return(i); }
private object ExecutePythonScriptCode(string code) { Python.Included.Installer.SetupPython().Wait(); if (!PythonEngine.IsInitialized) { PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); } using (Py.GIL()) { var result = Scope.Eval(code); return(CPythonEvaluator.OutputMarshaler.Marshal(result)); } }
public void Dispose() { if (Scope != null) { if (!PythonEngine.IsInitialized) { PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); } try { using (Py.GIL()) { Scope.Dispose(); } } catch {} } }
public DSCPythonCodeCompletionProviderCore() { VariableTypes = new Dictionary <string, Type>(); ImportedTypes = new Dictionary <string, Type>(); ClrModules = new HashSet <string>(); BadStatements = new Dictionary <string, int>(); Guid pythonScopeGUID = Guid.NewGuid(); uniquePythonScopeName += pythonScopeGUID.ToString(); // Special case for python variables defined as null ImportedTypes["None"] = null; BasicVariableTypes = new List <Tuple <Regex, Type> >(); BasicVariableTypes.Add(Tuple.Create(STRING_VARIABLE, typeof(PyString))); BasicVariableTypes.Add(Tuple.Create(DOUBLE_VARIABLE, typeof(PyFloat))); BasicVariableTypes.Add(Tuple.Create(INT_VARIABLE, typeof(PyInt))); BasicVariableTypes.Add(Tuple.Create(LIST_VARIABLE, typeof(PyList))); BasicVariableTypes.Add(Tuple.Create(DICT_VARIABLE, typeof(PyDict))); if (!PythonEngine.IsInitialized) { PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); } try { using (Py.GIL()) { if (GlobalScope == null) { GlobalScope = CreateGlobalScope(); } if (Scope == null) { Scope = CreateUniquePythonScope(); } var assemblies = AppDomain.CurrentDomain.GetAssemblies(); // Determine if the Revit API is available in the given context if (assemblies.Any(x => x.GetName().Name == "RevitAPI")) { // Try to load Revit Type for autocomplete try { var revitImports = "import clr\nclr.AddReference('RevitAPI')\nclr.AddReference('RevitAPIUI')\nfrom Autodesk.Revit.DB import *\nimport Autodesk\n"; Scope.Exec(revitImports); ClrModules.Add("RevitAPI"); ClrModules.Add("RevitAPIUI"); } catch { Log("Failed to load Revit types for autocomplete. Python autocomplete will not see Autodesk namespace types."); } } // Determine if the ProtoGeometry is available in the given context if (assemblies.Any(x => x.GetName().Name == "ProtoGeometry")) { // Try to load ProtoGeometry Type for autocomplete try { var libGImports = "import clr\nclr.AddReference('ProtoGeometry')\nfrom Autodesk.DesignScript.Geometry import *\n"; Scope.Exec(libGImports); ClrModules.Add("ProtoGeometry"); } catch (Exception e) { Log(e.ToString()); Log("Failed to load ProtoGeometry types for autocomplete. Python autocomplete will not see Autodesk namespace types."); } } // PythonNet evaluates basic types as Python native types: // Ex: a = "" // type(a) will have the value {class str} and not the clr System.String // // Populate the basic types after python is initialized // These instance types will correspond to Python basic types (no clr) // ex. PyString("") => Python's {class str} // PyInt(0) => Python's {class int} basicPyObjects = new Dictionary <Type, PyObject>(); basicPyObjects[typeof(PyString)] = new PyString(""); basicPyObjects[typeof(PyFloat)] = new PyFloat(0); basicPyObjects[typeof(PyInt)] = new PyInt(0); basicPyObjects[typeof(PyList)] = new PyList(); basicPyObjects[typeof(PyDict)] = new PyDict(); } } catch {} }
/// <summary> /// Generate completion data for the specified text, while import the given types into the /// scope and discovering variable assignments. /// </summary> /// <param name="code">The code to parse</param> /// <param name="expand">Determines if the entire namespace should be used</param> /// <returns>Return a list of IExternalCodeCompletionData </returns> public override IExternalCodeCompletionData[] GetCompletionData(string code, bool expand = false) { IEnumerable <PythonCodeCompletionDataCore> items = new List <PythonCodeCompletionDataCore>(); if (!PythonEngine.IsInitialized) { PythonEngine.Initialize(); PythonEngine.BeginAllowThreads(); } try { using (Py.GIL()) { if (code.Contains("\"\"\"")) { code = StripDocStrings(code); } UpdateImportedTypes(code); UpdateVariableTypes(code); // Possibly use hindley-milner in the future // If expand param is true use the entire namespace from the line of code // Else just return the last name of the namespace string name = expand ? GetLastNameSpace(code) : GetLastName(code); if (!String.IsNullOrEmpty(name)) { try { // Attempt to get type using naming Type type = expand ? TryGetTypeFromFullName(name) : TryGetType(name); // CLR type if (type != null) { items = EnumerateMembers(type, name).Select(x => new PythonCodeCompletionDataCore(x.Item1, x.Item2, x.Item3, x.Item4, this)); } // Variable assignment types else if (VariableTypes.TryGetValue(name, out type)) { if (basicPyObjects.TryGetValue(type, out PyObject basicObj)) { items = EnumerateMembers(basicObj, name).Select(x => new PythonCodeCompletionDataCore(x.Item1, x.Item2, x.Item3, x.Item4, this)); } else { items = EnumerateMembers(type, name).Select(x => new PythonCodeCompletionDataCore(x.Item1, x.Item2, x.Item3, x.Item4, this)); } } else { // Try to find the corresponding PyObject from the python environment // Most types should be successfully retrieved at this stage var pyObj = LookupObject(name); if (pyObj != null) { items = EnumerateMembers(pyObj, name).Select(x => new PythonCodeCompletionDataCore(x.Item1, x.Item2, x.Item3, x.Item4, this)); } } } catch (Exception ex) { Log(ex.ToString()); } } } } catch {} // If unable to find matching results and expand was set to false, // try again using the full namespace (set expand to true) if (!items.Any() && !expand) { return(GetCompletionData(code, true)); } return(items.ToArray()); }