public static object __new__(CodeContext context, PythonType type, object o) { object reversed; if (PythonOps.TryGetBoundAttr(context, o, "__reversed__", out reversed)) { return(PythonCalls.Call(context, reversed)); } object boundFunc; PythonTypeSlot getitem; PythonType pt = DynamicHelpers.GetPythonType(o); if (!pt.TryResolveSlot(context, "__getitem__", out getitem) || !getitem.TryGetValue(context, o, pt, out boundFunc) || o is PythonDictionary) { throw PythonOps.TypeError("argument to reversed() must be a sequence"); } int length; if (!DynamicHelpers.GetPythonType(o).TryGetLength(context, o, out length)) { throw PythonOps.TypeError("object of type '{0}' has no len()", DynamicHelpers.GetPythonType(o).Name); } if (type.UnderlyingSystemType == typeof(ReversedEnumerator)) { return(new ReversedEnumerator((int)length, boundFunc)); } return(type.CreateInstance(context, length, getitem)); }
public static object?__new__(CodeContext context, [NotNone] PythonType type, object?o) { if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__reversed__", out object?res)) { return(res); } object boundFunc; PythonTypeSlot getitem; PythonType pt = DynamicHelpers.GetPythonType(o); if (o is null || o is PythonDictionary || !pt.TryResolveSlot(context, "__getitem__", out getitem) || !getitem.TryGetValue(context, o, pt, out boundFunc)) { throw PythonOps.TypeError("argument to reversed() must be a sequence"); } int length; if (!DynamicHelpers.GetPythonType(o).TryGetLength(context, o, out length)) { throw PythonOps.TypeError("object of type '{0}' has no len()", PythonOps.GetPythonTypeName(o)); } if (type.UnderlyingSystemType == typeof(ReversedEnumerator)) { return(new ReversedEnumerator(length, o, boundFunc)); } return(type.CreateInstance(context, length, getitem)); }
private static bool IsHashable(SetCollection asSet) { PythonTypeSlot pts; PythonType pt = DynamicHelpers.GetPythonType(asSet); object slotValue; return(pt.TryResolveSlot(DefaultContext.Default, "__hash__", out pts) && pts.TryGetValue(DefaultContext.Default, asSet, pt, out slotValue) && slotValue != null); }
private static bool HasPythonProtocol(Type t, string name) { if (t.FullName.StartsWith(NewTypeMaker.TypePrefix)) { return(true); } PythonType dt = DynamicHelpers.GetPythonTypeFromType(t); if (dt == null) { return(false); } PythonTypeSlot tmp; return(dt.TryResolveSlot(DefaultContext.Default, name, out tmp)); }