public static object __new__(CodeContext context, [NotNull] PythonType cls, object? @object) { if (cls == TypeCache.Bytes) { if (@object?.GetType() == typeof(Bytes)) { return(@object); } else if (TryInvokeBytesOperator(context, @object, out Bytes? res)) { return(res); } else if (Converter.TryConvertToIndex(@object, throwOverflowError: true, out int size)) { if (size < 0) { throw PythonOps.ValueError("negative count"); } return(new Bytes(new byte[size])); } else { return(new Bytes(ByteOps.GetBytes(@object, useHint: true, context).ToArray())); } } else { return(cls.CreateInstance(context, __new__(context, TypeCache.Bytes, @object))); } }
internal static Bytes FromObject(CodeContext context, object?o) { if (o == null) { throw PythonOps.TypeError("cannot convert 'NoneType' object to bytes"); } else if (o.GetType() == typeof(Bytes)) { return((Bytes)o); } else if (TryInvokeBytesOperator(context, o, out Bytes? res)) { return(res); } else if (o is IBufferProtocol bp) { return(new Bytes(bp)); } else if (o is string || o is ExtensibleString) { throw PythonOps.TypeError("cannot convert unicode object to bytes"); } else { return(new Bytes(ByteOps.GetBytes(o, useHint: true, context).ToArray())); } }
public Bytes([NotNull] List bytes) { _bytes = ByteOps.GetBytes(bytes, ByteOps.GetByteListOk).ToArray(); }
public Bytes([NotNull] PythonList bytes) { _bytes = ByteOps.GetBytes(bytes).ToArray(); }