public static object encode(CodeContext /*!*/ context, object obj, string encoding = null, string errors = "strict") { if (encoding == null) { if (obj is string str) { PythonContext lc = context.LanguageContext; return(StringOps.DoEncode(context, str, errors, lc.GetDefaultEncodingName(), lc.DefaultEncoding, includePreamble: true)); } else { throw PythonOps.TypeError("expected str, got {0}", PythonTypeOps.GetName(obj)); } } PythonTuple t = lookup(context, encoding); return(PythonOps.GetIndex(context, PythonCalls.Call(context, t[EncoderIndex], obj, errors), 0)); }
public static object decode(CodeContext /*!*/ context, object obj, string encoding = null, string errors = "strict") { if (encoding == null) { if (obj is IList <byte> bytesLikeObj) { PythonContext lc = context.LanguageContext; return(StringOps.DoDecode(context, bytesLikeObj, errors, lc.GetDefaultEncodingName(), lc.DefaultEncoding, final: true, out _)); } else { throw PythonOps.TypeError("expected bytes-like object, got {0}", PythonTypeOps.GetName(obj)); } } PythonTuple t = lookup(context, encoding); return(PythonOps.GetIndex(context, PythonCalls.Call(context, t[DecoderIndex], obj, errors), 0)); }
public static object encode(CodeContext /*!*/ context, object?obj, [NotNull, DisallowNull] string?encoding = null, [NotNull] string errors = "strict") { if (encoding == null) { if (obj is string str) { PythonContext lc = context.LanguageContext; return(StringOps.DoEncode(context, str, errors, lc.GetDefaultEncodingName(), lc.DefaultEncoding, includePreamble: true)); } else { throw PythonOps.TypeError("expected str, got {0}", PythonTypeOps.GetName(obj)); } } else { object?encoder = lookup(context, encoding)[EncoderIndex]; if (!PythonOps.IsCallable(context, encoder)) { throw PythonOps.TypeError("encoding with '{0}' codec failed; encoder must be callable ('{1}' object is not callable)", encoding, PythonTypeOps.GetName(encoder)); } return(PythonOps.GetIndex(context, PythonCalls.Call(context, encoder, obj, errors), 0)); } }
public static object decode(CodeContext /*!*/ context, object?obj, [NotNull, DisallowNull] string?encoding = null, [NotNull] string errors = "strict") { if (encoding == null) { if (obj is IList <byte> bytesLikeObj) { PythonContext lc = context.LanguageContext; return(StringOps.DoDecode(context, bytesLikeObj, errors, lc.GetDefaultEncodingName(), lc.DefaultEncoding)); } else { throw PythonOps.TypeError("expected bytes-like object, got {0}", PythonTypeOps.GetName(obj)); } } else { object?decoder = lookup(context, encoding)[DecoderIndex]; if (!PythonOps.IsCallable(context, decoder)) { throw PythonOps.TypeError("decoding with '{0}' codec failed; decoder must be callable ('{1}' object is not callable)", encoding, PythonTypeOps.GetName(decoder)); } return(PythonOps.GetIndex(context, PythonCalls.Call(context, decoder, obj, errors), 0)); } }