public int ConvertPyFileToDescriptor(PythonFile pyFile) { FileStream stream = InappropriateReflection.StreamFromPythonFile(pyFile); SafeHandle safeHandle = stream.SafeFileHandle; IntPtr handle = safeHandle.DangerousGetHandle(); return Unmanaged._open_osfhandle(handle, 0); }
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"); } 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.Scope); return module.Scope; } 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); } }
static Analyzer() { state = new SystemState(); PythonFile empty = new PythonFile(System.IO.Stream.Null, System.Text.Encoding.Default, "rw"); state.__stderr__ = empty; state.__stdin__ = empty; state.__stdout__ = empty; }
/// <summary> /// Performs sys's initialization /// It is in it's own function so we can do reload(sys). On reload(sys), most of the attributes need to be /// reset. The following are left as they are - argv, exc_type, modules, path, path_hooks, path_importer_cache, ps1, ps2. /// </summary> public void Initialize() { if (__dict__ == null) { __dict__ = new FieldIdDict(); // These fields do not get reset on "reload(sys)" argv = Ops.MakeList(); modules = new Dict(); path = List.Make(); ps1 = Ops.ToPython(">>> "); ps2 = Ops.ToPython("... "); __stdin__ = new PythonFile(Console.OpenStandardInput(), Console.InputEncoding, "<stdin>", "r"); __stdout__ = new PythonFile(Options.UnbufferedStdOutAndError ? Console.OpenStandardOutput(0) : Console.OpenStandardOutput(), Console.OutputEncoding, "<stdout>", "w"); __stderr__ = new PythonFile(Options.UnbufferedStdOutAndError ? Console.OpenStandardError(0) : Console.OpenStandardError(), Console.OutputEncoding, "<stderr>", "w"); } __dict__[SymbolTable.Name] = "sys"; stdin = __stdin__; stdout = __stdout__; stderr = __stderr__; // removed from dictionary after the first call to set it. MethodInfo mi = typeof(SystemState).GetMethod("setdefaultencodingImpl", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); BuiltinMethodDescriptor descr = (BuiltinMethodDescriptor) new ReflectedMethod("setdefaultencoding", mi, FunctionType.PythonVisible | FunctionType.Method).GetDescriptor(); __dict__[SymbolTable.SetDefaultEncoding] = descr.GetAttribute(this, this); DefaultEncoding = Encoding.ASCII; byteorder = BitConverter.IsLittleEndian ? "little" : "big"; copyright = "Copyright (c) Microsoft Corporation. All rights reserved."; hexversion = 0x02040000; maxint = Int32.MaxValue; maxunicode = (int)ushort.MaxValue; platform = Ops.ToPython("cli"); version_info = Tuple.MakeTuple(2, 4); // !!! These fields do need to be reset on "reload(sys)". However, the initial value is specified by the // engine elsewhere. For now, we initialize them just once to some default value if (version == null) { version = IronPython.Hosting.PythonEngine.VersionString; warnoptions = List.Make(); executable = ""; } }
private static IEnumerator<byte> FileEnumerator(PythonFile/*!*/ file) { for (; ; ) { string data = file.read(1); if (data.Length == 0) { yield break; } yield return (byte)data[0]; } }
public static PythonFile GetFileFromId(int id) { PythonFile pf = mapping.GetObjectFromId(id); if (pf != null) { return(pf); } throw Ops.OSError("Bad file descriptor"); }
public override void Flush() { // avoid creating a site in the common case PythonFile pf = Sink as PythonFile; if (pf != null) { pf.flush(); return; } if (PythonOps.HasAttr(_context.SharedContext, Sink, "flush")) { PythonOps.Invoke(_context.SharedContext, Sink, "flush"); } }
public static void Remove(PythonFile pf) { mapping.RemoveOnObject(pf); }
public void tofile(PythonFile f) { f.write(tostring()); }
public PythonFileStreamContentProvider(PythonFile file) { _file = file; }
public PythonFileStreamContentProvider(PythonFile file) { _file = file; }
public static object load_source(CodeContext/*!*/ context, string/*!*/ name, string/*!*/ pathname, PythonFile/*!*/ file) { if (name == null) throw PythonOps.TypeError("load_source() argument 1 must be string, not None"); if (pathname == null) throw PythonOps.TypeError("load_source() argument 2 must be string, not None"); if (pathname == null) throw PythonOps.TypeError("load_source() argument 3 must be file, not None"); return LoadPythonSource(PythonContext.GetContext(context), name, file, pathname); }
public static int GetIdFromFile(PythonFile pf) { return(mapping.GetIdFromObject(pf)); }
public void FromFile(PythonFile f, int n) { int bytesNeeded = n * ItemSize; string bytes = f.Read(bytesNeeded); if (bytes.Length < bytesNeeded) throw Ops.EofError("file not large enough"); FromString(bytes); }
public static object load_compiled(string name, string pathname, PythonFile file) { return null; }
/// <summary> /// Get a logger which logs to the console verbose logs /// </summary> /// <param name="file">A python file object to use for output</param> /// <returns>The new logger</returns> public static Logger GetVerboseLogger(PythonFile file) { Logger l = GetLogger(file); l.LogLevel = Logger.LogEntryType.All; return l; }
static void ret_LogEntryAdded(PythonFile file, object sender, Logger.LogEntryAddedEventArgs e) { string text = e.LogEntry.Text; if (e.LogEntry.ExceptionObject != null) { text = e.LogEntry.ExceptionObject.ToString(); } file.write(String.Format("[{0}] {1} {2}: {3}\n", e.LogEntry.EntryType, e.LogEntry.Timestamp, e.LogEntry.SourceName, text)); }
/// <summary> /// Get a logger which logs to the console /// </summary> /// <param name="file">A python file object to use for output</param> /// <returns>The new logger</returns> public static Logger GetLogger(PythonFile file) { Logger ret = new Logger(); ret.LogEntryAdded += (sender, e) => ret_LogEntryAdded(file, sender, e); return ret; }
StoreTyped(PythonFile obj) { IntPtr ptr = this.allocator.Alloc((uint)Marshal.SizeOf(typeof(PyFileObject))); CPyMarshal.Zero(ptr, Marshal.SizeOf(typeof(PyFileObject))); CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", 1); CPyMarshal.WritePtrField(ptr, typeof(PyObject), "ob_type", this.PyFile_Type); CPyMarshal.WriteIntField(ptr, typeof(PyFileObject), "f_fp", -2); CPyMarshal.WritePtrField(ptr, typeof(PyFileObject), "f_name", this.Store(obj.name)); CPyMarshal.WritePtrField(ptr, typeof(PyFileObject), "f_mode", this.Store(obj.mode)); this.map.Associate(ptr, obj); return ptr; }
public static int GetIdFromFile(PythonFile pf) { return mapping.GetIdFromObject(pf); }
public void write(PythonFile f) { tofile(f); }
public void ToFile(PythonFile f) { f.Write(ConvertToString()); }
public static object Open(ICallerContext context, string filename, int flag, int mode) { try { FileStream fs = File.Open(filename, FileModeFromFlags(flag), FileAccessFromFlags(flag)); string mode2; if (fs.CanRead && fs.CanWrite) mode2 = "w+"; else if (fs.CanWrite) mode2 = "w"; else mode2 = "r"; PythonFile pf = new PythonFile(fs, context.SystemState.DefaultEncoding, filename, mode2, false); return PythonFileManager.AddToStrongMapping(pf); } catch (Exception e) { throw ToPythonException(e); } }
public void Write(PythonFile f) { ToFile(f); }
public static object load_dynamic(string name, string pathname, PythonFile file) { return null; }
public static void dump(object value, PythonFile/*!*/ file, int version) { if (file == null) throw PythonOps.TypeError("expected file, found None"); file.write(dumps(value, version)); }
private static Scope/*!*/ LoadPythonSource(PythonContext/*!*/ context, string/*!*/ name, PythonFile/*!*/ file, string/*!*/ fileName) { SourceUnit sourceUnit = context.CreateSnippet(file.read(), String.IsNullOrEmpty(fileName) ? null : fileName, SourceCodeKind.File); return context.CompileModule(fileName, name, sourceUnit, ModuleOptions.Initialize).Scope; }
public static int AddToWeakMapping(PythonFile pf) { return mapping.WeakAdd(pf); }
internal static object ReloadModule(CodeContext /*!*/ context, PythonModule /*!*/ module, PythonFile file) { PythonContext pc = context.LanguageContext; // We created the module and it only contains Python code. If the user changes // __file__ we'll reload from that file. // built-in module: if (!(module.GetFile() is string fileName)) { ReloadBuiltinModule(context, module); return(module); } string name = module.GetName() as string; if (name != null) { List path = null; // find the parent module and get it's __path__ property int dotIndex = name.LastIndexOf('.'); if (dotIndex != -1) { PythonModule parentModule; path = GetParentPathAndModule(context, name.Substring(0, dotIndex), out parentModule); } object reloaded; if (TryLoadMetaPathModule(context, module.GetName() as string, path, out reloaded) && reloaded != null) { return(module); } List sysPath; if (context.LanguageContext.TryGetSystemPath(out sysPath)) { object ret = ImportFromPathHook(context, name, name, sysPath, null); if (ret != null) { return(ret); } } } SourceUnit sourceUnit; if (file != null) { sourceUnit = pc.CreateSourceUnit(new PythonFileStreamContentProvider(file), fileName, file.Encoding, SourceCodeKind.File); } else { if (!pc.DomainManager.Platform.FileExists(fileName)) { throw PythonOps.SystemError("module source file not found"); } sourceUnit = pc.CreateFileUnit(fileName, pc.DefaultEncoding, SourceCodeKind.File); } pc.GetScriptCode(sourceUnit, name, ModuleOptions.None, Compiler.CompilationMode.Lookup).Run(module.Scope); return(module); }
public static void Remove(PythonFile pf) { mapping.RemoveOnObject(pf); }
internal static object ReloadModule(CodeContext/*!*/ context, PythonModule/*!*/ module, PythonFile file) { PythonContext pc = PythonContext.GetContext(context); // We created the module and it only contains Python code. If the user changes // __file__ we'll reload from that file. string fileName = module.GetFile() as string; // built-in module: if (fileName == null) { ReloadBuiltinModule(context, module); return module; } string name = module.GetName() as string; if (name != null) { List path = null; // find the parent module and get it's __path__ property int dotIndex = name.LastIndexOf('.'); if (dotIndex != -1) { PythonModule parentModule; path = GetParentPathAndModule(context, name.Substring(0, dotIndex), out parentModule); } object reloaded; if (TryLoadMetaPathModule(context, module.GetName() as string, path, out reloaded) && reloaded != null) { return module; } List sysPath; if (PythonContext.GetContext(context).TryGetSystemPath(out sysPath)) { object ret = ImportFromPathHook(context, name, name, sysPath, null); if (ret != null) { return ret; } } } SourceUnit sourceUnit; if (file != null) { sourceUnit = pc.CreateSourceUnit(new PythonFileStreamContentProvider(file), fileName, file.Encoding, SourceCodeKind.File); } else { if (!pc.DomainManager.Platform.FileExists(fileName)) { throw PythonOps.SystemError("module source file not found"); } sourceUnit = pc.CreateFileUnit(fileName, pc.DefaultEncoding, SourceCodeKind.File); } pc.GetScriptCode(sourceUnit, name, ModuleOptions.None).Run(module.Scope); return module; }
public void fromfile(PythonFile f, int n) { int bytesNeeded = n * itemsize; string bytes = f.read(bytesNeeded); if (bytes.Length < bytesNeeded) throw PythonOps.EofError("file not large enough"); fromstring(bytes); }
public static void dump(object value, PythonFile/*!*/ file) { dump(value, file, version); }
public static void dump(object value, PythonFile/*!*/ file) { dump(value, file, null); }
public static object load(PythonFile/*!*/ file) { if (file == null) throw PythonOps.TypeError("expected file, found None"); return MarshalOps.GetObject(FileEnumerator (file)); }
/// <summary> /// Performs sys's initialization /// It is in it's own function so we can do reload(sys). On reload(sys), most of the attributes need to be /// reset. The following are left as they are - argv, exc_type, modules, path, path_hooks, path_importer_cache, ps1, ps2. /// </summary> internal void Initialize() { if (__dict__ == null) { __dict__ = new FieldIdDict(); // These fields do not get reset on "reload(sys)" argv = Ops.MakeList(); modules = new Dict(); modules["sys"] = this; modules["__builtin__"] = Importer.MakePythonModule(this, "__builtin__", TypeCache.Builtin); path = List.Make(); ps1 = ">>> "; ps2 = "... "; isSocketRefCountHookInstalled = false; __stdin__ = new PythonFile(Console.OpenStandardInput(), Console.InputEncoding, "<stdin>", "r"); __stdout__ = new PythonFile(Options.BufferedStandardOutAndError ? Console.OpenStandardOutput() : Console.OpenStandardOutput(0), Console.OutputEncoding, "<stdout>", "w"); __stderr__ = new PythonFile(Options.BufferedStandardOutAndError ? Console.OpenStandardError() : Console.OpenStandardError(0), Console.OutputEncoding, "<stderr>", "w"); } __dict__[SymbolTable.Name] = "sys"; stdin = __stdin__; stdout = __stdout__; stderr = __stderr__; // removed from dictionary after the first call to set it. MethodInfo mi = typeof(SystemState).GetMethod("setdefaultencodingImpl", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); BuiltinMethodDescriptor descr = (BuiltinMethodDescriptor)BuiltinFunction.MakeMethod( "setdefaultencoding", mi, FunctionType.PythonVisible | FunctionType.Method).GetDescriptor(); __dict__[SymbolTable.SetDefaultEncoding] = descr.GetAttribute(this, this); DefaultEncoding = Encoding.ASCII; byteorder = BitConverter.IsLittleEndian ? "little" : "big"; copyright = "Copyright (c) Microsoft Corporation. All rights reserved."; maxint = Int32.MaxValue; maxunicode = (int)ushort.MaxValue; platform = "cli"; version_info = Tuple.MakeTuple(2, 4, 0, "final", 0); // report as being compatible w/ 2.4.0 final winver = "2.4"; // !!! These fields do need to be reset on "reload(sys)". However, the initial value is specified by the // engine elsewhere. For now, we initialize them just once to some default value if (version == null) { SetVersion(PythonEngine.VersionString); warnoptions = List.Make(); executable = ""; } }
public static FileStream StreamFromPythonFile(PythonFile pyFile) { FieldInfo streamField = (FieldInfo)(pyFile.GetType().GetMember( "_stream", BindingFlags.NonPublic | BindingFlags.Instance)[0]); return (FileStream)streamField.GetValue(pyFile); }
public static int AddToMapping(PythonFile pf) { return(mapping.Add(pf)); }