Exemplo n.º 1
0
        internal static object?ReduceProtocol0(CodeContext /*!*/ context, object self)
        {
            var copyreg    = context.LanguageContext.GetCopyRegModule();
            var _reduce_ex = PythonOps.GetBoundAttr(context, copyreg, "_reduce_ex");

            return(PythonOps.CallWithContext(context, _reduce_ex, self, 0));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs the pickle protocol
        /// </summary>
        public static object __reduce_ex__(CodeContext /*!*/ context, object self, object protocol)
        {
            object objectReduce = PythonOps.GetBoundAttr(context, DynamicHelpers.GetPythonTypeFromType(typeof(object)), "__reduce__");
            object myReduce;

            if (PythonOps.TryGetBoundAttr(context, DynamicHelpers.GetPythonType(self), "__reduce__", out 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));
            }
        }
Exemplo n.º 3
0
 public static object __getnewargs__(CodeContext context, Complex self)
 {
     return(PythonTuple.MakeTuple(
                PythonOps.GetBoundAttr(context, self, "real"),
                PythonOps.GetBoundAttr(context, self, "imag")
                ));
 }
Exemplo n.º 4
0
 public static string?GetClassName(object self)
 {
     if (PythonOps.TryGetBoundAttr(DefaultContext.DefaultCLS, self, "__class__", out object?cls))
     {
         return(PythonOps.GetBoundAttr(DefaultContext.DefaultCLS, cls, "__name__").ToString());
     }
     return(null);
 }
Exemplo n.º 5
0
        public static object __new__(CodeContext /*!*/ context, PythonType cls, object x)
        {
            object value = null;

            if (x is string)
            {
                value = ParseFloat((string)x);
            }
            else if (x is Extensible <string> )
            {
                if (!PythonTypeOps.TryInvokeUnaryOperator(context, x, "__float__", out value))
                {
                    value = ParseFloat(((Extensible <string>)x).Value);
                }
            }
            else if (x is char)
            {
                value = ParseFloat(ScriptingRuntimeHelpers.CharToString((char)x));
            }
            else if (x is Complex)
            {
                throw PythonOps.TypeError("can't convert complex to float; use abs(z)");
            }
            else
            {
                object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, "__float__"));
                if (d is double)
                {
                    value = d;
                }
                else if (d is Extensible <double> )
                {
                    value = ((Extensible <double>)d).Value;
                }
                else
                {
                    throw PythonOps.TypeError("__float__ returned non-float (type {0})", PythonTypeOps.GetName(d));
                }
            }

            if (cls == TypeCache.Double)
            {
                return(value);
            }
            else
            {
                return(cls.CreateInstance(context, value));
            }
        }
Exemplo n.º 6
0
        public static object __getnewargs__(CodeContext context, Complex self)
        {
#if CLR2
            if (!Object.ReferenceEquals(self, null))
            {
#endif
            return(PythonTuple.MakeTuple(
                       PythonOps.GetBoundAttr(context, self, "real"),
                       PythonOps.GetBoundAttr(context, self, "imag")
                       ));

#if CLR2
        }

        throw PythonOps.TypeErrorForBadInstance("__getnewargs__ requires a 'complex' object but received a '{0}'", self);
#endif
        }
Exemplo n.º 7
0
        public static object __new__(CodeContext /*!*/ context, PythonType cls, object x)
        {
            if (cls != TypeCache.Single)
            {
                return(cls.CreateInstance(context, x));
            }

            if (x is string)
            {
                return(ParseFloat((string)x));
            }
            else if (x is Extensible <string> )
            {
                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((float)doubleVal);
            }

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

            object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, "__float__"));

            if (d is double)
            {
                return((float)(double)d);
            }
            throw PythonOps.TypeError("__float__ returned non-float (type %s)", DynamicHelpers.GetPythonType(d));
        }
Exemplo n.º 8
0
 public override object GetValue(object component)
 {
     return(PythonOps.GetBoundAttr(DefaultContext.DefaultCLS, component, _name));
 }
Exemplo n.º 9
0
        private static PythonTuple ReduceProtocol2(CodeContext /*!*/ context, object self)
        {
            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 {