Beispiel #1
0
        public void TestEmptyStringCtor()
        {
            const string expected = "";
            var          actual   = new PyString(expected);

            Assert.AreEqual(expected, actual.ToString());
        }
Beispiel #2
0
        public void TestUnicode()
        {
            const string expected = "foo\u00e9";
            PyObject     actual   = new PyString(expected);

            Assert.AreEqual(expected, actual.ToString());
        }
Beispiel #3
0
            public new object this[string key]
            {
                get
                {
                    if (ContainsKey(key))
                    {
                        return(base[key]);
                    }

                    if (_lang.PyLang == null)
                    {
                        return(null);
                    }

                    object ret = null;
                    using (Py.GIL())
                    {
                        var pyKeyStr = new PyString(key);
                        var pyObj    = (PyObject)_lang.PyLang.meta.__getitem__(pyKeyStr);

                        if (!PyString.IsStringType(pyObj))
                        {
                            throw new NotImplementedException();
                        }

                        var pyValStr = new PyString(pyObj);
                        ret = pyValStr.ToString();
                        Add(key, ret);
                    }

                    return(ret);
                }
            }
Beispiel #4
0
 public void TestUnicode()
 {
     using (Py.GIL())
     {
         PyObject s = new PyString("foo\u00e9");
         Assert.AreEqual("foo\u00e9", s.ToString());
     }
 }
Beispiel #5
0
        public void TestCtorBorrowed()
        {
            const string expected = "foo";

            var t      = new PyString(expected);
            var actual = new PyString(t.Reference);

            Assert.AreEqual(expected, actual.ToString());
        }
Beispiel #6
0
        public void TestPyObjectCtor()
        {
            const string expected = "Foo";

            var t      = new PyString(expected);
            var actual = new PyString(t);

            Assert.AreEqual(expected, actual.ToString());
        }
Beispiel #7
0
        public void TestUnicodeSurrogate()
        {
            const string expected = "foo\ud83d\udc3c"; // "foo🐼"
            PyObject     actual   = new PyString(expected);

            // python treats "foo🐼" as 4 characters, dotnet as 5
            Assert.AreEqual(4, actual.Length());
            Assert.AreEqual(expected, actual.ToString());
        }
Beispiel #8
0
        public void TestCtorPtr()
        {
            const string expected = "foo";

            var t      = new PyString(expected);
            var actual = new PyString(t.Handle);

            Assert.AreEqual(expected, actual.ToString());
        }
Beispiel #9
0
        public static string GetString(dynamic dynStringPyObj, ref string stringMember)
        {
            if (stringMember != null)
            {
                return(stringMember);
            }

            using (Py.GIL())
            {
                var depPy = new PyString(dynStringPyObj);
                stringMember = depPy.ToString();
                return(stringMember);
            }
        }
Beispiel #10
0
        public object this[object key]
        {
            get
            {
                var keyStr = key as string;
                if (keyStr != null)
                {
                    if (_dictStrToNumber.ContainsKey(keyStr))
                    {
                        return(_dictStrToNumber[keyStr]);
                    }

                    BigInteger valHash;
                    using (Py.GIL())
                    {
                        var dynPyNumber = _pyStringStore.__getitem__(key);
                        var pyNumber    = new PyLong(dynPyNumber);
                        valHash = BigInteger.Parse(pyNumber.ToString());
                        _dictStrToNumber.Add(keyStr, valHash);
                    }

                    return(valHash);
                }

                var keyHash = key.AsBigInteger();
                if (_dictNumberToStr.ContainsKey(keyHash))
                {
                    return(_dictNumberToStr[keyHash]);
                }

                var valStr = string.Empty;
                using (Py.GIL())
                {
                    var dynPyStr = _pyStringStore.__getitem__(key);
                    var pyString = new PyString(dynPyStr);
                    valStr = pyString.ToString();
                    _dictNumberToStr.Add(keyHash, valStr);
                }

                return(valStr);
            }
        }
Beispiel #11
0
        public static List <T> GetListFromList <T>(dynamic pyList, ref List <T> lstMember)
        {
            if (lstMember != null)
            {
                return(lstMember);
            }

            using (Py.GIL())
            {
                lstMember = new List <T>();

                dynamic builtins = Py.Import("builtins");
                var     pyCount  = new PyInt(builtins.len(pyList));
                var     count    = pyCount.ToInt32();

                for (var i = 0; i < count; i++)
                {
                    var element = pyList[i];

                    object created = null;
                    if (typeof(T) == typeof(string))
                    {
                        var pyObj = new PyString(element);
                        created = pyObj.ToString();
                    }
                    else
                    {
                        Debug.Assert(false);
                        return(null);
                    }

                    lstMember.Add((T)created);
                }

                return(lstMember);
            }
        }