Esempio n. 1
0
        public static PyString CreateString(IParseTree context)
        {
            // TODO: Expand to include literals, unicode, f-strings...
            // For now, we just clip off the single/double quotes
            string rawText = context.GetText();

            // Check for long string.
            var match = LongStringDoubleQuoteRegex.Match(rawText);

            if (match.Success)
            {
                return(PyString.Create(match.Groups[1].Value));
            }

            match = LongStringSingleQuoteRegex.Match(rawText);
            if (match.Success)
            {
                return(PyString.Create(match.Groups[1].Value));
            }

            // At this point, just assume quotes on each end and clip them off.
            string finalString = rawText.Substring(1, rawText.Length - 2);

            return(PyString.Create(finalString));
        }
Esempio n. 2
0
        public void DeclareBasicTuple()
        {
            // The peephole optimizer for reference Python code can turn the constants straight into a tuple.
            // That's a huge pain, so here's an example that throws in some math to show how it goes under
            // the hood.
            //
            // >>> def big_tuple(x):
            // ...   a = (x + 1, x + 2)
            // ...   return a
            // ...
            // >>> dis.dis(big_tuple)
            // 2 0 LOAD_FAST                0(x)
            //   2 LOAD_CONST               1(1)
            //   4 BINARY_ADD
            //   6 LOAD_FAST                0(x)
            //   8 LOAD_CONST               2(2)
            //   10 BINARY_ADD
            //   12 BUILD_TUPLE              2
            //   14 STORE_FAST               1(a)
            //
            //   3          16 LOAD_FAST                1(a)
            //   18 RETURN_VALUE
            var interpreter = runProgram("a = (\"foo\", 1)\n", new Dictionary <string, object>(), 1);
            var variables   = interpreter.DumpVariables();

            Assert.That(variables.ContainsKey("a"));
            var tuple = (PyTuple)variables["a"];

            Assert.That(tuple.Values, Is.EquivalentTo(new object[] { PyString.Create("foo"), PyInteger.Create(1) }));
        }
        public void BasicSingleQuoteString()
        {
            var      context = new MockContext("'Hello'");
            PyString result  = ConstantsFactory.CreateString(context);

            Assert.That(result, Is.EqualTo(PyString.Create("Hello")));
        }
Esempio n. 4
0
        public static object Convert(object fromObj, Type toType)
        {
            cachedKey.Item1 = fromObj.GetType();
            cachedKey.Item2 = toType;

            if (fromObj == NoneType.Instance)
            {
                return(null);
            }
            else if (toType.IsAssignableFrom(cachedKey.Item1))
            {
                return(fromObj);
            }
            else if (toType == typeof(string))
            {
                return(fromObj.ToString());
            }
            else if (toType == typeof(PyString))
            {
                return(PyString.Create(fromObj.ToString()));
            }
            else if (converters.ContainsKey(cachedKey))
            {
                return(converters[cachedKey].Invoke(fromObj));
            }
            else if (fromObj is PyDotNetClassProxy)
            {
                return(((PyDotNetClassProxy)fromObj).__getattribute__(PyDotNetClassProxy.__dotnettype__));
            }
            else
            {
                return(fromObj);
            }
        }
        public void LongString()
        {
            var      context = new MockContext("\"\"\"Hello\"\"\"");
            PyString result  = ConstantsFactory.CreateString(context);

            Assert.That(result, Is.EqualTo(PyString.Create("Hello")));
        }
Esempio n. 6
0
        public void DeclareBasicDictionary()
        {
            // The peephole optimizer figures out basic, constant dictionaries and diverts them to
            // BUILD_CONST_KEY_MAP, so I have to use a more obtuse example here to show BUILD_MAP.
            // return_foo() just returns "foo":
            //
            // >>> def dict_name_maker():
            // ...   return {return_foo(): "bar", "number": 1}
            // ...
            // >>> dis.dis(dict_name_maker)
            //   2           0 LOAD_GLOBAL              0 (return_foo)
            //               2 CALL_FUNCTION            0
            //               4 LOAD_CONST               1 ('bar')
            //               6 LOAD_CONST               2 ('number')
            //               8 LOAD_CONST               3 (1)
            //              10 BUILD_MAP                2
            //              12 RETURN_VALUE
            //
            var interpreter = runProgram("a = { \"foo\": \"bar\", \"number\": 1 }\n", new Dictionary <string, object>(), 1);
            var variables   = interpreter.DumpVariables();

            Assert.That(variables.ContainsKey("a"));
            Assert.That(variables["a"], Is.EquivalentTo(new Dictionary <PyString, object> {
                { PyString.Create("foo"), PyString.Create("bar") },
                { PyString.Create("number"), PyInteger.Create(1) }
            }));
        }
Esempio n. 7
0
 public static PyString readline(PyIOBase self, PyInteger size = null)
 {
     if (size == null)
     {
         size = PyInteger.Create(-1);
     }
     return(PyString.Create(self.Readline(size.number)));
 }
Esempio n. 8
0
        public void DeclareSingleElementTuple()
        {
            var interpreter = runProgram("a = (\"foo\",)\n", new Dictionary <string, object>(), 1);
            var variables   = interpreter.DumpVariables();

            Assert.That(variables.ContainsKey("a"));
            var tuple = (PyTuple)variables["a"];

            Assert.That(tuple.Values, Is.EquivalentTo(new object[] { PyString.Create("foo") }));
        }
Esempio n. 9
0
 public void StringConcatenation()
 {
     // Making sure that we're properly parsing and generating all of these when there's multiples of the operator.
     runBasicTest(
         "a = 'Hello'\n" +
         "a = a + ', World!'\n",
         new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyString.Create("Hello, World!") }
     }), 1);
 }
Esempio n. 10
0
        public void SimpleStrAssign()
        {
            runBasicTest("a = 'Hello!'\n", new VariableMultimap(new TupleList <string, object>
            {
                { "a", PyString.Create("Hello!") }
            }), 1);

            runBasicTest("a = \"Hello!\"\n", new VariableMultimap(new TupleList <string, object>
            {
                { "a", PyString.Create("Hello!") }
            }), 1);
        }
Esempio n. 11
0
        public void IndexIntoListWithVariable()
        {
            var interpreter = runProgram("a = [\"foo\", 1]\n" +
                                         "b = 0\n" +
                                         "c = a[b]\n", new Dictionary <string, object>(), 1);
            var variables = interpreter.DumpVariables();

            Assert.That(variables.ContainsKey("c"));
            var element = (PyString)variables["c"];

            Assert.That(element, Is.EqualTo(PyString.Create("foo")));
        }
Esempio n. 12
0
        public void DictionaryReadWrite()
        {
            var interpreter = runProgram("a = { \"foo\": 1, \"bar\": 2 }\n" +
                                         "b = a[\"foo\"]\n" +
                                         "a[\"bar\"] = 200\n", new Dictionary <string, object>(), 1);
            var variables = interpreter.DumpVariables();

            Assert.That(variables.ContainsKey("a"));
            Assert.That(variables["a"], Is.EquivalentTo(new Dictionary <PyString, object> {
                { PyString.Create("foo"), PyInteger.Create(1) },
                { PyString.Create("bar"), PyInteger.Create(200) }
            }));
            Assert.That(variables.ContainsKey("b"));
            Assert.That(variables["b"], Is.EqualTo(PyInteger.Create(1)));
        }
Esempio n. 13
0
        /// <summary>
        /// Implements the dir() command used to dump methods and properties of this PyObject.
        /// </summary>
        /// <param name="o">The object to inspect</param>
        /// <returns>A PyList of the names of the methods and properties of this PyObject.</returns>
        //public static async Task<PyList> dir(IInterpreter interpreter, FrameContext context, PyObject o)
        public static async Task <object> dir(IInterpreter interpreter, FrameContext context, PyObject o)
        {
            // TODO: Figure out how to switch to Task<PyList> signature without everything hanging.
            var internalList = new List <object>();

            foreach (var name in o.__dict__.Keys)
            {
                internalList.Add(PyString.Create(name));
            }

            // Alphabetize them. It's how Python does it and it is quite useful for scanning through the output anyways.
            internalList.Sort((a, b) => a.ToString().CompareTo(b.ToString()));

            var retList = (PyList)await PyListClass.Instance.Call(interpreter, context, new object[0]);

            retList.SetList(internalList);
            return(retList);
        }
Esempio n. 14
0
        public void DeclareBasicList()
        {
            // 2 0 LOAD_CONST               1 ('foo')
            //   2 LOAD_CONST               2(1)
            //   4 BUILD_LIST               2
            //   6 STORE_FAST               0(a)
            var interpreter = runProgram("a = [\"foo\", 1]\n", new Dictionary <string, object>(), 1);
            var variables   = interpreter.DumpVariables();

            Assert.That(variables.ContainsKey("a"));
            List <object> referenceList = new List <object>();

            referenceList.Add(PyString.Create("foo"));
            referenceList.Add(PyInteger.Create(1));
            var list = (PyList)variables["a"];

            Assert.That(list, Is.EquivalentTo(referenceList));
        }
Esempio n. 15
0
        public async Task LenFunction()
        {
            var dictin = PyDict.Create();

            dictin.InternalDict[PyString.Create("1")] = PyInteger.Create(1);
            dictin.InternalDict[PyString.Create("2")] = PyInteger.Create(2);

            await runBasicTest(
                "listout = len(listin)\n" +
                "dictout = len(dictin)\n" +
                "tupleout = len(tuplein)\n" +
                "strout = len(strin)\n" +
                "rangeout = len(rangein)\n" +
                "arrayout = len(arrayin)\n" +
                "enumerableout = len(enumerablein)\n" +
                "dotnetstrout = len(dotnetstrin)\n",     // I think this should be IEnumerable but I'm not taking chances
                new Dictionary <string, object>()
            {
                { "listin", PyList.Create(new List <object>()
                    {
                        PyInteger.Create(1)
                    }) },
                { "dictin", dictin },
                { "tuplein", PyTuple.Create(new object[] { 1, 2, 3 }) },
                { "strin", PyString.Create("1234") },
                { "rangein", PyRange.Create(5, 0, 1) },
                { "arrayin", new int[] { 1, 2, 3, 4, 5, 6 } },
                { "enumerablein", new List <int>()
                  {
                      1, 2, 3, 4, 5, 6, 7
                  } },
                { "dotnetstrin", "12345678" },
            }, new VariableMultimap(new TupleList <string, object>
            {
                { "listout", PyInteger.Create(1) },
                { "dictout", PyInteger.Create(2) },
                { "tupleout", PyInteger.Create(3) },
                { "strout", PyInteger.Create(4) },
                { "rangeout", PyInteger.Create(5) },
                { "arrayout", PyInteger.Create(6) },
                { "enumerableout", PyInteger.Create(7) },
                { "dotnetstrout", PyInteger.Create(8) },
            }), 1);
        }
Esempio n. 16
0
        public async Task DoubleComprehension()
        {
            string program =
                "a = [['Hello', 'World!'], ['Lets', 'Eat!']]\n" +
                "b = [word for words in a for word in words]\n";

            var b = PyList.Create();

            b.list.Add(PyString.Create("Hello"));
            b.list.Add(PyString.Create("World!"));
            b.list.Add(PyString.Create("Lets"));
            b.list.Add(PyString.Create("Eat!"));

            await runBasicTest(program,
                               new VariableMultimap(new TupleList <string, object>
            {
                { "b", b }
            }), 1);
        }
Esempio n. 17
0
 public async Task NumericStringConversions()
 {
     await runBasicTest(
         "a_string = '1'\n" +
         "as_int = int(a_string)\n" +
         "as_float = float(a_string)\n" +
         "as_bool = bool(a_string)\n" +
         "int_str = str(1)\n" +
         "float_str = str(1.0)\n" +
         "bool_str = str(True)\n",
         new VariableMultimap(new TupleList <string, object>
     {
         { "as_int", PyInteger.Create(1) },
         { "as_float", PyFloat.Create(1.0) },
         { "as_bool", PyBool.True },
         { "int_str", PyString.Create("1") },
         { "float_str", PyString.Create("1.0") },
         { "bool_str", PyString.Create("True") },
     }), 1);
 }
Esempio n. 18
0
        public void setupFinders()
        {
            var repo = new InjectedModuleRepository();

            fooModule = PyModule.Create("foo");

            barModule  = PyModule.Create("bar");
            fooThing   = PyString.Create("FooThing");
            otherThing = PyString.Create("OtherThing");
            fooModule.__dict__.Add("bar", barModule);
            fooModule.__dict__.Add("FooThing", fooThing);
            fooModule.__dict__.Add("OtherThing", otherThing);

            foo2Module = PyModule.Create("foo2");

            repo.AddNewModuleRoot(fooModule);
            repo.AddNewModuleRoot(foo2Module);

            moduleFinders = new List <ISpecFinder>();
            moduleFinders.Add(repo);
        }
Esempio n. 19
0
        public async Task SecondLevelNonModule()
        {
            // Exception should be something like:
            // ModuleNotFoundError: No module named 'foo.bar.barstring'; 'foo.bar.barstring' is not a package
            // They would be forced to use the "from foo.bar import barstring"
            var repo      = new InjectedModuleRepository();
            var fooModule = PyModule.Create("foo");
            var barModule = PyModule.Create("bar");
            var barString = PyString.Create("bar string");

            barModule.__dict__.Add("barstring", barString);
            fooModule.__dict__.Add("bar", barModule);
            repo.AddNewModuleRoot(fooModule);

            var barStringSpec = repo.find_spec(null, "foo.bar.barstring", null, null);

            Assert.That(barStringSpec, Is.Not.Null);

            var barStringLoaded = await barStringSpec.Loader.Load(null, null, barStringSpec);

            Assert.That(barStringLoaded, Is.EqualTo(barString));
        }
Esempio n. 20
0
        public async Task BasicImport()
        {
            var repoRoots        = new List <string>();
            var fake_module_root = Path.Combine(Path.GetDirectoryName(typeof(FileImporterTests).Assembly.Location),
                                                "fake_module_root");

            repoRoots.Add(fake_module_root);

            var scheduler   = new Scheduler();              // Might not need the actual scheduler...
            var interpreter = new Interpreter(scheduler);

            var loader      = new FileBasedModuleLoader();
            var rootContext = new FrameContext();
            var repo        = new FileBasedModuleFinder(repoRoots, loader);

            var spec = repo.find_spec(null, "test", null, null);

            Assert.NotNull(spec);

            var loadedModule = await spec.Loader.Load(interpreter, rootContext, spec) as PyModule;

            Assert.That(loadedModule.__dict__, Contains.Key("a_string"));
            Assert.That(loadedModule.__dict__["a_string"], Is.EqualTo(PyString.Create("Yay!")));
        }