Example #1
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, object x) {
            if (cls == TypeCache.Double) {
                if (x is string) {
                    return ParseFloat((string)x);
                } else if (x is Extensible<string>) {
                    object res;
                    if (PythonTypeOps.TryInvokeUnaryOperator(context, x, Symbols.ConvertToFloat, out res)) {
                        return res;
                    }
                    return ParseFloat(((Extensible<string>)x).Value);
                } else if (x is char) {
                    return ParseFloat(ScriptingRuntimeHelpers.CharToString((char)x));
                }

                double doubleVal;
                if (Converter.TryConvertToDouble(x, out doubleVal)) return doubleVal;

                if (x is Complex64) throw PythonOps.TypeError("can't convert complex to float; use abs(z)");

                object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, Symbols.ConvertToFloat));
                if (d is double) return d;
                throw PythonOps.TypeError("__float__ returned non-float (type %s)", DynamicHelpers.GetPythonType(d));
            } else {
                return cls.CreateInstance(context, x);
            }
        }
Example #2
0
 public DynamicStackFrame(CodeContext context, MethodBase method, string funcName, string filename, int line) {
     _context = context;
     _funcName = funcName;
     _filename = filename;
     _lineNo = line;
     _method = method;
 }
Example #3
0
        public static object __new__(CodeContext context, PythonType cls, object x) {
            Extensible<string> es;

            if (x is string) {
                return ReturnBigInteger(context, cls, ParseBigIntegerSign((string)x, 10));
            } else if ((es = x as Extensible<string>) != null) {
                object value;
                if (PythonTypeOps.TryInvokeUnaryOperator(context, x, Symbols.ConvertToLong, out value)) {
                    return ReturnBigInteger(context, cls, (BigInteger)value);
                }

                return ReturnBigInteger(context, cls, ParseBigIntegerSign(es.Value, 10));
            }

            BigInteger intVal;
            if (Converter.TryConvertToBigInteger(x, out intVal)) {
                if (Object.Equals(intVal, null)) throw PythonOps.TypeError("can't convert {0} to long", PythonTypeOps.GetName(x));

                return ReturnBigInteger(context, cls, intVal);
            }


            if (x is Complex64) throw PythonOps.TypeError("can't convert complex to long; use long(abs(z))");

            throw PythonOps.ValueError("long argument must be convertible to long (string, number, or type that defines __long__, got {0})",
                StringOps.Quote(PythonOps.GetPythonTypeName(x)));
        }
Example #4
0
        public CodeContext(Scope scope, LanguageContext languageContext, CodeContext parent) {
            Assert.NotNull(languageContext);

            _languageContext = languageContext;
            _scope = scope;
            _parent = parent;
        }
 internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) {
     IWeakReferenceable reference;
     if (context.GetPythonContext().TryConvertToWeakReferenceable(instance, out reference)) {
         return reference.SetWeakRef(new WeakRefTracker(value, instance));
     }
     return false;
 }
Example #6
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, object x) {
            if (cls == TypeCache.Double) {
                if (x is string) {
                    return ParseFloat((string)x);
                } else if (x is Extensible<string>) {
                    object res;
                    if (PythonTypeOps.TryInvokeUnaryOperator(context, x, "__float__", out res)) {
                        return res;
                    }
                    return ParseFloat(((Extensible<string>)x).Value);
                } else if (x is char) {
                    return ParseFloat(ScriptingRuntimeHelpers.CharToString((char)x));
                }

                if (x is Complex) {
                    throw PythonOps.TypeError("can't convert complex to float; use abs(z)");
                }

                object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, "__float__"));
                if (d is double) {
                    return d;
                } else if (d is Extensible<double>) {
                    return ((Extensible<double>)d).Value;
                }

                throw PythonOps.TypeError("__float__ returned non-float (type {0})", PythonTypeOps.GetName(d));
            } else {
                return cls.CreateInstance(context, x);
            }
        }
 internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) {
     IWeakReferenceable reference = instance as IWeakReferenceable;
     if (reference != null) {
         return reference.SetWeakRef(new WeakRefTracker(value, instance));
     }
     return false;
 }
        internal override bool TryDeleteValue(CodeContext context, object instance, PythonType owner) {
            if (_deleter == null || instance == null) {
                return base.TryDeleteValue(context, instance, owner);
            }

            CallTarget(context, null, new MethodInfo[] { _deleter }, instance);
            return true;
        }
        internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) {
            if (instance != null) {
                Setter(instance, value);
                return true;
            }

            return false;
        }
 internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
     try {
         value = PythonOps.GetUserDescriptor(Value, instance, owner);
         return true;
     } catch (MissingMemberException) {
         value = null;
         return false;
     }
 }
        internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
            if (Getter.Length == 0 || instance == null) {
                value = null;
                return false;
            }

            value = CallGetter(context, null, instance, ArrayUtils.EmptyObjects);
            return true;
        }
        internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
            if (Getter.Length == 0 || (instance == null && Getter[0].IsDefined(typeof(WrapperDescriptorAttribute), false))) {
                value = null;
                return false;
            }

            value = CallGetter(context, null, instance, ArrayUtils.EmptyObjects);
            return true;
        }
Example #13
0
        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);
            }
        }
Example #14
0
        // This can produce a IsCallable rule that returns the immutable constant isCallable.
        // Beware that objects can have a mutable callable property. Eg, in Python, assign or delete the __call__ attribute.
        public static bool MakeIsCallableRule(CodeContext context, object self, bool isCallable, RuleBuilder rule) {
            rule.MakeTest(CompilerHelpers.GetType(self));
            rule.Target =
                rule.MakeReturn(
                    context.LanguageContext.Binder,
                    Ast.Constant(isCallable)
                );

            return true;
        }
        internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
            if (instance != null) {
                value = Getter(instance);
                PythonOps.CheckInitializedAttribute(value, instance, _name);
                return true;
            }

            value = this;
            return true;
        }
Example #16
0
        public static object __new__(CodeContext context, PythonType pythonType, ICollection items) {
            Type type = pythonType.UnderlyingSystemType.GetElementType();

            Array res = Array.CreateInstance(type, items.Count);

            int i = 0;
            foreach (object item in items) {
                res.SetValue(Converter.Convert(item, type), i++);
            }

            return res;
        }
Example #17
0
        public static object __new__(CodeContext context, PythonType cls, string s, int radix) {
            if (radix == 16 || radix == 8 || radix == 2) {
                s = Int32Ops.TrimRadix(s, radix);
            }

            if (cls == TypeCache.BigInteger) {
                return ParseBigIntegerSign(s, radix);
            } else {
                BigInteger res = ParseBigIntegerSign(s, radix);
                return cls.CreateInstance(context, res);
            }
        }
Example #18
0
        public static List GetMemberNames(CodeContext/*!*/ context, Assembly self) {
            Debug.Assert(self != null);
            List ret = DynamicHelpers.GetPythonTypeFromType(self.GetType()).GetMemberNames(context);

            foreach (object o in GetReflectedAssembly(context, self).Keys) {
                if (o is string) {
                    ret.AddNoLock((string)o);
                }
            }

            return ret;
        }
Example #19
0
        internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
            if (instance == null) {
                if (owner == TypeCache.Null) {
                    value = owner;
                } else {
                    value = DynamicHelpers.GetPythonType(owner);
                }
            } else {
                value = DynamicHelpers.GetPythonType(instance);
            }

            return true;
        }
Example #20
0
        internal override object CreateInstance(CodeContext context) {
            if (_ctorSite0 == null) {
                Interlocked.CompareExchange(
                    ref _ctorSite0,
                    CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object>>.Create(
                        PythonContext.GetContext(context).DefaultBinderState.InvokeOne
                    ),
                    null
                );
            }

            return _ctorSite0.Target(_ctorSite0, context, Type.Ctor, Type);
        }
Example #21
0
        private static TopNamespaceTracker GetReflectedAssembly(CodeContext/*!*/ context, Assembly assem) {
            Debug.Assert(assem != null);
            lock (assemblyMap) {
                TopNamespaceTracker reflectedAssembly;
                if (assemblyMap.TryGetValue(assem, out reflectedAssembly))
                    return reflectedAssembly;

                reflectedAssembly = new TopNamespaceTracker(context.LanguageContext.DomainManager);
                reflectedAssembly.LoadAssembly(assem);
                assemblyMap[assem] = reflectedAssembly;

                return reflectedAssembly;
            }
        }
Example #22
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            if (cls == TypeCache.Double) {
                object value;
                IPythonObject po = s as IPythonObject;
                if (po != null &&
                    PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, "__float__", out value)) {
                    return value;
                }

                return ParseFloat(s.MakeString());
            }

            return cls.CreateInstance(context, s);
        }
Example #23
0
        public IList<object> GetMemberNames(CodeContext context) {
            Dictionary<string, string> members = new Dictionary<string, string>();
            foreach (MemberInfo mi in Type.GetMembers()) {
                if (mi.MemberType != MemberTypes.Constructor) {
                    members[mi.Name] = mi.Name;
                }
            }

            List<object> res = new List<object>();
            foreach (string key in members.Keys) {
                res.Add(key);
            }
            return res;
        }
Example #24
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            if (cls == TypeCache.Double) {
                object value;
                IPythonObject po = s as IPythonObject;
                if (po != null &&
                    PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, Symbols.ConvertToFloat, out value)) {
                    return value;
                }

                return ParseFloat(StringOps.FromByteArray(s));
            }

            return cls.CreateInstance(context, s);
        }
Example #25
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            if (cls == TypeCache.BigInteger) {
                object value;
                IPythonObject po = s as IPythonObject;
                if (po != null &&
                    PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, Symbols.ConvertToLong, out value)) {
                    return value;
                }

                return ParseBigIntegerSign(s.MakeString(), 10);
            }

            return cls.CreateInstance(context, ParseBigIntegerSign(s.MakeString(), 10));
        }
Example #26
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            object value;
            IPythonObject po = s as IPythonObject;
            if (po == null ||
                !PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, "__long__", out value)) {
                    value = ParseBigIntegerSign(s.MakeString(), 10);
            }

            if (cls == TypeCache.BigInteger) {
                return value;
            } else {
                // derived long creation...
                return cls.CreateInstance(context, value);
            }
        }
        internal object GetValue(CodeContext context, object instance, PythonType owner) {
            if (_descVersion == UserDescriptorFalse) {
                return _value;
            } else if (_descVersion != DynamicHelpers.GetPythonType(_value).Version) {
                CalculateDescriptorInfo();
                if (_descVersion == UserDescriptorFalse) {
                    return _value;
                }
            }

            object res;
            Debug.Assert(_desc.GetAlwaysSucceeds);
            _desc.TryGetValue(context, _value, DynamicHelpers.GetPythonType(_value), out res);
            return PythonContext.GetContext(context).Call(context, res, instance, owner);
        }
Example #28
0
        public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
            // First, check for subclasses of bytearray/bytes
            object value;
            IPythonObject po = s as IPythonObject;
            if (po == null ||
                !PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, po, "__float__", out value)) {
                // If __float__oes not exist, just parse the string normally
                value = ParseFloat(s.MakeString());
            }

            if (cls == TypeCache.Double) {
                return value;
            } else { 
                return cls.CreateInstance(context, value);
            }
        }
Example #29
0
        public static object GetBoundMember(CodeContext/*!*/ context, Assembly self, string name) {
            TopNamespaceTracker reflectedAssembly = GetReflectedAssembly(context, self);

            if (name == "__dict__") {
                return new PythonDictionary(new WrapperDictionaryStorage(reflectedAssembly));
            }
            MemberTracker mem = reflectedAssembly.TryGetPackageAny(name);
            if (mem != null) {
                if (mem.MemberType == TrackerTypes.Type) {
                    return DynamicHelpers.GetPythonTypeFromType(((TypeTracker)mem).Type);
                }
                // namespace or type collision
                return mem;
            }
            return OperationFailed.Value;
        }
Example #30
0
        internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) {
            if (instance == null) return false;

            IPythonObject sdo = instance as IPythonObject;
            if (sdo == null) {
                throw PythonOps.TypeError("__class__ assignment: only for user defined types");
            }

            PythonType dt = value as PythonType;
            if (dt == null) throw PythonOps.TypeError("__class__ must be set to new-style class, not '{0}' object", DynamicHelpers.GetPythonType(value).Name);

            if(dt.UnderlyingSystemType != DynamicHelpers.GetPythonType(instance).UnderlyingSystemType)
                throw PythonOps.TypeErrorForIncompatibleObjectLayout("__class__ assignment", DynamicHelpers.GetPythonType(instance), dt.UnderlyingSystemType);

            sdo.SetPythonType(dt);
            return true;
        }