Example #1
0
 public static PythonTuple raw_unicode_escape_decode(CodeContext /*!*/ context, [BytesConversion] IList <byte> input, string errors = "strict")
 {
     return(PythonTuple.MakeTuple(
                StringOps.RawDecode(context, input, "raw-unicode-escape", errors),
                input.Count
                ));
 }
Example #2
0
 public static PythonTuple mbcs_decode(CodeContext /*!*/ context, [BytesConversion] IList <byte> input, string errors = "strict", bool final = false)
 {
     return(PythonTuple.MakeTuple(
                StringOps.RawDecode(context, input, Encoding.Default, errors),
                Builtin.len(input)
                ));
 }
Example #3
0
        private static array ArrayReconstructor(CodeContext context, [NotNull]PythonType cls, [NotNull]string typecode, int mformat_code, [NotNull]Bytes items) {
            if (typecode.Length != 1)
                throw PythonOps.TypeError("expected character, got {0}", PythonTypeOps.GetName(typecode));
            if (!typecodes.Contains(typecode))
                throw PythonOps.ValueError("bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)");

            var actualTypeCode = MachineFormatToTypeCode(mformat_code, out bool isBigEndian, out string? encoding);

            var arrayType = DynamicHelpers.GetPythonTypeFromType(typeof(array));

            if (!cls.IsSubclassOf(arrayType)) {
                throw PythonOps.TypeError($"{cls} is not a subtype of array.array");
            }

            array res;
            if (cls == arrayType) {
                res = new array(actualTypeCode);
            } else if (cls.CreateInstance(context, actualTypeCode) is array arr) {
                res = arr;
            } else {
                throw PythonOps.TypeError($"{cls} is not a subtype of array.array");
            }

            if (encoding == null) {
                res.frombytes(items);
                if (isBigEndian) res.byteswap();
            } else {
                res.fromunicode(context, StringOps.RawDecode(context, items, encoding, null));
            }
            return res;
Example #4
0
 public string decode(CodeContext /*!*/ context, [NotNull] string encoding = "utf-8", [NotNull] string errors = "strict")
 {
     return(StringOps.RawDecode(context, _bytes, encoding, errors));
 }