internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value)
        {
            if (instance == null)
            {
                value = this;
                return(true);
            }

            IWeakReferenceable reference;

            if (context.GetPythonContext().TryConvertToWeakReferenceable(instance, out reference))
            {
                WeakRefTracker tracker = reference.GetWeakRef();
                if (tracker == null || tracker.HandlerCount == 0)
                {
                    value = null;
                }
                else
                {
                    value = tracker.GetHandlerCallback(0);
                }
                return(true);
            }

            value = null;
            return(false);
        }
Пример #2
0
            public @ref(CodeContext context, object @object, object callback)
            {
                _context = context;
                WeakRefTracker wrt = WeakRefHelpers.InitializeWeakRef(_context.GetPythonContext(), this, @object, callback);

                _target   = new WeakHandle(@object, false);
                _targetId = wrt.TargetId;
            }
        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);
        }
Пример #4
0
        public static void unregister(CodeContext context, object func)
        {
            var functions = context.GetPythonContext().GetModuleState(_registry_key) as List <FunctionDescriptor>;

            if (functions != null)
            {
                lock (functions) {
                    functions.RemoveAll(x => PythonOps.Compare(context, x.Func, func) == 0);
                }
            }
        }
Пример #5
0
        public static void _clear(CodeContext context)
        {
            var functions = context.GetPythonContext().GetModuleState(_registry_key) as List <FunctionDescriptor>;

            if (functions != null)
            {
                lock (functions) {
                    functions.Clear();
                }
            }
        }
Пример #6
0
        public static int _ncallbacks(CodeContext context)
        {
            int result    = 0;
            var functions = context.GetPythonContext().GetModuleState(_registry_key) as List <FunctionDescriptor>;

            if (functions != null)
            {
                lock (functions) {
                    result = functions.Count;
                }
            }
            return(result);
        }
Пример #7
0
        private static void AddFinalizer(CodeContext /*!*/ context, PythonType dt, object newObject)
        {
            // check if object has finalizer...
            PythonTypeSlot dummy;

            if (dt.TryResolveSlot(context, "__del__", out dummy))
            {
                IWeakReferenceable iwr = context.GetPythonContext().ConvertToWeakReferenceable(newObject);
                Debug.Assert(iwr != null);

                InstanceFinalizer nif = new InstanceFinalizer(context, newObject);
                iwr.SetFinalizer(new WeakRefTracker(iwr, nif, nif));
            }
        }
Пример #8
0
        public static object register(CodeContext context, object func, [ParamDictionary] IDictionary <object, object> kwargs, params object[] args)
        {
            if (!PythonOps.IsCallable(context, func))
            {
                throw PythonOps.TypeError("the first argument must be callable");
            }

            var functions = context.GetPythonContext().GetModuleState(_registry_key) as List <FunctionDescriptor>;

            if (functions != null)
            {
                lock (functions) {
                    functions.Add(new FunctionDescriptor(func, kwargs, args));
                }
            }
            return(func);
        }
Пример #9
0
        public static void _run_exitfuncs(CodeContext context)
        {
            var pc        = context.GetPythonContext();
            var functions = pc.GetModuleState(_registry_key) as List <FunctionDescriptor>;

            if (functions != null)
            {
                Exception lastException = null;
                lock (functions) {
                    for (int i = functions.Count - 1; i >= 0; i--)
                    {
                        var func = functions[i];
                        try {
                            if (func.KeywordArgs.Count > 0)
                            {
                                pc.CallWithKeywordsAndContext(context, func.Func, func.Args, func.KeywordArgs);
                            }
                            else
                            {
                                pc.CallWithContext(context, func.Func, func.Args);
                            }
                        } catch (SystemExitException ex) {
                            lastException = ex;
                        } catch (Exception ex) {
                            lastException = ex;
                            PythonOps.PrintWithDest(context, pc.SystemStandardError, "Error in atexit._run_exitfuncs:\n");
                            PythonOps.PrintException(context, ex);
                        }
                    }
                }

                if (lastException != null)
                {
                    throw lastException;
                }
            }
        }
Пример #10
0
        private static void AddFinalizer(CodeContext/*!*/ context, PythonType dt, object newObject) {
            // check if object has finalizer...
            PythonTypeSlot dummy;
            if (dt.TryResolveSlot(context, "__del__", out dummy)) {
                IWeakReferenceable iwr = context.GetPythonContext().ConvertToWeakReferenceable(newObject);
                Debug.Assert(iwr != null);

                InstanceFinalizer nif = new InstanceFinalizer(context, newObject);
                iwr.SetFinalizer(new WeakRefTracker(nif, nif));
            }
        }