コード例 #1
0
ファイル: ObjectOps.cs プロジェクト: SueDou/python
        /// <summary>
        /// Implements the default __reduce_ex__ method as specified by PEP 307 case 2 (new-style instance, protocol 0 or 1)
        /// </summary>
        internal static PythonTuple ReduceProtocol0(CodeContext /*!*/ context, object self)
        {
            // CPython implements this in copy_reg._reduce_ex

            PythonType myType = DynamicHelpers.GetPythonType(self); // PEP 307 calls this "D"

            ThrowIfNativelyPickable(myType);

            object getState;
            bool   hasGetState = PythonOps.TryGetBoundAttr(context, self, "__getstate__", out getState);

            object slots;

            if (PythonOps.TryGetBoundAttr(context, myType, "__slots__", out slots) && PythonOps.Length(slots) > 0 && !hasGetState)
            {
                // ??? does this work with superclass slots?
                throw PythonOps.TypeError("a class that defines __slots__ without defining __getstate__ cannot be pickled with protocols 0 or 1");
            }

            PythonType closestNonPythonBase = FindClosestNonPythonBase(myType); // PEP 307 calls this "B"

            object func = context.LanguageContext.PythonReconstructor;

            object funcArgs = PythonTuple.MakeTuple(
                myType,
                closestNonPythonBase,
                TypeCache.Object == closestNonPythonBase ? null : PythonCalls.Call(context, closestNonPythonBase, self)
                );

            object state;

            if (hasGetState)
            {
                state = PythonOps.CallWithContext(context, getState);
            }
            else
            {
                IPythonObject ipo = self as IPythonObject;
                if (ipo != null)
                {
                    state = ipo.Dict;
                }
                else if (!PythonOps.TryGetBoundAttr(context, self, "__dict__", out state))
                {
                    state = null;
                }
            }
            if (!PythonOps.IsTrue(state))
            {
                state = null;
            }

            return(PythonTuple.MakeTuple(func, funcArgs, state));
        }
コード例 #2
0
 public static object __ne__(CodeContext context, object self, object other)
 {
     if (PythonTypeOps.TryInvokeBinaryOperator(context, self, other, "__eq__", out object res))
     {
         if (res is NotImplementedType)
         {
             return(NotImplementedType.Value);
         }
         return(!PythonOps.IsTrue(res));
     }
     return(NotImplementedType.Value);
 }
コード例 #3
0
ファイル: BoolOps.cs プロジェクト: rudimk/dlr-dotnet
 public static bool __new__(object cls, object o)
 {
     return(PythonOps.IsTrue(o));
 }