public static BigInteger __long__(Complex self) { throw PythonOps.TypeError("can't convert complex to long; use long(abs(z))"); }
public static bool LessThan(Complex x, Complex y) { throw PythonOps.TypeError("complex is not an ordered type"); }
public static double __float__(Complex self) { throw PythonOps.TypeError("can't convert complex to float; use abs(z)"); }
public static int __int__(Complex self) { throw PythonOps.TypeError(" can't convert complex to int; use int(abs(z))"); }
private static BigInteger DivMod(BigInteger x, BigInteger y, out BigInteger r) { BigInteger rr; BigInteger qq; #if !FEATURE_NUMERICS if (Object.ReferenceEquals(x, null)) { throw PythonOps.TypeError("unsupported operands for div/mod: NoneType and long"); } if (Object.ReferenceEquals(y, null)) { throw PythonOps.TypeError("unsupported operands for div/mod: long and NoneType"); } #endif qq = BigInteger.DivRem(x, y, out rr); if (x >= BigInteger.Zero) { if (y > BigInteger.Zero) { r = rr; return(qq); } else { if (rr == BigInteger.Zero) { r = rr; return(qq); } else { r = rr + y; return(qq - BigInteger.One); } } } else { if (y > BigInteger.Zero) { if (rr == BigInteger.Zero) { r = rr; return(qq); } else { r = rr + y; return(qq - BigInteger.One); } } else { r = rr; return(qq); } } }
public static PythonTuple DivMod(CodeContext context, Complex x, Complex y) { throw PythonOps.TypeError("can't take floor or mod of complex number"); }
public static object __new__(CodeContext context, PythonType cls, object x) { Extensible <string> es; if (x is string) { return(ReturnObject(context, cls, ParseBigIntegerSign((string)x, 10))); } else if ((es = x as Extensible <string>) != null) { object value; if (PythonTypeOps.TryInvokeUnaryOperator(context, x, "__long__", out value)) { return(ReturnObject(context, cls, (BigInteger)value)); } return(ReturnObject(context, cls, ParseBigIntegerSign(es.Value, 10))); } if (x is double) { return(ReturnObject(context, cls, DoubleOps.__long__((double)x))); } if (x is int) { return(ReturnObject(context, cls, (BigInteger)(int)x)); } if (x is BigInteger) { return(ReturnObject(context, cls, x)); } if (x is Complex) { throw PythonOps.TypeError("can't convert complex to long; use long(abs(z))"); } if (x is decimal) { return(ReturnObject(context, cls, (BigInteger)(decimal)x)); } object result; int intRes; BigInteger bigintRes; if (PythonTypeOps.TryInvokeUnaryOperator(context, x, "__long__", out result) && !Object.ReferenceEquals(result, NotImplementedType.Value) || x is OldInstance && PythonTypeOps.TryInvokeUnaryOperator(context, x, "__int__", out result) && !Object.ReferenceEquals(result, NotImplementedType.Value)) { if (result is int || result is BigInteger || result is Extensible <int> || result is Extensible <BigInteger> ) { return(ReturnObject(context, cls, result)); } else { throw PythonOps.TypeError("__long__ returned non-long (type {0})", PythonTypeOps.GetOldName(result)); } } else if (PythonOps.TryGetBoundAttr(context, x, "__trunc__", out result)) { result = PythonOps.CallWithContext(context, result); if (Converter.TryConvertToInt32(result, out intRes)) { return(ReturnObject(context, cls, (BigInteger)intRes)); } else if (Converter.TryConvertToBigInteger(result, out bigintRes)) { return(ReturnObject(context, cls, bigintRes)); } else { throw PythonOps.TypeError("__trunc__ returned non-Integral (type {0})", PythonTypeOps.GetOldName(result)); } } if (x is OldInstance) { throw PythonOps.AttributeError("{0} instance has no attribute '__trunc__'", ((OldInstance)x)._class.Name); } else { throw PythonOps.TypeError("long() argument must be a string or a number, not '{0}'", DynamicHelpers.GetPythonType(x).Name); } }
private static object FastNew(CodeContext /*!*/ context, object o, int @base = 10) { Extensible <BigInteger> el; if (o is string) { return(__new__(null, (string)o, @base)); } if (o is double) { return(DoubleOps.__int__((double)o)); } if (o is int) { return(o); } if (o is bool) { return(((bool)o) ? 1 : 0); } if (o is BigInteger) { int res; if (((BigInteger)o).AsInt32(out res)) { return(ScriptingRuntimeHelpers.Int32ToObject(res)); } return(o); } if ((el = o as Extensible <BigInteger>) != null) { int res; if (el.Value.AsInt32(out res)) { return(ScriptingRuntimeHelpers.Int32ToObject(res)); } return(el.Value); } if (o is float) { return(DoubleOps.__int__((double)(float)o)); } if (o is Complex) { throw PythonOps.TypeError("can't convert complex to int; use int(abs(z))"); } if (o is Int64) { Int64 val = (Int64)o; if (Int32.MinValue <= val && val <= Int32.MaxValue) { return((Int32)val); } else { return((BigInteger)val); } } else if (o is UInt32) { UInt32 val = (UInt32)o; if (val <= Int32.MaxValue) { return((Int32)val); } else { return((BigInteger)val); } } else if (o is UInt64) { UInt64 val = (UInt64)o; if (val <= Int32.MaxValue) { return((Int32)val); } else { return((BigInteger)val); } } else if (o is Decimal) { Decimal val = (Decimal)o; if (Int32.MinValue <= val && val <= Int32.MaxValue) { return((Int32)val); } else { return((BigInteger)val); } } else if (o is Enum) { return(((IConvertible)o).ToInt32(null)); } if (o is Extensible <string> es) { // __int__ takes precedence, call it if it's available... object value; if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, es, "__int__", out value)) { return(value); } // otherwise call __new__ on the string value return(__new__(null, es.Value, @base)); } object result; int intRes; BigInteger bigintRes; if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__int__", out result) && !Object.ReferenceEquals(result, NotImplementedType.Value)) { if (result is int || result is BigInteger || result is Extensible <int> || result is Extensible <BigInteger> ) { return(result); } else { throw PythonOps.TypeError("__int__ returned non-Integral (type {0})", PythonTypeOps.GetName(result)); } } else if (PythonOps.TryGetBoundAttr(context, o, "__trunc__", out result)) { result = PythonOps.CallWithContext(context, result); if (result is int || result is BigInteger || result is Extensible <int> || result is Extensible <BigInteger> ) { return(result); } else if (Converter.TryConvertToInt32(result, out intRes)) { return(intRes); } else if (Converter.TryConvertToBigInteger(result, out bigintRes)) { return(bigintRes); } else { throw PythonOps.TypeError("__trunc__ returned non-Integral (type {0})", PythonTypeOps.GetName(result)); } } throw PythonOps.TypeError("int() argument must be a string or a number, not '{0}'", PythonTypeOps.GetName(o)); }
internal static byte GetByte(object o) { Extensible <int> ei; Extensible <BigInteger> ebi; Extensible <double> ed; int i; if (o is int) { return(((int)o).ToByteChecked()); } else if (o is BigInteger) { return(((BigInteger)o).ToByteChecked()); } else if (o is double) { return(((double)o).ToByteChecked()); } else if ((ei = o as Extensible <int>) != null) { return(ei.Value.ToByteChecked()); } else if (!Object.ReferenceEquals(ebi = o as Extensible <BigInteger>, null)) { return(ebi.Value.ToByteChecked()); } else if (!Object.ReferenceEquals(ed = o as Extensible <double>, null)) { return(ed.Value.ToByteChecked()); } else if (o is byte) { return((byte)o); } else if (o is sbyte) { return(((int)(sbyte)o).ToByteChecked()); } else if (o is char) { return(((int)(char)o).ToByteChecked()); } else if (o is short) { return(((int)(short)o).ToByteChecked()); } else if (o is ushort) { return(((int)(ushort)o).ToByteChecked()); } else if (o is uint) { return(((BigInteger)(uint)o).ToByteChecked()); } else if (o is float) { return(((double)(float)o).ToByteChecked()); } else if (Converter.TryConvertToIndex(o, out i)) { return(i.ToByteChecked()); } else { throw PythonOps.TypeError("an integer or string of size 1 is required"); } }
/// <summary> /// Implements the default __reduce_ex__ method as specified by PEP 307 case 3 (new-style instance, protocol 2) /// </summary> private static PythonTuple ReduceProtocol2(CodeContext /*!*/ context, object self) { PythonType myType = DynamicHelpers.GetPythonType(self); object func, state, listIterator, dictIterator; object[] funcArgs; func = context.LanguageContext.NewObject; object getNewArgsCallable; if (PythonOps.TryGetBoundAttr(context, myType, "__getnewargs__", out getNewArgsCallable)) { // TypeError will bubble up if __getnewargs__ isn't callable PythonTuple newArgs = PythonOps.CallWithContext(context, getNewArgsCallable, self) as PythonTuple; if (newArgs == null) { throw PythonOps.TypeError("__getnewargs__ should return a tuple"); } funcArgs = new object[1 + newArgs.Count]; funcArgs[0] = myType; for (int i = 0; i < newArgs.Count; i++) { funcArgs[i + 1] = newArgs[i]; } } else { funcArgs = new object[] { myType }; } if (!PythonTypeOps.TryInvokeUnaryOperator(context, self, "__getstate__", out state)) { object dict; if (self is IPythonObject ipo) { dict = ipo.Dict; } else if (!PythonOps.TryGetBoundAttr(context, self, "__dict__", out dict)) { dict = null; } PythonDictionary initializedSlotValues = GetInitializedSlotValues(self); if (initializedSlotValues != null && initializedSlotValues.Count == 0) { initializedSlotValues = null; } if (dict == null && initializedSlotValues == null) { state = null; } else if (dict != null && initializedSlotValues == null) { state = dict; } else if (dict != null && initializedSlotValues != null) { state = PythonTuple.MakeTuple(dict, initializedSlotValues); } else /*dict == null && initializedSlotValues != null*/ state {
private static PythonTuple ReduceProtocol2(CodeContext /*!*/ context, object self) { // builtin types which can't be pickled (due to tp_itemsize != 0) if (self is MemoryView) { throw PythonOps.TypeError("can't pickle memoryview objects"); } PythonType myType = DynamicHelpers.GetPythonType(self); object?state; object?[] funcArgs; var copyreg = context.LanguageContext.GetCopyRegModule(); var func = PythonOps.GetBoundAttr(context, copyreg, "__newobj__"); if (PythonOps.TryGetBoundAttr(context, myType, "__getnewargs__", out object?getNewArgsCallable)) { // TypeError will bubble up if __getnewargs__ isn't callable if (!(PythonOps.CallWithContext(context, getNewArgsCallable, self) is PythonTuple newArgs)) { throw PythonOps.TypeError("__getnewargs__ should return a tuple"); } funcArgs = new object[1 + newArgs.Count]; funcArgs[0] = myType; for (int i = 0; i < newArgs.Count; i++) { funcArgs[i + 1] = newArgs[i]; } } else { funcArgs = new object[] { myType }; } if (!PythonTypeOps.TryInvokeUnaryOperator(context, self, "__getstate__", out state)) { object?dict; if (self is IPythonObject ipo) { dict = ipo.Dict; } else if (!PythonOps.TryGetBoundAttr(context, self, "__dict__", out dict)) { dict = null; } PythonDictionary?initializedSlotValues = GetInitializedSlotValues(self); if (initializedSlotValues != null && initializedSlotValues.Count == 0) { initializedSlotValues = null; } if (dict == null && initializedSlotValues == null) { state = null; } else if (dict != null && initializedSlotValues == null) { state = dict; } else if (dict != null && initializedSlotValues != null) { state = PythonTuple.MakeTuple(dict, initializedSlotValues); } else /*dict == null && initializedSlotValues != null*/ state {
public static object __new__( CodeContext context, PythonType cls, [DefaultParameterValue(null)] object real, [DefaultParameterValue(null)] object imag ) { Complex real2, imag2; real2 = imag2 = new Complex(); if (real == null && imag == null && cls == TypeCache.Complex) { throw PythonOps.TypeError("argument must be a string or a number"); } if (imag != null) { if (real is string) { throw PythonOps.TypeError("complex() can't take second arg if first is a string"); } if (imag is string) { throw PythonOps.TypeError("complex() second arg can't be a string"); } imag2 = Converter.ConvertToComplex(imag); } if (real != null) { if (real is string) { real2 = LiteralParser.ParseComplex((string)real); } else if (real is Extensible <string> ) { real2 = LiteralParser.ParseComplex(((Extensible <string>)real).Value); } else if (real is Complex) { if (imag == null && cls == TypeCache.Complex) { return(real); } else { real2 = (Complex)real; } } else { real2 = Converter.ConvertToComplex(real); } } double real3 = real2.Real - imag2.Imaginary(); double imag3 = real2.Imaginary() + imag2.Real; if (cls == TypeCache.Complex) { return(new Complex(real3, imag3)); } else { return(cls.CreateInstance(context, real3, imag3)); } }
public static object __new__(CodeContext context, PythonType cls, object?real, [Optional] object?imag) { // Fast-track for a single argument when type(arg) is complex and no subclasses are involved. if (real is Complex && imag is Missing && cls == TypeCache.Complex) { return(real); } if (real is string s) { return(ParseComplex(s, imag)); } if (real is Extensible <string> es) { return(ParseComplex(es.Value, imag)); } if (imag is string || imag is Extensible <string> ) { throw PythonOps.TypeError("complex() second arg can't be a string"); } Complex real2; bool real_is_entirely_real = false; if (real is Complex z) { real2 = z; } else if (!TryInvokeComplex(context, real, out real2)) { if (real is Extensible <Complex> ez) { real2 = ez.Value; } else if (DoubleOps.TryToFloat(context, real, out double res)) { real2 = res; real_is_entirely_real = true; // to preserve zero-sign of imag } else { throw PythonOps.TypeErrorForBadInstance("complex() first argument must be a string or a number, not '{0}'", real); } } double real3, imag3; if (imag is Missing) { real3 = real2.Real; imag3 = real2.Imaginary; } else { Complex imag2; if (imag is Complex z2) { imag2 = z2; // surprisingly, no TryInvokeComplex here } else if (imag is Extensible <Complex> ez2) { imag2 = ez2.Value; } else if (DoubleOps.TryToFloat(context, imag, out double res)) { imag2 = res; } else { throw PythonOps.TypeErrorForBadInstance("complex() second argument must be a number, not '{0}'", imag); } real3 = real2.Real - imag2.Imaginary; imag3 = real_is_entirely_real ? imag2.Real : real2.Imaginary + imag2.Real; } if (cls == TypeCache.Complex) { return(new Complex(real3, imag3)); } else { return(cls.CreateInstance(context, real3, imag3)); }
public static bool GreaterThanOrEqual(Complex x, Complex y) { throw PythonOps.TypeError("complex is not an ordered type"); }
public static Complex FloorDivide(CodeContext context, Complex x, Complex y) { throw PythonOps.TypeError("can't take floor of complex number"); }
public static object __new__(CodeContext context, PythonType cls, [Optional] object?real, [Optional] object?imag) { if (real == null) { throw PythonOps.TypeError($"complex() first argument must be a string or a number, not '{PythonTypeOps.GetName(real)}'"); } if (imag == null) { throw PythonOps.TypeError($"complex() second argument must be a number, not '{PythonTypeOps.GetName(real)}'"); } Complex imag2; if (imag is Missing) { imag2 = Complex.Zero; } else { if (real is string) { throw PythonOps.TypeError("complex() can't take second arg if first is a string"); } if (imag is string) { throw PythonOps.TypeError("complex() second arg can't be a string"); } if (!Converter.TryConvertToComplex(imag, out imag2)) { throw PythonOps.TypeError($"complex() second argument must be a number, not '{PythonTypeOps.GetName(real)}'"); } } Complex real2; if (real is Missing) { real2 = Complex.Zero; } else if (real is string) { real2 = LiteralParser.ParseComplex((string)real); } else if (real is Extensible <string> ) { real2 = LiteralParser.ParseComplex(((Extensible <string>)real).Value); } else if (real is Complex) { if (imag is Missing && cls == TypeCache.Complex) { return(real); } else { real2 = (Complex)real; } } else if (!Converter.TryConvertToComplex(real, out real2)) { throw PythonOps.TypeError($"complex() first argument must be a string or a number, not '{PythonTypeOps.GetName(real)}'"); } double real3 = real2.Real - imag2.Imaginary; double imag3 = real2.Imaginary + imag2.Real; if (cls == TypeCache.Complex) { return(new Complex(real3, imag3)); } else { return(cls.CreateInstance(context, real3, imag3)); } }
public static Complex Mod(CodeContext context, Complex x, Complex y) { throw PythonOps.TypeError("can't mod complex numbers"); }
public static void DeleteItem(List <T> l, Slice slice) { if (slice == null) { throw PythonOps.TypeError("List<T> indices must be slices or integers"); } int start, stop, step; // slice is sealed, indices can't be user code... slice.indices(l.Count, out start, out stop, out step); if (step > 0 && (start >= stop)) { return; } if (step < 0 && (start <= stop)) { return; } if (step == 1) { int i = start; for (int j = stop; j < l.Count; j++, i++) { l[i] = l[j]; } l.RemoveRange(i, stop - start); return; } else if (step == -1) { int i = stop + 1; for (int j = start + 1; j < l.Count; j++, i++) { l[i] = l[j]; } l.RemoveRange(i, start - stop); return; } else if (step < 0) { // find "start" we will skip in the 1,2,3,... order int i = start; while (i > stop) { i += step; } i -= step; // swap start/stop, make step positive stop = start + 1; start = i; step = -step; } int curr, skip, move; // skip: the next position we should skip // curr: the next position we should fill in data // move: the next position we will check curr = skip = move = start; while (curr < stop && move < stop) { if (move != skip) { l[curr++] = l[move]; } else { skip += step; } move++; } while (stop < l.Count) { l[curr++] = l[stop++]; } l.RemoveRange(curr, l.Count - curr); }
internal static List <byte> /*!*/ FromHex(string /*!*/ @string) { if (@string == null) { throw PythonOps.TypeError("expected str, got NoneType"); } List <byte> res = new List <byte>(); for (int i = 0; i < @string.Length; i++) { char c = @string[i]; int iVal = 0; if (Char.IsDigit(c)) { iVal = (c - '0') * 16; } else if (c >= 'A' && c <= 'F') { iVal = (c - 'A' + 10) * 16; } else if (c >= 'a' && c <= 'f') { iVal = (c - 'a' + 10) * 16; } else if (c == ' ') { continue; } else { throw PythonOps.ValueError("non-hexadecimal number found in fromhex() arg at position {0}", i); } i++; if (i == @string.Length) { throw PythonOps.ValueError("non-hexadecimal number found in fromhex() arg at position {0}", i - 1); } c = @string[i]; if (Char.IsDigit(c)) { iVal += c - '0'; } else if (c >= 'A' && c <= 'F') { iVal += c - 'A' + 10; } else if (c >= 'a' && c <= 'f') { iVal += c - 'a' + 10; } else { throw PythonOps.ValueError("non-hexadecimal number found in fromhex() arg at position {0}", i); } res.Add((byte)iVal); } return(res); }