Example #1
0
 public int ConvertPyFileToDescriptor(PythonFile pyFile)
 {
     FileStream stream = InappropriateReflection.StreamFromPythonFile(pyFile);
     SafeHandle safeHandle = stream.SafeFileHandle;
     IntPtr handle = safeHandle.DangerousGetHandle();
     return Unmanaged._open_osfhandle(handle, 0);
 }
Example #2
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;
 }
Example #4
0
        /// <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  = "";
            }
        }
Example #5
0
        private static IEnumerator<byte> FileEnumerator(PythonFile/*!*/ file) {
            for (; ; ) {
                string data = file.read(1);
                if (data.Length == 0) {
                    yield break;
                }

                yield return (byte)data[0];
            }
        }
Example #6
0
        public static PythonFile GetFileFromId(int id)
        {
            PythonFile pf = mapping.GetObjectFromId(id);

            if (pf != null)
            {
                return(pf);
            }
            throw Ops.OSError("Bad file descriptor");
        }
Example #7
0
        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);
 }
Example #9
0
 public void tofile(PythonFile f) {
     f.write(tostring());
 }
Example #10
0
 public PythonFileStreamContentProvider(PythonFile file)
 {
     _file = file;
 }
Example #11
0
 public PythonFileStreamContentProvider(PythonFile file) {
     _file = file;
 }
Example #12
0
 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);
 }
Example #13
0
 public static int GetIdFromFile(PythonFile pf)
 {
     return(mapping.GetIdFromObject(pf));
 }
Example #14
0
            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);
            }
Example #15
0
 public static object load_compiled(string name, string pathname, PythonFile file) {
     return null;
 }
Example #16
0
        /// <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;
        }
Example #17
0
        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));
        }
Example #18
0
        /// <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;
        }
Example #19
0
 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;
 }
Example #20
0
 public static int GetIdFromFile(PythonFile pf)
 {
     return mapping.GetIdFromObject(pf);
 }
Example #21
0
 public void write(PythonFile f) {
     tofile(f);
 }
Example #22
0
 public void ToFile(PythonFile f)
 {
     f.Write(ConvertToString());
 }
Example #23
0
        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);
            }
        }
Example #24
0
 public void Write(PythonFile f)
 {
     ToFile(f);
 }
Example #25
0
 public static object load_dynamic(string name, string pathname, PythonFile file) {
     return null;
 }
Example #26
0
        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));
        }
Example #27
0
 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;
 }
Example #28
0
 public static int AddToWeakMapping(PythonFile pf)
 {
     return mapping.WeakAdd(pf);
 }
Example #29
0
        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);
        }
Example #30
0
 public static void Remove(PythonFile pf)
 {
     mapping.RemoveOnObject(pf);
 }
Example #31
0
        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;
        }
Example #32
0
            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);
            }
Example #33
0
 public static void dump(object value, PythonFile/*!*/ file) {
     dump(value, file, version);
 }
Example #34
0
 public static void dump(object value, PythonFile/*!*/ file) {
     dump(value, file, null);
 }
Example #35
0
        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);
 }
Example #38
0
 public static int AddToMapping(PythonFile pf)
 {
     return(mapping.Add(pf));
 }