Ejemplo n.º 1
0
 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)));
     }
 }
Ejemplo n.º 2
0
 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()));
     }
 }
Ejemplo n.º 3
0
 public Bytes([NotNull] List bytes)
 {
     _bytes = ByteOps.GetBytes(bytes, ByteOps.GetByteListOk).ToArray();
 }
Ejemplo n.º 4
0
 public Bytes([NotNull] PythonList bytes)
 {
     _bytes = ByteOps.GetBytes(bytes).ToArray();
 }