/// <summary> /// Return a dict that maps slot names to slot values, but only include slots that have been assigned to. /// Looks up slots in base types as well as the current type. /// /// Sort-of Python equivalent (doesn't look up base slots, while the real code does): /// return dict([(slot, getattr(self, slot)) for slot in type(self).__slots__ if hasattr(self, slot)]) /// /// Return null if the object has no __slots__, or empty dict if it has __slots__ but none are initialized. /// </summary> private static PythonDictionary GetInitializedSlotValues(object obj) { PythonDictionary initializedSlotValues = new PythonDictionary(); IList <PythonType> mro = DynamicHelpers.GetPythonType(obj).ResolutionOrder; object slots; object slotValue; foreach (object type in mro) { if (PythonOps.TryGetBoundAttr(type, "__slots__", out slots)) { List <string> slotNames = PythonType.SlotsToList(slots); foreach (string slotName in slotNames) { if (slotName == "__dict__") { continue; } // don't reassign same-named slots from types earlier in the MRO if (initializedSlotValues.__contains__(slotName)) { continue; } if (PythonOps.TryGetBoundAttr(obj, slotName, out slotValue)) { initializedSlotValues[slotName] = slotValue; } } } } if (initializedSlotValues.Count == 0) { return(null); } return(initializedSlotValues); }
public static object?__reduce_ex__(CodeContext /*!*/ context, object self, object protocol) { object objectReduce = PythonOps.GetBoundAttr(context, DynamicHelpers.GetPythonTypeFromType(typeof(object)), "__reduce__"); if (PythonOps.TryGetBoundAttr(context, DynamicHelpers.GetPythonType(self), "__reduce__", out object?myReduce)) { if (!PythonOps.IsRetBool(myReduce, objectReduce)) { // A derived class overrode __reduce__ but not __reduce_ex__, so call // specialized __reduce__ instead of generic __reduce_ex__. // (see the "The __reduce_ex__ API" section of PEP 307) return(PythonOps.CallWithContext(context, myReduce, self) !); } } if (context.LanguageContext.ConvertToInt32(protocol) < 2) { return(ReduceProtocol0(context, self)); } else { return(ReduceProtocol2(context, self)); } }
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)) { 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.GetName(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.GetName(result)); } } throw PythonOps.TypeError("long() argument must be a string or a number, not '{0}'", DynamicHelpers.GetPythonType(x).Name); }
/// <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; IPythonObject ipo = self as IPythonObject; if (ipo != null) { 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 object FastNew(CodeContext /*!*/ context, object o) { Extensible <BigInteger> el; if (o is string) { return(__new__(null, (string)o, 10)); } 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)); } Extensible <string> es = o as Extensible <string>; if (es != null) { // __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, 10)); } 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.GetOldName(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.GetOldName(result)); } } if (o is OldInstance) { throw PythonOps.AttributeError("{0} instance has no attribute '__trunc__'", PythonTypeOps.GetOldName((OldInstance)o)); } else { throw PythonOps.TypeError("int() argument must be a string or a number, not '{0}'", PythonTypeOps.GetName(o)); } }
public override bool ShouldSerializeValue(object component) { object o; return(PythonOps.TryGetBoundAttr(component, _name, out o)); }
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 {