internal static void Deinitialize() { _mainModule.Dispose(); _mainModule = null; PythonInterop.Py_DecRef(PythonInterop.Py_None); PythonInterop.Py_DecRef(PythonInterop.Py_True); PythonInterop.Py_DecRef(PythonInterop.Py_False); PythonInterop.Py_DecRef(PythonInterop.PyType_BaseObject); PythonInterop.Py_DecRef(PythonInterop.PyType_Module); PythonInterop.Py_DecRef(PythonInterop.PyType_Class); PythonInterop.Py_DecRef(PythonInterop.PyType_Instance); PythonInterop.Py_DecRef(PythonInterop.PyType_Method); #if PY_MAJOR_VERSION_3 PythonInterop.Py_DecRef(PythonInterop.PyType_Bytes); #endif #if PY_MAJOR_VERSION_2 PythonInterop.Py_DecRef(PythonInterop.PyType_Int); PythonInterop.Py_DecRef(PythonInterop.PyType_LongLong); PythonInterop.Py_DecRef(PythonInterop.PyType_UnsignedLongLong); #endif PythonInterop.Py_DecRef(PythonInterop.PyType_Unicode); PythonInterop.Py_DecRef(PythonInterop.PyType_String); PythonInterop.Py_DecRef(PythonInterop.PyType_Tuple); PythonInterop.Py_DecRef(PythonInterop.PyType_List); PythonInterop.Py_DecRef(PythonInterop.PyType_Dict); PythonInterop.Py_DecRef(PythonInterop.PyType_Long); PythonInterop.Py_DecRef(PythonInterop.PyType_UnsignedLong); PythonInterop.Py_DecRef(PythonInterop.PyType_Float); PythonInterop.Py_DecRef(PythonInterop.PyType_Bool); PythonInterop.Py_DecRef(PythonInterop.PyType_None); PythonInterop.Py_DecRef(PythonInterop.PyType_Type); foreach (var nameAndModuleKeyValuePair in Modules) { nameAndModuleKeyValuePair.Value.Dispose(); } Modules.Clear(); PythonInterop.Py_Finalize(); if (PythonInterop.Py_IsInitialized() != 0) { throw new PythonException("Python is not de-initialized."); } }
public static object load_module(CodeContext /*!*/ context, string name, PythonFile file, string filename, PythonTuple /*!*/ description) { if (description == null) { throw PythonOps.TypeError("load_module() argument 4 must be 3-item sequence, not None"); } else if (description.__len__() != 3) { throw PythonOps.TypeError("load_module() argument 4 must be sequence of length 3, not {0}", description.__len__()); } PythonContext pythonContext = PythonContext.GetContext(context); // already loaded? do reload() PythonModule module = pythonContext.GetModuleByName(name); if (module != null) { Importer.ReloadModule(context, module, file); return(module); } int type = PythonContext.GetContext(context).ConvertToInt32(description[2]); switch (type) { case PythonSource: return(LoadPythonSource(pythonContext, name, file, filename)); case CBuiltin: return(LoadBuiltinModule(context, name)); case PackageDirectory: return(LoadPackageDirectory(pythonContext, name, filename)); default: throw PythonOps.TypeError("don't know how to import {0}, (type code {1}", name, type); } }
public int RunFile(string fileName) { Parser p = Parser.FromFile(engineContext.SystemState, context.CopyWithNewSourceFile(fileName)); Stmt s = p.ParseFileInput(); string moduleName = "tmp" + counter++; PythonModule mod = OutputGenerator.GenerateModule(engineContext.SystemState, p.CompilerContext, s, moduleName); foreach (KeyValuePair <SymbolId, object> name in engineContext.Module.__dict__.SymbolAttributes) { mod.SetAttr(engineContext, name.Key, name.Value); } mod.SetAttr(engineContext, SymbolTable.File, fileName); try { mod.Initialize(); } catch (PythonSystemExit e) { return(e.GetExitCode(engineContext)); } return(0); }
private DocumentEntry CreateDocument(ModuleCreationOptions mco) { IDocument document; switch (mco.ModuleType) { case ModuleType.Compiled when TryAddModulePath(mco): document = new CompiledPythonModule(mco.ModuleName, ModuleType.Compiled, mco.FilePath, mco.Stub, _services); break; case ModuleType.CompiledBuiltin: document = new CompiledBuiltinPythonModule(mco.ModuleName, mco.Stub, _services); break; case ModuleType.User: TryAddModulePath(mco); document = new PythonModule(mco, _services); break; case ModuleType.Library when TryAddModulePath(mco): document = new PythonModule(mco, _services); break; default: throw new InvalidOperationException($"CreateDocument does not support module type {mco.ModuleType}"); } var entry = new DocumentEntry(document); _documentsByUri[document.Uri] = entry; _documentsByName[mco.ModuleName] = entry; _services.GetService <IPythonAnalyzer>().InvalidateAnalysis(document); return(entry); }
static void Main(string[] args) { Python.Py_NoSiteFlag = true; Python.Py_Initialize(); PythonModule testModule = PythonModule.FromFile(@"..\..\..\Test.py"); dynamic testDynamic = testModule; { Console.WriteLine("a: " + testModule.GetAttribute("a")); Console.WriteLine("b: " + testModule.GetAttribute("b")); Console.WriteLine(); } { Console.WriteLine("a: " + testDynamic.a); Console.WriteLine("b: " + testDynamic.b); testDynamic.c = (int)testDynamic.a + (int)testDynamic.b; testDynamic.show("c:"); } Console.ReadKey(true); }
public string[] GetModuleFilenames() { List <string> res = new List <string>(); PythonDictionary dict = (object)_engine.GetSysModule().GetVariable("modules") as PythonDictionary; if (dict != null) { foreach (var kvp in dict) { string key = kvp.Key as string; PythonModule module = kvp.Value as PythonModule; if (key != null && module != null) { var modDict = module.Get__dict__(); object file; if (modDict.TryGetValue("__file__", out file) && file != null) { res.Add(key); } } } } return(res.ToArray()); }
public static object Eval(ICallerContext context, string expression, IDictionary <object, object> globals, object locals) { if (locals != null && PythonOperator.IsMappingType(context, locals) == Ops.FALSE) { throw Ops.TypeError("locals must be mapping"); } CompilerContext cc = context.CreateCompilerContext(); Parser p = Parser.FromString(context.SystemState, cc, expression.TrimStart(' ', '\t')); Expr e = p.ParseTestListAsExpression(); PythonModule mod = new PythonModule("<eval>", globals, context.SystemState, null, context.ContextFlags); if (Options.FastEval) //!!! experimenting with a HUGE (>100x) performance boost to simple evals { return(e.Evaluate(new NameEnv(mod, locals))); } else { Stmt s = new ReturnStmt(e); FrameCode fc = OutputGenerator.GenerateSnippet(cc, s, false); return(fc.Run(new Frame(mod, globals, locals))); } }
public InterpFunction(string[] argNames, object[] defaults, Statement body, PythonModule globals) { this.argNames = argNames; this.defaults = defaults; this.body = body; this.globals = globals; }
public static IDictionary <object, object> Globals(ICallerContext context) { PythonModule mod = context.Module; return((IDictionary <object, object>)mod.__dict__); }
public void Reset() { module = new PythonModule(); containers.Clear(); containers.Push(module); }
public FunctionX(PythonModule globals, string name, CallTargetN target, string[] argNames, object[] defaults, FunctionAttributes flags) : base(globals, name, target, argNames, defaults) { this.flags = flags; nparams = argNames.Length; if ((flags & FunctionAttributes.KeywordDictionary) != 0) { extraArgs++; nparams--; kwDictPos = nparams; } if ((flags & FunctionAttributes.ArgumentList) != 0) { extraArgs++; nparams--; argListPos = nparams; } Debug.Assert(defaults.Length <= nparams); }
public AD7Module(PythonModule debuggedModule) { this.DebuggedModule = debuggedModule; }
public static object reload(CodeContext /*!*/ context, PythonModule scope) { return(Builtin.reload(context, scope)); }
internal static IPythonModule ImportModule(string modulePath, string moduleName) { PythonModule module; var moduleHash = modulePath + "/" + moduleName; if (Modules.TryGetValue(moduleHash, out module)) { return module; } module = new PythonModule(modulePath, moduleName); Modules.Add(moduleHash, module); return module; }
/// <summary> /// Creates the module which will act as a class so it can hold any methods defined in the module. /// </summary> void CreateModule() { if (module == null) { module = new PythonModule(compilationUnit); } }
protected PythonFunction(PythonModule globals, string name, string[] argNames, object[] defaults) { this.name = name; this.argNames = argNames; this.defaults = defaults; Debug.Assert(defaults.Length <= argNames.Length); if (name.IndexOf('#') > 0) { // dynamic method, strip the trailing id... this.name = name.Substring(0, name.IndexOf('#')); } else { this.name = name; } this.module = globals; this.FunctionCode = new FunctionCode(this); }
internal static void Initialize() { PythonInterop.Py_Initialize(); if (PythonInterop.Py_IsInitialized() != 1) { throw new PythonException("Python is not initialized."); } PythonInterop.PyEval_InitThreads(); PythonInterop.PySys_SetArgvEx(0, new[] { Environment.CurrentDirectory }, 0); _mainModule = new PythonModule("__main__"); var pyBuiltIn = PythonInterop.PyDict_GetItemString(((PythonDictionary)_mainModule.Dictionary).NativePythonObject, "__builtins__"); PythonInterop.Py_None = PythonInterop.PyObject_GetAttrString(pyBuiltIn, "None"); PythonInterop.Py_True = PythonInterop.PyObject_GetAttrString(pyBuiltIn, "True"); PythonInterop.Py_False = PythonInterop.PyObject_GetAttrString(pyBuiltIn, "False"); PythonInterop.PyType_Module = PythonInterop.PyObject_Type(pyBuiltIn); PythonInterop.PyType_BaseObject = PythonInterop.PyObject_GetAttrString(pyBuiltIn, "object"); PythonInterop.PyType_Bool = PythonInterop.PyObject_Type(PythonInterop.Py_True); PythonInterop.PyType_None = PythonInterop.PyObject_Type(PythonInterop.Py_None); PythonInterop.PyType_Type = PythonInterop.PyObject_Type(PythonInterop.PyType_None); var op = PythonInterop.PyObject_GetAttrString(((PythonDictionary)_mainModule.Dictionary).NativePythonObject, "keys"); PythonInterop.PyType_Method = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); #if PY_MAJOR_VERSION_3 op = PythonInterop.PyBytes_FromString("string"); PythonInterop.PyType_Bytes = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); #endif op = PythonInterop.PyString_FromString("string"); PythonInterop.PyType_String = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = PythonInterop.PyUnicode_FromString("unicode"); PythonInterop.PyType_Unicode = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = PythonInterop.PyTuple_New(0); PythonInterop.PyType_Tuple = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = PythonInterop.PyList_New(0); PythonInterop.PyType_List = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = PythonInterop.PyDict_New(); PythonInterop.PyType_Dict = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); #if PY_MAJOR_VERSION_2 op = PythonInterop.PyInt_FromLong(0); PythonInterop.PyType_Int = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = PythonInterop.PyLong_FromLongLong(0); PythonInterop.PyType_LongLong = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = PythonInterop.PyLong_FromUnsignedLongLong(0); PythonInterop.PyType_UnsignedLongLong = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); #endif op = PythonInterop.PyLong_FromLong(0); PythonInterop.PyType_Long = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = PythonInterop.PyLong_FromUnsignedLong(0); PythonInterop.PyType_UnsignedLong = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = PythonInterop.PyFloat_FromDouble(0); PythonInterop.PyType_Float = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = _mainModule.Execute<IntPtr>( "class TempClass:" + "\n" + " \"\"\"Temporary class to get Python class type\"\"\"" + "\n" + "tempClass = TempClass", "tempClass"); PythonInterop.PyType_Class = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); op = _mainModule.Execute<IntPtr>( "class TempClass:" + "\n" + " \"\"\"Temporary class to get Python class type\"\"\"" + "\n" + "tempClass = TempClass()", "tempClass"); PythonInterop.PyType_Instance = PythonInterop.PyObject_Type(op); PythonInterop.Py_DecRef(op); var pyErrors = _mainModule.Execute( "tempBaseException = BaseException()\n" + "tempSyntaxError = SyntaxError()\n" + "tempIndentationError = IndentationError()\n" + "tempTabError = TabError()\n" + "tempImportError = ImportError()\n", new Dictionary<string, Type> { {"tempBaseException", typeof(IntPtr) }, {"tempSyntaxError", typeof(IntPtr) }, {"tempIndentationError", typeof(IntPtr) }, {"tempTabError", typeof(IntPtr) }, {"tempImportError", typeof(IntPtr) } }); PythonInterop.PyType_BaseException = PythonInterop.PyObject_Type((IntPtr)pyErrors[0]); PythonInterop.PyType_SyntaxError = PythonInterop.PyObject_Type((IntPtr)pyErrors[1]); PythonInterop.PyType_IndentationError = PythonInterop.PyObject_Type((IntPtr)pyErrors[2]); PythonInterop.PyType_TabError = PythonInterop.PyObject_Type((IntPtr)pyErrors[3]); PythonInterop.PyType_ImportError = PythonInterop.PyObject_Type((IntPtr)pyErrors[4]); foreach (var pyError in pyErrors) { PythonInterop.Py_DecRef((IntPtr)pyError); } }
public Function5(PythonModule globals, string name, CallTarget5 target, string[] argNames, object[] defaults) : base(globals, name, argNames, defaults) { this.target = target; }
public SnippetModuleRunner(string name, SystemState state) { module = new PythonModule(name, dict, state, this.Initialize); frame = new Frame(module); }
PyEval_GetBuiltins() { PythonModule __builtin__ = this.GetModule("__builtin__"); return(this.Store(__builtin__.Get__dict__())); }
ExecInModule(string code, PythonModule module) { SourceUnit script = this.python.CreateSnippet(code, SourceCodeKind.Statements); script.Execute(new Scope(module.Get__dict__())); }
public PythonVirtualEnv Load(LambdaRuntimeInfo runtimeInfo) { var sitePackages = this.GetSitePackagesDir(runtimeInfo); foreach (var dist in Directory.EnumerateDirectories( sitePackages, "*.dist-info", SearchOption.TopDirectoryOnly)) { // RECORD file tells us where to find the code and is a 3 field header-less CSV var recordFile = Path.Combine(dist, "RECORD"); // Name comes from the dist-info directory name // Package names always have hyphens, not underscores var module = new PythonModule { Name = PackageNameRegex.Match(Path.GetFileName(dist)).Groups["packageName"].Value .Replace('_', '-') }; if (File.Exists(recordFile)) { var paths = new HashSet <string>(); // Pull all items from RECORD file into a set to get a unique list of what to include, ignoring dist-info files, Windows .pyd files and scripts placed in bin foreach (var path in File.ReadAllLines(recordFile) .Select(line => line.Split(',').First().Split('/').First()) .Where(path => !(path.EndsWith(".dist-info") || path.EndsWith(".pyd")) && path != ".." && path != "__pycache__")) { paths.Add(Path.Combine(sitePackages, path)); } // TODO: Warn if a binary is a Windows DLL. Not likely to be good in Lambda (linux) if (paths.Count == 0) { throw new PackagerException( $"Found no content for package {module.Name}"); } foreach (var p in paths) { module.Paths.Add(Path.Combine(sitePackages, p)); } } else { throw new FileNotFoundException( "Unable to determine package location - cannot find RECORD file", recordFile); } // METADATA file tells us any dependencies with Requires-Dist records. var metadataFile = Path.Combine(dist, "METADATA"); if (File.Exists(metadataFile)) { foreach (var dependency in File.ReadAllLines(metadataFile) .Where(l => l.StartsWith("Requires-Dist:"))) { var includeDependency = true; if (dependency.Contains(";")) { var expression = dependency.Split(';').Last(); var r = this.parser.Parse(expression); if (r.IsError) { var errorMessage = string.Join( Environment.NewLine, r.Errors.Select(e => e.ErrorMessage)); throw new PackagerException( $"Error parsing: {expression}\nIf this expression is valid, please raise an issue.\n{errorMessage}"); } // Set variables according to targeted Python version. this.pep508Variables["python_version"] = runtimeInfo.RuntimeVersion; this.pep508Variables["python_full_version"] = runtimeInfo.RuntimeVersion; var evaluation = r.Result.Evaluate(new ExpressionContext(this.pep508Variables)); includeDependency = (bool?)evaluation ?? throw new PackagerException( $"Error evaluating: {expression}\nIf this expression is valid, please raise an issue."); } if (includeDependency) { module.Dependencies.Add(RequirementsRegex.Match(dependency).Groups["dependency"].Value); } } } this.modules.Add(module); } return(this); }
PyModule_GetDict(IntPtr modulePtr) { PythonModule module = (PythonModule)this.Retrieve(modulePtr); return(this.Store(module.Get__dict__())); }
ExecInModule(string code, PythonModule module) { SourceUnit script = this.python.CreateSnippet(code, SourceCodeKind.Statements); script.Execute(new Scope(new DictionaryWrapper((IDictionary <object, object>)module.Get__dict__()))); }
AddModule(string name, PythonModule module) { PythonDictionary modules = (PythonDictionary)this.python.SystemState.Get__dict__()["modules"]; modules[name] = module; }
ICompletionDataList GenerateCompletionData(CodeCompletionContext completionContext, PythonModule module, TextEditorData editor, char completionChar) { var triggerWord = GetTriggerWord(editor, completionContext); // Its annoying when the data is poped up during an assignment such as: // abc = _ if (completionChar == '=' && String.IsNullOrEmpty(triggerWord)) { return(null); } var triggerLine = editor.GetLineText(completionContext.TriggerLine); // if completionChar is ' ' and it is not a known completion type // that we can handle, return as early as possible if (completionChar == ' ') { if (!triggerWord.Contains('.') && !triggerLine.StartsWith("class") && !triggerLine.StartsWith("def") && !triggerLine.StartsWith("from") && !triggerLine.StartsWith("import")) { return(null); } } // "self." if (module != null && triggerWord == "self" && completionChar == '.') { var klass = GetClass(module, completionContext.TriggerLine); if (klass == null) { return(null); // nothing to complete, self not in a class } return(new CompletionDataList(SelfDotCompletionData(klass))); } var inFrom = triggerLine.StartsWith("from "); var inClass = triggerLine.StartsWith("class ") || (triggerLine.StartsWith("class") && completionChar == ' '); var inDef = triggerLine.StartsWith("def ") || (triggerLine.StartsWith("def") && completionChar == ' '); var parts = triggerLine.Split(new char [] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // "from blah " if (inFrom && parts.Length == 2 && completionChar == ' ') { return(new CompletionDataList(new CompletionData[] { new CompletionData("import") })); } // "from blah import " else if (inFrom && parts.Length > 2) { triggerWord = parts [1] + "."; return(new CompletionDataList( from ParserItem item in m_site.Database.Find(triggerWord) where !item.FullName.Substring(triggerWord.Length).Contains('.') select CreateCompletionData(item, triggerWord)) ); } // if we are in a new class line and not to '(' yet // we cannot complete anything at this time, finish now if (inClass && parts.Length < 2) { return(null); } // if we are in a new def line, the only time we can complete // is after an equal '='. so ignore space trigger if (inDef && completionChar == ' ') { return(null); } else if (inDef && completionChar == '=') { triggerWord = ""; } if (inClass) { if (completionChar == '(') { triggerWord = ""; } else { triggerWord = triggerLine.Substring(triggerLine.LastIndexOf('(') + 1); } } // limit the depth of search to number of "." in trigger // "xml." has depth of 1 so anything matching ^xml. and no more . with match int depth = 0; foreach (var c in triggerWord) { if (c == '.') { depth++; } } // anything in the sqlite store if (!String.IsNullOrEmpty(triggerWord)) { // todo: try to complete on class/module/func/attr data return(new CompletionDataList( from ParserItem item in m_site.Database.Find(triggerWord, ParserItemType.Any, depth) select CreateCompletionData(item, triggerWord)) ); } ParserItemType itemType = String.IsNullOrEmpty(triggerWord) ? ParserItemType.Module : ParserItemType.Any; return(new CompletionDataList( from ParserItem item in m_site.Database.Find("", itemType, depth) select CreateCompletionData(item, triggerWord)) ); }
public CustomPythonWalker() { module = new PythonModule(); containers = new Stack <PythonContainerNode> (); containers.Push(module); }
internal IMember MakeObject(object obj) { if (obj == null) { return(null); } lock (this) { IMember res; if (!_members.TryGetValue(obj, out res)) { PythonModule mod = obj as PythonModule; if (mod != null) { // FIXME: name object name; if (!mod.Get__dict__().TryGetValue("__name__", out name) || !(name is string)) { name = ""; } _members[obj] = res = new IronPythonModule(this, mod, (string)name); } PythonType type = obj as PythonType; if (type != null) { _members[obj] = res = GetTypeFromType(type.__clrtype__()); } BuiltinFunction func = obj as BuiltinFunction; if (func != null) { _members[obj] = res = new IronPythonBuiltinFunction(this, func); } BuiltinMethodDescriptor methodDesc = obj as BuiltinMethodDescriptor; if (methodDesc != null) { _members[obj] = res = new IronPythonBuiltinMethodDescriptor(this, methodDesc); } ReflectedField field = obj as ReflectedField; if (field != null) { return(new IronPythonField(this, field)); } ReflectedProperty prop = obj as ReflectedProperty; if (prop != null) { _members[obj] = res = new IronPythonProperty(this, prop); } ReflectedExtensionProperty extProp = obj as ReflectedExtensionProperty; if (extProp != null) { _members[obj] = res = new IronPythonExtensionProperty(this, extProp); } NamespaceTracker ns = obj as NamespaceTracker; if (ns != null) { _members[obj] = res = new IronPythonNamespace(this, ns); } Method method = obj as Method; if (method != null) { _members[obj] = res = new IronPythonGenericMember(this, method, PythonMemberType.Method); } var classMethod = obj as ClassMethodDescriptor; if (classMethod != null) { _members[obj] = res = new IronPythonGenericMember(this, classMethod, PythonMemberType.Method); } var typeSlot = obj as PythonTypeTypeSlot; if (typeSlot != null) { _members[obj] = res = new IronPythonGenericMember(this, typeSlot, PythonMemberType.Property); } ReflectedEvent eventObj = obj as ReflectedEvent; if (eventObj != null) { return(new IronPythonEvent(this, eventObj)); } if (res == null) { var genericTypeSlot = obj as PythonTypeSlot; if (genericTypeSlot != null) { _members[obj] = res = new IronPythonGenericMember(this, genericTypeSlot, PythonMemberType.Property); } } TypeGroup tg = obj as TypeGroup; if (tg != null) { _members[obj] = res = new PythonObject <TypeGroup>(this, tg); } var attrType = (obj != null) ? obj.GetType() : typeof(DynamicNull); if (attrType == typeof(bool) || attrType == typeof(int) || attrType == typeof(Complex) || attrType == typeof(string) || attrType == typeof(long) || attrType == typeof(double) || attrType.IsEnum || obj == null) { _members[obj] = res = new IronPythonConstant(this, obj); } if (res == null) { Debug.Assert(!(obj is bool)); _members[obj] = res = new PythonObject <object>(this, obj); } } return(res); } }
public ModuleLoadedEventArgs(PythonModule module) { _module = module; }