Exemplo n.º 1
0
 //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;
 }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
 public WeakRefWrapper(DynamicType parent)
 {
     this.parentType = parent;
 }
 public static object Make(DynamicType cls)
 {
     return Make(cls, default(SByte));
 }
Exemplo n.º 5
0
 /// <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);
 }
Exemplo n.º 6
0
        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;
        }
Exemplo n.º 7
0
 public static object MakeModule(ICallerContext context, DynamicType cls, [ParamDict] Dict kwDict\u03c4, params object[] args\u03c4)
 {
     return MakeModule(context, cls, args\u03c4);
 }
Exemplo n.º 8
0
 public static object fromkeys(DynamicType cls, object seq, object value)
 {
     return Dict.FromKeys(cls, seq, value);
 }
Exemplo n.º 9
0
        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;
        }
Exemplo n.º 10
0
 public static object Make(DynamicType cls)
 {
     return Make(cls, 0);
 }
Exemplo n.º 11
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);
            }
        }
Exemplo n.º 12
0
        public static object Make(DynamicType cls)
        {
            if (cls == FloatType) return 0.0;

            return cls.ctor.Call(cls);
        }
Exemplo n.º 13
0
 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;
     }
 }
Exemplo n.º 14
0
 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");
     }
 }
Exemplo n.º 15
0
 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));
     }
 }
Exemplo n.º 16
0
 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);
 }
Exemplo n.º 17
0
 public static object fromkeys(DynamicType cls, object seq)
 {
     return Dict.FromKeys(cls, seq, null);
 }
Exemplo n.º 18
0
        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();
        }
Exemplo n.º 19
0
 public static object MakeModule(ICallerContext context, DynamicType cls, params object[] args\u03c4)
 {
     return PythonModule.MakeModule(context, cls);
 }
Exemplo n.º 20
0
 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);
 }
Exemplo n.º 21
0
 public static MethodWrapper MakeUndefined(DynamicType pt, SymbolId name)
 {
     return new MethodWrapper(pt, name);
 }
Exemplo n.º 22
0
        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);
        }
Exemplo n.º 23
0
 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);
 }
Exemplo n.º 24
0
        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);
        }
Exemplo n.º 25
0
 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;
     }
 }
Exemplo n.º 26
0
        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");
            }
        }
Exemplo n.º 27
0
 private static void ThrowIfNativelyPickable(DynamicType type)
 {
     if (Ops.TRUE == NativelyPickleableTypes.Contains(type)) {
         throw Ops.TypeError("can't pickle {0} objects", type.Name);
     }
 }
Exemplo n.º 28
0
 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__");
 }
Exemplo n.º 30
0
 public DictWrapper(DynamicType pt)
 {
     type = pt;
 }