Esempio n. 1
0
        internal void EnsureLoaded()
        {
#if FALSE
            // If we're fully loaded, just return.
            if (_loadState == LoadState.Loaded)
            {
                return;
            }

            // If we're loading or not loaded, we need to take this lock.
            if (!_typeDb.BeginModuleLoad(this, 10000))
            {
                Debug.Fail("Timeout loading {0}", _modName);
                //throw new InvalidOperationException("Cannot load module at this time");
                return;
            }

            try {
                // Ensure we haven't started/finished loading while waiting
                if (_loadState != LoadState.NotLoaded)
                {
                    return;
                }

                // Mark as loading now (before it completes), if we have circular
                // references we'll fix them up after loading
                // completes.
                _loadState = LoadState.Loading;

                using (var stream = new FileStream(_dbFile, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    Dictionary <string, object> contents = null;
                    try {
                        contents = (Dictionary <string, object>)Unpickle.Load(stream);
                    } catch (ArgumentException) {
                        _typeDb.OnDatabaseCorrupt();
                    } catch (InvalidOperationException) {
                        // Bug 511 - http://pytools.codeplex.com/workitem/511
                        // Ignore a corrupt database file.
                        _typeDb.OnDatabaseCorrupt();
                    }

                    if (contents != null)
                    {
                        LoadModule(contents);
                    }
                }
            } catch (FileNotFoundException) {
                // if the file got deleted before we've loaded it don't crash...
            } catch (IOException) {
                // or if someone has locked the file for some reason, also don't crash...
            } finally {
                // Regardless of how we finish, mark us as loaded so we don't
                // try again.
                _loadState = LoadState.Loaded;
                _typeDb.EndModuleLoad(this);
            }
#endif
        }
Esempio n. 2
0
        public static RawView FromFile(string filename)
        {
            object contents = null;

            try {
                using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    try {
                        contents = Unpickle.Load(stream);
                    } catch (ArgumentException) {
                    } catch (InvalidOperationException) {
                    }
                }
            } catch (Exception ex) {
                contents = ex;
            }
            return(new RawView(Path.GetFileName(filename), filename, contents));
        }
Esempio n. 3
0
        public void TestSimpleDeserialize()
        {
            object obj;

            using (var stream = new FileStream(TestData.GetPath(@"TestData\empty_dict.pickle"), FileMode.Open, FileAccess.Read)) {
                obj = Unpickle.Load(stream);
            }

            Assert.AreEqual(obj.GetType(), typeof(Dictionary <string, object>));
            Assert.AreEqual(((Dictionary <string, object>)obj).Count, 0);

            using (var stream = new FileStream(TestData.GetPath(@"TestData\simple_dict.pickle"), FileMode.Open, FileAccess.Read)) {
                obj = Unpickle.Load(stream);
            }

            Assert.AreEqual(obj.GetType(), typeof(Dictionary <string, object>));
            var dict = ((Dictionary <string, object>)obj);

            Assert.AreEqual(dict.Count, 8);

            Assert.AreEqual(dict["int"].GetType(), typeof(int)); Assert.AreEqual(dict["int"], 42);
            Assert.AreEqual(dict["str"].GetType(), typeof(string)); Assert.AreEqual(dict["str"], "baz");
            Assert.AreEqual(dict["long"].GetType(), typeof(BigInteger)); Assert.AreEqual(dict["long"], (BigInteger)42);
            Assert.AreEqual(dict["float"].GetType(), typeof(double)); Assert.AreEqual(dict["float"], 42.0);
            Assert.AreEqual(dict["emptytuple"].GetType(), typeof(object[])); Assert.AreEqual(((object[])dict["emptytuple"]).Length, 0);
            Assert.AreEqual(dict["tuple"].GetType(), typeof(object[]));
            Assert.AreEqual(dict["emptylist"].GetType(), typeof(List <object>));
            Assert.AreEqual(dict["list"].GetType(), typeof(List <object>));

            using (var stream = new FileStream(TestData.GetPath(@"TestData\simple_dict2.pickle"), FileMode.Open, FileAccess.Read)) {
                obj = Unpickle.Load(stream);
            }

            Assert.AreEqual(obj.GetType(), typeof(Dictionary <string, object>));
            dict = ((Dictionary <string, object>)obj);
            Assert.AreEqual(dict.Count, 7);

            Assert.AreEqual(dict["true"].GetType(), typeof(bool));  Assert.AreEqual(dict["true"], true);
            Assert.AreEqual(dict["false"].GetType(), typeof(bool)); Assert.AreEqual(dict["false"], false);
            Assert.AreEqual(dict["None"], null);
            Assert.AreEqual(dict["tuple1"].GetType(), typeof(object[])); Assert.AreEqual(((object[])dict["tuple1"])[0], 42);
            Assert.AreEqual(dict["tuple2"].GetType(), typeof(object[])); Assert.AreEqual(((object[])dict["tuple2"])[0], 42); Assert.AreEqual(((object[])dict["tuple2"])[1], 42);
            Assert.AreEqual(dict["tuple3"].GetType(), typeof(object[])); Assert.AreEqual(((object[])dict["tuple3"])[0], 42); Assert.AreEqual(((object[])dict["tuple3"])[1], 42); Assert.AreEqual(((object[])dict["tuple3"])[2], 42);
            Assert.AreEqual(dict["unicode"].GetType(), typeof(string)); Assert.AreEqual(dict["unicode"], "abc");
        }
Esempio n. 4
0
        private void EnsureLoaded()
        {
            if (!_loaded)
            {
                // mark as loading now (before it completes), if we have circular references we'll fix them up after loading completes.
                _loaded = true;

                _loadDepth++;
                using (var stream = new FileStream(_dbFile, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    var contents = (Dictionary <string, object>)Unpickle.Load(stream);

                    LoadModule((Dictionary <string, object>)contents);
                }
                _loadDepth--;

                // don't run fixups until we've processed all of the inter-dependent modules.
                if (_loadDepth == 0)
                {
                    _typeDb.RunFixups();
                }
            }
        }
Esempio n. 5
0
        private void TestOpen(PythonVersion path)
        {
            path.AssertInstalled();
            Console.WriteLine(path.InterpreterPath);

            Guid testId  = Guid.NewGuid();
            var  testDir = TestData.GetTempPath(testId.ToString());

            var scraper = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "PythonScraper.py");

            Assert.IsTrue(File.Exists(scraper), $"Did not find {scraper}");

            // run the scraper
            using (var proc = ProcessOutput.RunHiddenAndCapture(
                       path.InterpreterPath,
                       scraper,
                       testDir,
                       CompletionDB
                       )) {
                Console.WriteLine("Command: " + proc.Arguments);

                proc.Wait();

                // it should succeed
                Console.WriteLine("**Stdout**");
                foreach (var line in proc.StandardOutputLines)
                {
                    Console.WriteLine(line);
                }
                Console.WriteLine("");
                Console.WriteLine("**Stdout**");
                foreach (var line in proc.StandardErrorLines)
                {
                    Console.WriteLine(line);
                }

                Assert.AreEqual(0, proc.ExitCode, "Bad exit code: " + proc.ExitCode);
            }

            // perform some basic validation
            dynamic builtinDb = Unpickle.Load(new FileStream(Path.Combine(testDir, path.Version.Is3x() ? "builtins.idb" : "__builtin__.idb"), FileMode.Open, FileAccess.Read));

            if (path.Version.Is2x())   // no open in 3.x
            {
                foreach (var overload in builtinDb["members"]["open"]["value"]["overloads"])
                {
                    Assert.AreEqual("__builtin__", overload["ret_type"][0][0]);
                    Assert.AreEqual("file", overload["ret_type"][0][1]);
                }

                if (!path.InterpreterPath.Contains("Iron"))
                {
                    // http://pytools.codeplex.com/workitem/799
                    var arr = (IList <object>)builtinDb["members"]["list"]["value"]["members"]["__init__"]["value"]["overloads"];
                    Assert.AreEqual(
                        "args",
                        ((dynamic)(arr[0]))["args"][1]["name"]
                        );
                }
            }

            if (!path.InterpreterPath.Contains("Iron"))
            {
                dynamic itertoolsDb = Unpickle.Load(new FileStream(Path.Combine(testDir, "itertools.idb"), FileMode.Open, FileAccess.Read));
                var     tee         = itertoolsDb["members"]["tee"]["value"];
                var     overloads   = tee["overloads"];
                var     nArg        = overloads[0]["args"][1];
                Assert.AreEqual("n", nArg["name"]);
                Assert.AreEqual("2", nArg["default_value"]);

                dynamic sreDb   = Unpickle.Load(new FileStream(Path.Combine(testDir, "_sre.idb"), FileMode.Open, FileAccess.Read));
                var     members = sreDb["members"];
                Assert.IsTrue(members.ContainsKey("SRE_Pattern"));
                Assert.IsTrue(members.ContainsKey("SRE_Match"));
            }

            Console.WriteLine("Passed: {0}", path.InterpreterPath);
        }
Esempio n. 6
0
        internal void EnsureLoaded()
        {
            // If we're fully loaded, just return.
            if (_loadState == LoadState.Loaded)
            {
                return;
            }

            // If we're loading or not loaded, we need to take this lock.
            if (!_typeDb.BeginModuleLoad(this, 10000))
            {
                Debug.Fail(string.Format("Timeout loading {0}", _modName));
                //throw new InvalidOperationException("Cannot load module at this time");
                return;
            }

            try {
                // Ensure we haven't started/finished loading while waiting
                if (_loadState != LoadState.NotLoaded)
                {
                    return;
                }

                // Mark as loading now (before it completes), if we have circular
                // references we'll fix them up after loading
                // completes.
                _loadState = LoadState.Loading;

                // Will be set to true if we should delete this file and then
                // trigger a corruption notification.
                bool deleteFile = false;

                using (var stream = new FileStream(_dbFile, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    Dictionary <string, object> contents = null;
                    try {
                        contents = (Dictionary <string, object>)Unpickle.Load(stream);
                    } catch (ArgumentException) {
                        _typeDb.OnDatabaseCorrupt();
                    } catch (InvalidOperationException) {
                        // Bug 511 - http://pytools.codeplex.com/workitem/511
                        // Ignore a corrupt database file.
                        _typeDb.OnDatabaseCorrupt();
                    }

                    if (contents != null)
                    {
                        object obj;
                        string filename;
                        if (contents.TryGetValue("filename", out obj))
                        {
                            filename = obj as string;
                            if (!string.IsNullOrEmpty(filename) && !File.Exists(filename))
                            {
                                // Issue 2358 - must refresh DB after it moves
                                // on disk.
                                deleteFile = true;
                            }
                        }

                        LoadModule(contents);
                    }
                }

                if (deleteFile)
                {
                    // Only notify if we actually remove the file
                    try {
                        File.Delete(_dbFile);
                        _typeDb.OnDatabaseCorrupt();
                    } catch (IOException) {
                    } catch (UnauthorizedAccessException) {
                    }
                }
            } catch (FileNotFoundException) {
                // if the file got deleted before we've loaded it don't crash...
            } catch (IOException) {
                // or if someone has locked the file for some reason, also don't crash...
            } finally {
                // Regardless of how we finish, mark us as loaded so we don't
                // try again.
                _loadState = LoadState.Loaded;
                _typeDb.EndModuleLoad(this);
            }
        }