//public static MethodWrapper MakeDefault() { return new MethodWrapper(null, true, true); } public MethodWrapper(DynamicType pt, SymbolId name) { this.pythonType = pt; this.name = name; string fieldname = SymbolTable.IdToString(name) + "F"; this.myField = typeof(DynamicType).GetField(fieldname); this.isObjectMethod = true; this.isBuiltinMethod = true; this.isSuperTypeMethod = true; }
public static MethodWrapper Make(DynamicType pt, SymbolId name) { MethodWrapper ret = new MethodWrapper(pt, name); object meth; if (pt.dict.TryGetValue(name, out meth)) { object otherMeth; if (!pt.TryLookupSlotInBases(DefaultContext.Default, name, out otherMeth) || otherMeth != meth) { ret.SetDeclaredMethod(meth); } else { // they did __foo__ = myBase.__foo__, we'll just ignore it... ret.UpdateFromBases(pt.MethodResolutionOrder); } } else { ret.UpdateFromBases(pt.MethodResolutionOrder); } //pt.dict[name] = ret; //??? return ret; }
public WeakRefWrapper(DynamicType parent) { this.parentType = parent; }
public static object Make(DynamicType cls) { return Make(cls, default(SByte)); }
/// <summary> /// Returns the closest base class (in terms of MRO) that isn't defined in Python code /// </summary> private static DynamicType FindClosestNonPythonBase(DynamicType type) { foreach (object pythonBase in type.MethodResolutionOrder) { if (pythonBase is ReflectedType) { return (DynamicType)pythonBase; } } throw Ops.TypeError("can't pickle {0} instance: no non-Python bases found", type.Name); }
public static MethodWrapper MakeForObject(DynamicType pt, SymbolId name, Delegate func) { MethodWrapper ret = new MethodWrapper(pt, name); ret.isObjectMethod = true; ret.isBuiltinMethod = true; ret.isSuperTypeMethod = false; ret.func = BuiltinFunction.MakeMethod((string)SymbolTable.IdToString(name), func.Method, FunctionType.Function); ret.funcAsFunc = ret.func as BuiltinFunction; //pt.dict[name] = ret; return ret; }
public static object MakeModule(ICallerContext context, DynamicType cls, [ParamDict] Dict kwDict\u03c4, params object[] args\u03c4) { return MakeModule(context, cls, args\u03c4); }
public static object fromkeys(DynamicType cls, object seq, object value) { return Dict.FromKeys(cls, seq, value); }
public static object DefaultNewClsKW(ICallerContext context, DynamicType type\u03c4, [ParamDict] Dict kwargs\u03ba, params object[] args\u03b1) { object res = DefaultNew(context, type\u03c4, args\u03b1); if (kwargs\u03ba.Count > 0) { foreach (KeyValuePair<object, object> kvp in (IDictionary<object, object>)kwargs\u03ba) { Ops.SetAttr(context, res, SymbolTable.StringToId(kvp.Key.ToString()), kvp.Value); } } return res; }
public static object Make(DynamicType cls) { return Make(cls, 0); }
public static object Make(DynamicType cls, object x) { if (cls == FloatType) { if (x is string) { return ParseFloat((string)x); } if (x is char) { return ParseFloat(Ops.Char2String((char)x)); } double doubleVal; if (Converter.TryConvertToDouble(x, out doubleVal)) return doubleVal; if (x is Complex64) throw Ops.TypeError("can't convert complex to float; use abs(z)"); object d = Ops.Call(Ops.GetAttr(DefaultContext.Default, x, SymbolTable.ConvertToFloat)); if (d is double) return d; throw Ops.TypeError("__float__ returned non-float (type %s)", Ops.GetDynamicType(d)); } else { return cls.ctor.Call(cls, x); } }
public static object Make(DynamicType cls) { if (cls == FloatType) return 0.0; return cls.ctor.Call(cls); }
public static StructTime Make(DynamicType cls, int year, int month, int day, int hour, int minute, int second, int dayOfWeek, int dayOfYear, int isDst) { if (cls == Ops.GetDynamicTypeFromType(typeof(StructTime))) { return new StructTime(year, month, day, hour, minute, second, dayOfWeek, dayOfYear, isDst); } else { StructTime st = cls.ctor.Call(cls, year, month, day, hour, minute, second, dayOfWeek, dayOfYear, isDst) as StructTime; if (st == null) throw Ops.TypeError("{0} is not a subclass of time.struct_time", cls); return st; } }
public void RemoveSubclass(DynamicType subclass) { lock (subclass) { foreach (WeakReference subType in subclasses) { if (subclass == (subType.Target as DynamicType)) { subclasses.Remove(subType); return; } } Debug.Assert(false, "subclass was not found"); } }
public void AddSubclass(DynamicType subclass) { // stored as a weak ref so when GC collects the subtypes we can // get rid of our reference. lock (subclass) { subclasses.Add(new WeakReference(subclass, true)); } }
public static Tuple Reduce(Dict items, DynamicType type) { object[] keys = new object[items.Keys.Count]; items.Keys.CopyTo(keys, 0); return Tuple.MakeTuple(type, Tuple.MakeTuple(new List(keys)), null); }
public static object fromkeys(DynamicType cls, object seq) { return Dict.FromKeys(cls, seq, null); }
public static object DefaultNewKW(ICallerContext context, DynamicType type\u03c4, [ParamDict] Dict kwargs\u03ba, params object[] args\u03b1) { if (type\u03c4 == null) throw Ops.TypeError("__new__ expected type object, got {0}", Ops.StringRepr(Ops.GetDynamicType(type\u03c4))); CheckInitArgs(context, kwargs\u03ba, args\u03b1, type\u03c4); return type\u03c4.AllocateObject(); }
public static object MakeModule(ICallerContext context, DynamicType cls, params object[] args\u03c4) { return PythonModule.MakeModule(context, cls); }
public static object NonDefaultNew(ICallerContext context, DynamicType type\u03c4, params object[] args\u03b1) { if (type\u03c4 == null) throw Ops.TypeError("__new__ expected type object, got {0}", Ops.StringRepr(Ops.GetDynamicType(type\u03c4))); if (args\u03b1 == null) args\u03b1 = new object[1]; return type\u03c4.AllocateObject(args\u03b1); }
public static MethodWrapper MakeUndefined(DynamicType pt, SymbolId name) { return new MethodWrapper(pt, name); }
public static object NonDefaultNewKWNoParams(ICallerContext context, DynamicType type\u03c4, [ParamDict] Dict kwargs\u03ba) { if (type\u03c4 == null) throw Ops.TypeError("__new__ expected type object, got {0}", Ops.StringRepr(Ops.GetDynamicType(type\u03c4))); return type\u03c4.AllocateObject(kwargs\u03ba, Ops.EMPTY); }
public static object OverloadedNewBasic(ICallerContext context, BuiltinFunction overloads\u03bf, DynamicType type\u03c4, params object[] args\u03b1) { if (type\u03c4 == null) throw Ops.TypeError("__new__ expected type object, got {0}", Ops.StringRepr(Ops.GetDynamicType(type\u03c4))); if (args\u03b1 == null) args\u03b1 = new object[1]; return overloads\u03bf.Call(context, args\u03b1); }
public static object OverloadedNewKW(ICallerContext context, BuiltinFunction overloads\u03bf, DynamicType type\u03c4, [ParamDict] Dict kwargs\u03ba) { if (type\u03c4 == null) throw Ops.TypeError("__new__ expected type object, got {0}", Ops.StringRepr(Ops.GetDynamicType(type\u03c4))); object[] finalArgs; string[] names; DynamicType.GetKeywordArgs(kwargs\u03ba, Ops.EMPTY, out finalArgs, out names); return overloads\u03bf.CallHelper(context, finalArgs, names, null); }
public static Pickler Make(DynamicType cls, [DefaultParameterValue(null)] object file, [DefaultParameterValue(null)] object protocol, [DefaultParameterValue(null)] object bin ) { if (cls == Ops.GetDynamicTypeFromClsOnlyType(typeof(Pickler))) { // For undocumented (yet tested in official CPython tests) list-based pickler, the // user could do something like Pickler(1), which would create a protocol-1 pickler // with an internal string output buffer (retrievable using GetValue()). For a little // more info, see // https://sourceforge.net/tracker/?func=detail&atid=105470&aid=939395&group_id=5470 int intProtocol; if (file == null) { file = new PythonReadableFileOutput(new PythonStringIO.StringO()); } else if (Converter.TryConvertToInt32(file, out intProtocol)) { return new Pickler((IFileOutput) new PythonReadableFileOutput(new PythonStringIO.StringO()), intProtocol, bin); } return new Pickler(file, protocol, bin); } else { Pickler pickler = cls.ctor.Call(cls, file, protocol, bin) as Pickler; if (pickler == null) throw Ops.TypeError("{0} is not a subclass of Pickler", cls); return pickler; } }
private static void CheckInitArgs(ICallerContext context, Dict dict, object[] args, DynamicType pt) { object initMethod; if (((args != null && args.Length > 0) || (dict != null && dict.Count > 0)) && (pt.TryGetSlot(context, SymbolTable.Init, out initMethod) || pt.TryLookupSlotInBases(context, SymbolTable.Init, out initMethod)) && initMethod == Init) { throw Ops.TypeError("default __new__ does not take parameters"); } }
private static void ThrowIfNativelyPickable(DynamicType type) { if (Ops.TRUE == NativelyPickleableTypes.Contains(type)) { throw Ops.TypeError("can't pickle {0} objects", type.Name); } }
public static PythonModule MakeModule(ICallerContext context, DynamicType cls, params object[] args\u03c4) { if (cls.IsSubclassOf(TypeCache.Module)) { return new PythonModule(context); } throw Ops.TypeError("{0} is not a subtype of module", cls.__name__); }
public static object Make(DynamicType cls, object value) { if (cls != SByteType) { throw Ops.TypeError("SByte.__new__: first argument must be SByte type."); } IConvertible valueConvertible; if ((valueConvertible = value as IConvertible) != null) { switch (valueConvertible.GetTypeCode()) { case TypeCode.Byte: return (SByte)(Byte)value; case TypeCode.SByte: return (SByte)(SByte)value; case TypeCode.Int16: return (SByte)(Int16)value; case TypeCode.UInt16: return (SByte)(UInt16)value; case TypeCode.Int32: return (SByte)(Int32)value; case TypeCode.UInt32: return (SByte)(UInt32)value; case TypeCode.Int64: return (SByte)(Int64)value; case TypeCode.UInt64: return (SByte)(UInt64)value; case TypeCode.Single: return (SByte)(Single)value; case TypeCode.Double: return (SByte)(Double)value; } } if (value is String) { return SByte.Parse((String)value); } else if (value is BigInteger) { return (SByte)(BigInteger)value; } else if (value is ExtensibleInt) { return (SByte)((ExtensibleInt)value).value; } else if (value is ExtensibleLong) { return (SByte)((ExtensibleLong)value).Value; } else if (value is ExtensibleFloat) { return (SByte)((ExtensibleFloat)value).value; } else if (value is Enum) { return Converter.CastEnumToSByte(value); } throw Ops.ValueError("invalid value for SByte.__new__"); }
public DictWrapper(DynamicType pt) { type = pt; }