Inheritance: ICodeFormattable, IValueEquality, ICustomTypeDescriptor, ISerializable, IWeakReferenceable, IDynamicMetaObjectProvider, IPythonMembersList, Binding.IFastGettable
Ejemplo n.º 1
0
        public object __cmp__(CodeContext context, object other)
        {
            OldInstance oiOther = other as OldInstance;

            // CPython raises this if called directly, but not via cmp(os,ns) which still calls the user __cmp__
            //if(!(oiOther is OldInstance))
            //    throw Ops.TypeError("instance.cmp(x,y) -> y must be an instance, got {0}", Ops.StringRepr(DynamicHelpers.GetPythonType(other)));

            object res = InternalCompare("__cmp__", other);

            if (res != NotImplementedType.Value)
            {
                return(res);
            }
            if (oiOther != null)
            {
                res = oiOther.InternalCompare("__cmp__", this);
                if (res != NotImplementedType.Value)
                {
                    return(((int)res) * -1);
                }
            }

            return(NotImplementedType.Value);
        }
        private Proc MakeProc(OldInstance instance, string methodName)
        {
            var nakedProc =
                _engine.CreateMethod<Proc<object>>(string.Format("instance.{0}()", methodName), Params("instance"));

            Proc testCase = () => nakedProc(instance);

            return testCase;
        }
Ejemplo n.º 3
0
        public static object __rdivmod__(CodeContext context, object divmod, [NotNull] OldInstance self)
        {
            object value;

            if (self.TryGetBoundCustomMember(context, "__rdivmod__", out value))
            {
                return(PythonCalls.Call(context, value, divmod));
            }

            return(NotImplementedType.Value);
        }
Ejemplo n.º 4
0
        public object Call(CodeContext context, [ParamDictionary] IDictionary <object, object> dict\u00F8, [NotNull] params object[] args\u00F8)
        {
            OldInstance inst = new OldInstance(context, this);
            object      meth;

            if (PythonOps.TryGetBoundAttr(inst, "__init__", out meth))
            {
                PythonCalls.CallWithKeywordArgs(context, meth, args\u00F8, dict\u00F8);
            }
            else if (dict\u00F8.Count > 0 || args\u00F8.Length > 0)
            {
                MakeCallError();
            }
            return(inst);
        }
Ejemplo n.º 5
0
        public object Call(CodeContext context, [NotNull] params object[] args\u00F8)
        {
            OldInstance inst = new OldInstance(context, this);
            object      value;

            // lookup the slot directly - we don't go through __getattr__
            // until after the instance is created.
            if (TryLookupSlot("__init__", out value))
            {
                PythonOps.CallWithContext(context, GetOldStyleDescriptor(context, value, inst, this), args\u00F8);
            }
            else if (args\u00F8.Length > 0)
            {
                MakeCallError();
            }
            return(inst);
        }
Ejemplo n.º 6
0
        private object CompareForwardReverse(object other, string forward, string reverse)
        {
            object res = InternalCompare(forward, other);

            if (res != NotImplementedType.Value)
            {
                return(res);
            }

            OldInstance oi = other as OldInstance;

            if (oi != null)
            {
                // comparison operators are reflexive
                return(oi.InternalCompare(reverse, this));
            }

            return(NotImplementedType.Value);
        }
Ejemplo n.º 7
0
        private void loadPythonClass(string className)
        {
            // first, instantiate the object.
            object classObject = _scope.GetVariable(className);
            _pythonInstance = (OldInstance)_scope.Engine.Operations.CreateInstance(classObject);

            // Pluck fields out of our python module
            loadPins();
            loadParameters();
        }
Ejemplo n.º 8
0
 public OldInstanceException(OldInstance instance) {
     _instance = instance;
 }
Ejemplo n.º 9
0
 public object Call(ICallerContext context, object[] args, string[] names)
 {
     OldInstance inst = new OldInstance(this);
     object meth;
     if (Ops.TryGetAttr(inst, SymbolTable.Init, out meth)) {
         Ops.Call(context, meth, args, names);
     } else {
         Debug.Assert(names.Length != 0);
         throw Ops.TypeError("this constructor takes no arguments");
     }
     return inst;
 }
Ejemplo n.º 10
0
 private object MakeInstance(CodeContext/*!*/ context, object cls, object[] args) {
     OldClass oc = cls as OldClass;
     if (oc != null) {
         OldInstance inst = new OldInstance(context, oc);
         if (args.Length != 0 || PythonOps.HasAttr(context, cls, Symbols.GetInitArgs)) {
             PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, inst, Symbols.Init), args);
         }
         return inst;
     }
     return PythonOps.CallWithContext(context, cls, args);
 }
Ejemplo n.º 11
0
 public MetaOldInstance(Expression/*!*/ expression, BindingRestrictions/*!*/ restrictions, OldInstance/*!*/ value)
     : base(expression, BindingRestrictions.Empty, value) {
     Assert.NotNull(value);
 }
Ejemplo n.º 12
0
 // a version of GetName that also works on old-style classes
 internal static string GetOldName(OldInstance instance) {
     return instance._class.Name;
 }
Ejemplo n.º 13
0
 private object MakeInstance(ICallerContext context, IPythonType cls, object[] args)
 {
     if (cls is OldClass) {
         OldInstance inst = new OldInstance((OldClass)cls);
         if (args.Length != 0 || Ops.HasAttr(context, cls, SymbolTable.GetInitArgs)) {
             Ops.Call(Ops.GetAttr(context, inst, SymbolTable.Init), args);
         }
         return inst;
     }
     return Ops.Call(cls, args);
 }
Ejemplo n.º 14
0
        StoreTyped(OldInstance inst)
        {
            uint size = (uint)Marshal.SizeOf(typeof(PyInstanceObject));
            IntPtr ptr = this.allocator.Alloc(size);
            CPyMarshal.Zero(ptr, size);
            
            CPyMarshal.WriteIntField(ptr, typeof(PyObject), "ob_refcnt", 1);
            CPyMarshal.WritePtrField(ptr, typeof(PyObject), "ob_type", this.PyInstance_Type);

            CPyMarshal.WritePtrField(ptr, typeof(PyInstanceObject), "in_class", 
                this.Store(Builtin.getattr(this.scratchContext, inst, "__class__")));
            CPyMarshal.WritePtrField(ptr, typeof(PyInstanceObject), "in_dict", 
                this.Store(Builtin.getattr(this.scratchContext, inst, "__dict__")));
            
            this.map.Associate(ptr, inst);
            return ptr;
        }
Ejemplo n.º 15
0
 public object Call(ICallerContext context, params object[] args)
 {
     OldInstance inst = new OldInstance(this);
     object value;
     // lookup the slot directly - we don't go through __getattr__
     // until after the instance is created.
     if (TryLookupSlot(SymbolTable.Init, out value)) {
         Ops.Call(Ops.GetDescriptor(value, inst, this), args);
     } else if (args.Length > 0) {
         throw Ops.TypeError("this constructor takes no arguments");
     }
     return inst;
 }
Ejemplo n.º 16
0
 public object Call(CodeContext context, [ParamDictionary] IAttributesCollection dict\u00F8, [NotNull]params object[] args\u00F8) {
     OldInstance inst = new OldInstance(context, this);
     object meth;
     if (PythonOps.TryGetBoundAttr(inst, Symbols.Init, out meth)) {
         PythonCalls.CallWithKeywordArgs(context, meth, args\u00F8, dict\u00F8);
     } else if (dict\u00F8.Count > 0 || args\u00F8.Length > 0) {
         MakeCallError();
     }
     return inst;
 }
Ejemplo n.º 17
0
 public object Call(CodeContext context, [NotNull]params object[] args\u00F8) {
     OldInstance inst = new OldInstance(context, this);
     object value;
     // lookup the slot directly - we don't go through __getattr__
     // until after the instance is created.
     if (TryLookupSlot(Symbols.Init, out value)) {
         PythonOps.CallWithContext(context, GetOldStyleDescriptor(context, value, inst, this), args\u00F8);
     } else if (args\u00F8.Length > 0) {
         MakeCallError();
     }
     return inst;
 }
        /// <summary>
        /// Returns the Python exception associated with a CLR exception
        /// creating a new Python exception if necessary.
        /// </summary>
        public static object ToPython(Exception clrException)
        {
            if (clrException.Data.Contains(pythonExceptionKey)) {
                // this is already associated w/ a CLR exception.
                return clrException.Data[pythonExceptionKey];
            }

            ThreadAbortException ta = clrException as ThreadAbortException;
            if (ta != null) {
                // transform TA w/ our reason into a KeyboardInterrupt exception.
                PythonKeyboardInterruptException reason = ta.ExceptionState as PythonKeyboardInterruptException;
                if (reason != null) return ToPython(reason);
            }

            object res;
            ICustomExceptionConversion ice = clrException as ICustomExceptionConversion;
            if (ice != null) {
                res = ice.ToPythonException();
            } else {
                // this is an exception raised from CLR space crossing
                // into Python space.  We need to create a new Python
                // exception.
                IPythonType pythonType = GetPythonTypeFromCLR(clrException.GetType());

                // create new instance of Python type and save it (we do this directly
                // as we're calling during low-stack situations and don't want to invoke
                // a python method that would do a stack-check).
                res = new OldInstance((OldClass)pythonType);

                Ops.SetAttr(DefaultContext.Default, res, SymbolTable.ExceptionMessage, clrException.Message);
                if (clrException.Message != null) {
                    Ops.SetAttr(DefaultContext.Default, res, SymbolTable.Arguments, Tuple.MakeTuple(clrException.Message));
                } else {
                    Ops.SetAttr(DefaultContext.Default, res, SymbolTable.Arguments, Tuple.MakeTuple());
                }
                //'filename', 'lineno','offset', 'print_file_and_line', 'text'
            }

            OldInstance exRes = res as OldInstance;
            if (exRes != null) AssociateExceptions(clrException, exRes);

            return res;
        }