Exemple #1
0
    def grab(self, class_instance, exclusive=False):
        '''Grab this motion event. You can grab a touch if you absolutly
        want to receive on_touch_move() and on_touch_up(), even if the
        touch is not dispatched by your parent::

            def on_touch_down(self, touch):
                touch.grab(self)

            def on_touch_move(self, touch):
                if touch.grab_current is self:
                    # I received my grabbed touch
                else:
                    # it's a normal touch

            def on_touch_up(self, touch):
                if touch.grab_current is self:
                    # I receive my grabbed touch, I must ungrab it!
                    touch.ungrab(self)
                else:
                    # it's a normal touch
                    pass
        '''
        if not self.is_touch:
            raise Exception('Grab works only for Touch MotionEvents.')
        if self.grab_exclusive_class is not None:
            raise Exception('Cannot grab the touch, touch is exclusive')
        try:
            class_instance = weakref.ref(class_instance)
        except TypeError:
            # handle weakproxy objects which cannot be weakref'd
            class_instance = weakref.ref(class_instance.__self__)
        if exclusive:
            self.grab_exclusive_class = class_instance
        self.grab_list.append(class_instance)
Exemple #2
0
    def _gst_init(self):
        # self._videosink will receive the buffers so we can upload them to GPU
        if PY2:
            self._videosink = gst.element_factory_make('appsink', 'videosink')
            self._videosink.set_property('caps', gst.Caps(_VIDEO_CAPS))
        else:
            self._videosink = gst.ElementFactory.make('appsink', 'videosink')
            self._videosink.set_property('caps',
                 gst.caps_from_string(_VIDEO_CAPS))

        self._videosink.set_property('async', True)
        self._videosink.set_property('drop', True)
        self._videosink.set_property('qos', True)
        self._videosink.set_property('emit-signals', True)
        self._videosink.connect('new-' + BUF_SAMPLE, partial(
            _gst_new_buffer, ref(self)))

        # playbin, takes care of all, loading, playing, etc.
        # XXX playbin2 have some issue when playing some video or streaming :/
        #self._playbin = gst.element_factory_make('playbin2', 'playbin')
        if PY2:
            self._playbin = gst.element_factory_make('playbin', 'playbin')
        else:
            self._playbin = gst.ElementFactory.make('playbin', 'playbin')
        self._playbin.set_property('video-sink', self._videosink)

        # gstreamer bus, to attach and listen to gst messages
        self._bus = self._playbin.get_bus()
        self._bus.add_signal_watch()
        self._bus.connect('message', _on_gst_message)
        self._bus.connect('message::eos', partial(
            _on_gst_eos, ref(self)))
Exemple #3
0
    def create_dict(self):
        """Create a new dict for the current greenlet, and return it."""
        localdict = {}
        key = self.key
        greenlet = getcurrent()
        idt = id(greenlet)

        def local_deleted(_, key=key):
            # When the localimpl is deleted, remove the greenlet attribute.
            greenlet = wrgreenlet()
            if greenlet is not None:
                del greenlet.__dict__[key]

        def greenlet_deleted(_, idt=idt):
            # When the greenlet is deleted, remove the local dict.
            # Note that this is suboptimal if the greenlet object gets
            # caught in a reference loop. We would like to be called
            # as soon as the OS-level greenlet ends instead.
            local = wrlocal()
            if local is not None:
                local.dicts.pop(idt)

        wrlocal = ref(self, local_deleted)
        wrgreenlet = ref(greenlet, greenlet_deleted)
        greenlet.__dict__[key] = wrlocal
        self.dicts[idt] = wrgreenlet, localdict
        return localdict
Exemple #4
0
 def __init__(self, c):
     if hasattr(c,'im_func'):
         self._func = weakref.ref(c.im_func)
         self._ob = weakref.ref(c.im_self)
     else:
         self._func = weakref.ref(c)
         self._ob = None
    def test_no_refcycle_through_target(self):
        class RunSelfFunction(object):
            def __init__(self, should_raise):
                # The links in this refcycle from Thread back to self
                # should be cleaned up when the thread completes.
                self.should_raise = should_raise
                self.thread = threading.Thread(target=self._run, args=(self,), kwargs={"yet_another": self})
                self.thread.start()

            def _run(self, other_ref, yet_another):
                if self.should_raise:
                    raise SystemExit

        cyclic_object = RunSelfFunction(should_raise=False)
        weak_cyclic_object = weakref.ref(cyclic_object)
        cyclic_object.thread.join()
        del cyclic_object
        self.assertIsNone(
            weak_cyclic_object(), msg=("%d references still around" % sys.getrefcount(weak_cyclic_object()))
        )

        raising_cyclic_object = RunSelfFunction(should_raise=True)
        weak_raising_cyclic_object = weakref.ref(raising_cyclic_object)
        raising_cyclic_object.thread.join()
        del raising_cyclic_object
        self.assertIsNone(
            weak_raising_cyclic_object(),
            msg=("%d references still around" % sys.getrefcount(weak_raising_cyclic_object())),
        )
Exemple #6
0
 def __init__(self, method):
     self.object = weakref.ref(method.im_self)
     self.func = weakref.ref(method.im_func)
     # don't create a weak reference to the class.  That only works for
     # new-style classes.  It's highly unlikely the class will ever need to
     # be garbage collected anyways.
     self.cls = method.im_class
Exemple #7
0
 def test_weakref_slots(self):
     """Check that classes are using slots and are weak-referenceable"""
     for cls in [self.xs_cls, self.hxs_cls, self.xxs_cls]:
         x = cls()
         weakref.ref(x)
         assert not hasattr(x, '__dict__'), "%s does not use __slots__" % \
             x.__class__.__name__
Exemple #8
0
    def test_pool(self):
        # Create pools
        parent_pool = Pool()
        parent_pool_ref = weakref.ref(parent_pool)
        pool = Pool(Pool(parent_pool))
        pool = Pool(pool)

        # Make sure proper exceptions are raised with incorrect input
        self.assertRaises(TypeError, lambda: Pool("abcd"))

        # Check that garbage collection is working OK
        self.assertNotNone(parent_pool_ref())
        top_pool_ref = weakref.ref(parent_pool._parent_pool)
        del parent_pool
        self.assertNotNone(parent_pool_ref())
        self.assertNotNone(top_pool_ref())
        pool.clear()
        newpool = libsvn.core.svn_pool_create(pool)
        libsvn.core.apr_pool_destroy(newpool)
        self.assertNotNone(newpool)
        pool.clear()
        self.assertNotNone(parent_pool_ref())
        del pool
        self.assertNotNone(parent_pool_ref())
        del newpool
        self.assertNone(parent_pool_ref())
        self.assertNone(top_pool_ref())

        # Make sure anonymous pools are destroyed properly
        anonymous_pool_ref = weakref.ref(Pool())
        self.assertNone(anonymous_pool_ref())
def safeRef(target, onDelete = None):
    """Return a *safe* weak reference to a callable target

    target -- the object to be weakly referenced, if it's a
        bound method reference, will create a BoundMethodWeakref,
        otherwise creates a simple weakref.
    onDelete -- if provided, will have a hard reference stored
        to the callable to be called after the safe reference
        goes out of scope with the reference object, (either a
        weakref or a BoundMethodWeakref) as argument.
    """
    if hasattr(target, '__self__'):
        if target.__self__ is not None:
            # Turn a bound method into a BoundMethodWeakref instance.
            # Keep track of these instances for lookup by disconnect().
            assert hasattr(target, '__func__'), """safeRef target %r has __self__, but no __func__, don't know how to create reference"""%( target,)
            reference = get_bound_method_weakref(
                target=target,
                onDelete=onDelete
            )
            return reference
    if callable(onDelete):
        return weakref.ref(target, onDelete)
    else:
        return weakref.ref( target )
Exemple #10
0
    def show_widget_info(self):
        self.content.clear_widgets()
        widget = self.widget
        treeview = self.treeview
        for node in list(treeview.iterate_all_nodes())[:]:
            node.widget_ref = None
            treeview.remove_node(node)
        if not widget:
            if self.at_bottom:
                Animation(top=60, t='out_quad', d=.3).start(self.layout)
            else:
                Animation(y=self.height - 60, t='out_quad', d=.3).start(self.layout)
            self.widget_info = False
            return
        self.widget_info = True
        if self.at_bottom:
            Animation(top=250, t='out_quad', d=.3).start(self.layout)
        else:
            Animation(top=self.height, t='out_quad', d=.3).start(self.layout)
        for node in list(treeview.iterate_all_nodes())[:]:
            treeview.remove_node(node)

        keys = list(widget.properties().keys())
        keys.sort()
        node = None
        wk_widget = weakref.ref(widget)
        for key in keys:
            text = '%s' % key
            node = TreeViewProperty(text=text, key=key, widget_ref=wk_widget)
            node.bind(is_selected=self.show_property)
            widget.bind(**{key: partial(
                self.update_node_content, weakref.ref(node))})
            treeview.add_node(node)
Exemple #11
0
    def test_compatibility_layer(self):
        # Create a new pool
        pool = Pool()
        parent_pool_ref = weakref.ref(pool)
        pool = svn_pool_create(Pool(pool))
        pool_ref = weakref.ref(pool)

        # Make sure proper exceptions are raised with incorrect input
        self.assertRaises(TypeError, lambda: svn_pool_create("abcd"))

        # Test whether pools are destroyed properly
        pool = svn_pool_create(pool)
        self.assertNotNone(pool_ref())
        self.assertNotNone(parent_pool_ref())
        del pool
        self.assertNone(pool_ref())
        self.assertNone(parent_pool_ref())

        # Ensure that AssertionErrors are raised when a pool is deleted twice
        newpool = Pool()
        newpool2 = Pool(newpool)
        svn_pool_clear(newpool)
        self.assertRaises(AssertionError, lambda: libsvn.core.apr_pool_destroy(newpool2))
        self.assertRaises(AssertionError, lambda: svn_pool_destroy(newpool2))
        svn_pool_destroy(newpool)
        self.assertRaises(AssertionError, lambda: svn_pool_destroy(newpool))

        # Try to allocate memory from a destroyed pool
        self.assertRaises(AssertionError, lambda: svn_pool_create(newpool))

        # Create and destroy a pool
        svn_pool_destroy(svn_pool_create())

        # Make sure anonymous pools are destroyed properly
        anonymous_pool_ref = weakref.ref(svn_pool_create())
        self.assertNone(anonymous_pool_ref())

        # Try to cause a segfault using apr_terminate
        apr_terminate()
        apr_initialize()
        apr_terminate()
        apr_terminate()

        # Destroy the application pool
        svn_pool_destroy(libsvn.core.application_pool)

        # Double check that the application pool has been deleted
        self.assertNone(libsvn.core.application_pool)

        # Try to allocate memory from the old application pool
        self.assertRaises(AssertionError, lambda: svn_pool_create(application_pool))

        # Bring the application pool back to life
        svn_pool_create()

        # Double check that the application pool has been created
        self.assertNotNone(libsvn.core.application_pool)

        # We can still destroy and create pools at will
        svn_pool_destroy(svn_pool_create())
Exemple #12
0
    def allocate(self, obj: object, index: Optional[int] = None) -> int:
        """Allocate a resource.

        When index is None or unspecified, a free resource value within the
        range is located and returned after it is marked allocated.
        Otherwise, it is verified unallocated, then returned.

        :param obj: The object requesting the resource.
        :param index: The resource to allocate
        :returns: The index of the allocated block.
        :raises IndexError: If there are no resources available to be
            allocated or the specified index is already used.
        """
        if index is None:
            for i in range(len(self.numAllocated)):
                r = self.numAllocated[i]
                if r is None or r() is None:
                    self.numAllocated[i] = weakref.ref(obj)
                    return i
            raise IndexError("No available resources")

        if index >= len(self.numAllocated) or index < 0:
            raise IndexError("Index %d out of range" % index)
        r = self.numAllocated[index]
        if r is not None and r() is not None:
            raise IndexError("Resource at index %d already allocated" % index)
        self.numAllocated[index] = weakref.ref(obj)
        return index
Exemple #13
0
def safe_ref(target, on_delete=None):
    """Return a *safe* weak reference to a callable target.

    - ``target``: The object to be weakly referenced, if it's a bound
      method reference, will create a BoundMethodWeakref, otherwise
      creates a simple weakref.

    - ``on_delete``: If provided, will have a hard reference stored to
      the callable to be called after the safe reference goes out of
      scope with the reference object, (either a weakref or a
      BoundMethodWeakref) as argument.
    """
    try:
        im_self = get_self(target)
    except AttributeError:
        if callable(on_delete):
            return weakref.ref(target, on_delete)
        else:
            return weakref.ref(target)
    else:
        if im_self is not None:
            # Turn a bound method into a BoundMethodWeakref instance.
            # Keep track of these instances for lookup by disconnect().
            assert hasattr(target, 'im_func') or hasattr(target, '__func__'), (
                "safe_ref target %r has im_self, but no im_func, "
                "don't know how to create reference" % target)
            reference = BoundMethodWeakref(target=target, on_delete=on_delete)
            return reference
Exemple #14
0
    def name(self, value):
        old_name = self._name
        if value is old_name:
            return
        self._name = value
        configs = ConfigParser._named_configs

        if old_name:  # disconnect this parser from previously connected props
            _, props = configs.get(old_name, (None, []))
            for widget, prop in props:
                widget = widget()
                if widget:
                    widget.property(prop).set_config(None)
            configs[old_name] = (None, props)

        if not value:
            return

        # if given new name, connect it with property that used this name
        try:
            config, props = configs[value]
        except KeyError:
            configs[value] = (ref(self), [])
            return

        if config is not None:
            raise ValueError('A parser named {} already exists'.format(value))
        for widget, prop in props:
            widget = widget()
            if widget:
                widget.property(prop).set_config(self)
        configs[value] = (ref(self), props)
	def load(self, db):
		data = db("SELECT value FROM last_active_settlement WHERE type = \"PLAYER\"")
		self._last_player_settlement = weakref.ref(WorldObject.get_object_by_id(data[0][0])) if data else None
		data = db("SELECT value FROM last_active_settlement WHERE type = \"ANY\"")
		self._last_settlement = weakref.ref(WorldObject.get_object_by_id(data[0][0])) if data else None
		data = db("SELECT value FROM last_active_settlement WHERE type = \"LAST_NONE_FLAG\"")
		self._last_player_settlement_hovered_was_none = bool(data[0][0])
Exemple #16
0
 def __init__(self, *args, **kwargs):
     self._items=set()
     if args:
         self._items.update(args)
     else:
         self._items.add(dummy)
     for i in kwargs:
         setattr(self,i,kwargs[i])
     if not hasattr (self,'ruletype'):
         self.ruletype='persistent'
     # default ruletype
     if self.ruletype == 'persistent':
         _persistent.add(weakref.ref(self))
     # periodic rules, of course rule must have a 'period' attribute
     elif self.ruletype == 'periodic':
         _periodic.add(weakref.ref(self))  # is this necessary ?
         schedule_interval(self,self.period) # or schedule a oneshot rule ?
     # will run only once, then erased from list
     elif self.ruletype == 'oneshot':
         _oneshot.add(weakref.ref(self))
     # of course rule must have a 'condition' attribute
     elif self.ruletype == 'conditional':
         _conditional.add(weakref.ref(self))
     else:
         print ':: unrecognized type of rule'
     self.setup()
     print "::"
     print ":: new rule :::::::::::::::::::::::::::::::::::::::::::::::::::"
     print "::"
     dumpObj(self)
Exemple #17
0
def ViewCell(base, get_transform, set_transform, **kwargs):
    """
    A Cell whose value is always a transformation of another.
    
    TODO: Stop implementing this as LooseCell.
    """
    
    def forward(view_value):
        base_value = set_transform(view_value)
        base.set(base_value)
        actual_base_value = base.get()
        if base_value != actual_base_value:
            reverse(actual_base_value)
    
    def reverse(base_value):
        self.set(get_transform(base_value))
    
    self = LooseCell(
        value=get_transform(base.get()),
        post_hook=forward,
        **kwargs)
    
    sub = base._subscribe_immediate(reverse)
    weakref.ref(self, lambda: sub.unsubscribe())

    # Allows the cell to be put back in sync if the transform changes.
    # Not intended to be called except by the creator of the cell, but mostly harmless.
    def changed_transform():
        reverse(base.get())
    
    self.changed_transform = changed_transform  # pylint: disable=attribute-defined-outside-init
    
    return self
Exemple #18
0
def _weak_callback(func):
    '''
    Store a weak reference to a callback or method.
    '''
    if isinstance(func, types.MethodType):
        # Don't hold a reference to the object, otherwise we might create
        # a cycle.
        # Reference: http://stackoverflow.com/a/6975682
        # Tell coveralls to not cover this if block, as the Python 2.x case
        # doesn't test the 3.x code and vice versa.
        if sys.version_info[0] < 3:  # pragma: no cover
            # Python 2.x case
            obj_ref = weakref.ref(func.im_self)
            func_ref = weakref.ref(func.im_func)
        else:  # pragma: no cover
            # Python 3.x case
            obj_ref = weakref.ref(func.__self__)
            func_ref = weakref.ref(func.__func__)
        func = None

        def _callback(*args, **kwargs):
            obj = obj_ref()
            func = func_ref()
            if (obj is None) or (func is None):
                return
            return func(obj, *args, **kwargs)
        return _callback
    else:
        # We should be safe enough holding callback functions ourselves.
        return func
Exemple #19
0
 def test_del_and_callback_and_id(self):
     if not self.runappdirect:
         skip("the id() doesn't work correctly in __del__ and "
              "callbacks before translation")
     import gc, weakref
     seen_del = []
     class A(object):
         def __del__(self):
             seen_del.append(id(self))
             seen_del.append(w1() is None)
             seen_del.append(w2() is None)
     seen_callback = []
     def callback(r):
         seen_callback.append(r is w2)
         seen_callback.append(w1() is None)
         seen_callback.append(w2() is None)
     a = A()
     w1 = weakref.ref(a)
     w2 = weakref.ref(a, callback)
     aid = id(a)
     del a
     for i in range(5):
         gc.collect()
     if seen_del:
         assert seen_del == [aid, True, True]
     if seen_callback:
         assert seen_callback == [True, True, True]
Exemple #20
0
	def ref(self, callable):
		self.id = id(callable)
		try:
			self.cbref = weakref.ref(callable.__self__)
			self.meth = callable.__func__
		except AttributeError:
			self.cbref = weakref.ref(callable)
    def __init__(self, graphicsFrameWidget, parent=None):
        
        (self.minCon, self.maxCon) = (0, 0)
        
        self.parentWidget=parent
        
        
        # self.graphicsFrameWidget = graphicsFrameWidget()        
        # self.qvtkWidget = self.graphicsFrameWidget.qvtkWidget
        
        from weakref import ref
        gfw=ref(graphicsFrameWidget)
        self.graphicsFrameWidget = gfw()
        
        # qvtk=ref(self.graphicsFrameWidget.qvtkWidget)
        
        self.qvtkWidget =ref(self.graphicsFrameWidget.qvtkWidget)       
        # self.qvtkWidget = qvtk()        
        
        # # # self.graphicsFrameWidget=graphicsFrameWidget
        # # # self.qvtkWidget=self.graphicsFrameWidget.qvtkWidget
        self.currentDrawingFunction=None       
        self.fieldTypes=None 
        self.currentDrawingParameters=None
#        self.scaleGlyphsByVolume = False
        
        self.hexFlag = self.parentWidget.latticeType==Configuration.LATTICE_TYPES["Hexagonal"]
Exemple #22
0
 def __init__(self, root):
     self.config = Config("tkc.ini")
     root.title("Tkinter Commander")
     root.protocol("WM_DELETE_WINDOW", self.on_delete)
     self.root = root
     root.geometry(self.config.get("fm_geometry"))
     root.grid_columnconfigure(0, weight=1)
     root.grid_rowconfigure(1, weight=1)
     pw = Panedwindow(root, orient="horizontal", takefocus=False)
     frame = Frame(pw)
     self.left = Panel(frame, Local_fs(), self.config)
     pw.add(frame)
     pw.pane(frame, weight=1)
     frame = Frame(pw)
     self.right = Panel(frame, Local_fs(), self.config)
     self.right.oposite = ref(self.left)
     self.left.oposite = ref(self.right)
     self.left.activate()
     pw.add(frame)
     pw.pane(frame, weight=1)
     pw.grid(column=0, row=1, columnspan=2, sticky="senw")
     self.add_menu()
     self.add_btns()
     root.tk.call(
         "wm", "iconphoto", root._w,
         PhotoImage(file=join(dirname(__file__), "data", "favicon.gif")))
Exemple #23
0
    def disableNotifications(self, observable=None, notification=None, observer=None):
        """
        Disable all posts of **notification** from **observable** posted
        to **observer** observing.

        * **observable** The object that the notification belongs to. This is optional.
          If no *observable* is given, *all* *notifications* will be disabled for *observer*.
        * **notification** The name of the notification. This is optional.
          If no *notification* is given, *all* notifications for *observable*
          will be disabled for *observer*.
        * **observer** The specific observer to not send posts to. If no
          *observer* is given, the appropriate notifications will not
          be posted to any observers.

        This object will retain a count of how many times it has been told to
        disable notifications for *notification* and *observable*. It will not
        enable new notifications until the *notification* and *observable*
        have been released the same number of times.
        """
        if observable is not None:
            observable = weakref.ref(observable)
        if observer is not None:
            observer = weakref.ref(observer)
        key = (notification, observable, observer)
        if key not in self._disabled:
            self._disabled[key] = 0
        self._disabled[key] += 1
Exemple #24
0
def test_keep_alive_noleak2():
    # Even if the above would not work ...
    
    class Foo:
        pass
    
    # Create a session and an object that has a reference to it (like Model)
    session = app.Session('test')
    foo = Foo()
    foo.session = session
    
    # Let the session keep the object alive, so it keeps its reference
    session.keep_alive(foo)
    
    session_ref = weakref.ref(session)
    foo_ref = weakref.ref(foo)
    
    # Removing session wont delete it
    del session
    gc.collect()
    assert session_ref() is not None
    
    # But removing both will; gc is able to clear circular ref
    del foo
    gc.collect()
    assert session_ref() is None
    assert foo_ref() is None
Exemple #25
0
    def holdNotifications(self, observable=None, notification=None, observer=None):
        """
        Hold all notifications posted to all objects observing
        **notification** in **observable**.

        * **observable** The object that the notification belongs to. This is optional.
          If no *observable* is given, *all* *notifications* will be held.
        * **notification** The name of the notification. This is optional.
          If no *notification* is given, *all* notifications for *observable*
          will be held.
         * **observer** The specific observer to not hold notifications for.
           If no *observer* is given, the appropriate notifications will be
           held for all observers.

        Held notifications will be posted after the matching *notification*
        and *observable* have been passed to :meth:`Notification.releaseHeldNotifications`.
        This object will retain a count of how many times it has been told to
        hold notifications for *notification* and *observable*. It will not
        post the notifications until the *notification* and *observable*
        have been released the same number of times.
        """
        if observable is not None:
            observable = weakref.ref(observable)
        if observer is not None:
            observer = weakref.ref(observer)
        key = (notification, observable, observer)
        if key not in self._holds:
            self._holds[key] = dict(count=0, notifications=[])
        self._holds[key]["count"] += 1
  def testMemoryIsFreed(self):
    # Note: we use `set` values for components and metadata because we need
    # to construct weakrefs to them.  Other builtin types, such as `list` and
    # `tuple`, do not support weakrefs.
    ct1 = CT(set([1, 2]), set(['no', 'leaks']))
    ct2 = CT(set([3, 4]), set(['no', 'leaks']))
    ct3 = CT(set([5, 6]), set(['other', 'metadata']))

    # Note: map_structure exercises flatten, pack_sequence_as, and
    # assert_same_structure.
    func = lambda x, y: x | y
    ct4 = nest.map_structure(func, ct1, ct2, expand_composites=True)

    # Check that the exception-raising path in assert_same_structure
    # doesn't leak any objects.
    with self.assertRaisesRegexp(ValueError,
                                 ".*don't have the same nested structure.*"):
      nest.map_structure(func, ct2, ct3, expand_composites=True)
    if hasattr(sys, 'exc_clear'):
      sys.exc_clear()  # Remove any references in exception stack traces.

    refs = []
    for ct in [ct1, ct2, ct3, ct4]:
      refs.append(weakref.ref(ct))
      refs.append(weakref.ref(ct.components))
      refs.append(weakref.ref(ct.metadata))
    del ct  # pylint: disable=undefined-loop-variable

    for ref in refs:
      self.assertIsNotNone(ref())

    del ct1, ct2, ct3, ct4
    gc.collect()
    for ref in refs:
      self.assertIsNone(ref())
Exemple #27
0
def test_keep_alive_noleak1():
    
    class Foo:
        pass
    
    # Create a session and an object that has a reference to it (like Model)
    session = app.Session('test')
    foo = Foo()
    foo.session = session
    
    # Let the session keep the object alive, so it keeps its reference
    session.keep_alive(foo)
    
    session_ref = weakref.ref(session)
    foo_ref = weakref.ref(foo)
    
    # Removing object wont delete it
    del foo
    gc.collect()
    assert foo_ref() is not None
    
    # But closing the session will; session clears up after itself
    session.close()
    gc.collect()
    assert foo_ref() is None
Exemple #28
0
    def addObserver(self, observer, methodName, notification=None, observable=None):
        """
        Add an observer to this notification dispatcher.

        * **observer** An object that can be referenced with weakref.
        * **methodName** A string epresenting the method to be called
          when the notification is posted.
        * **notification** The notification that the observer should
          be notified of. If this is None, all notifications for
          the *observable* will be posted to *observer*.
        * **observable** The object to observe. If this is None,
          all notifications with the name provided as *notification*
          will be posted to the *observer*.

        If None is given for both *notification* and *observable*
        **all** notifications posted will be sent to the method
        given method of the observer.

        The method that will be called as a result of the action
        must accept a single *notification* argument. This will
        be a :class:`Notification` object.
        """
        if observable is not None:
            observable = weakref.ref(observable)
        observer = weakref.ref(observer)
        key = (notification, observable)
        if key not in self._registry:
            self._registry[key] = ObserverDict()
        assert observer not in self._registry[key], "An observer is only allowed to have one callback for a given notification + observable combination."
        self._registry[key][observer] = methodName
    def test_model_garbage_collection(self):
        """
        Make sure tenant models are correctly garbage collected upon deletion.
        """
        tenant = Tenant.objects.create(name='tenant')

        # Keep weak-references to tenant and associated models to make sure
        # they have been colllected.
        tenant_wref = weakref.ref(tenant)
        models_wrefs = []
        for model in TenantModelBase.references:
            # Make sure all models have their relation tree populated.
            getattr(model._meta, '_relation_tree')
            models_wrefs.append(weakref.ref(model.for_tenant(tenant)))

        # Delete the tenant and all it's associated models.
        tenant.delete()
        del tenant

        # Force a garbage collection for the benefit of non-reference counting
        # implementations.
        gc.collect()

        # Make sure all references have been removed.
        self.assertIsNone(tenant_wref())
        for model_wref in models_wrefs:
            self.assertIsNone(model_wref())
Exemple #30
0
def test_supports_weakref_with_multi_level_inheritance():
    import weakref

    class PPoint(Point):
        a = field()

    weakref.ref(PPoint(x=1, y=2))
Exemple #31
0
 def add(self, item):
     if self._pending_removals:
         self._commit_removals()
     self.data.add(ref(item, self._remove))
Exemple #32
0
    def include_flux(self, *args, **kwargs):
        """Include a flux contribution to a specific node.

        The flux can be described as a HOC reference, a point process and a
        property, a Python function, or something that evaluates to a constant
        Python float.

        Supported units:
            molecule/ms
            mol/ms
            mmol/ms == millimol/ms == mol/s

        Examples:
            node.include_flux(mglur, 'ip3flux')           # default units: molecule/ms
            node.include_flux(mglur, 'ip3flux', units='mol/ms') # units: moles/ms
            node.include_flux(mglur._ref_ip3flux, units='molecule/ms')
            node.include_flux(lambda: mglur.ip3flux)
            node.include_flux(lambda: math.sin(h.t))
            node.include_flux(47)

        Warning:
            Flux denotes a change in *mass* not a change in concentration.
            For example, a metabotropic synapse produces a certain amount of
            substance when activated. The corresponding effect on the node's
            concentration depends on the volume of the node. (This scaling is
            handled automatically by NEURON's rxd module.)
        """
        global _has_node_fluxes, _node_fluxes
        if len(args) not in (1, 2):
            raise RxDException(
                "node.include_flux takes only one or two arguments")
        if "units" in kwargs:
            units = kwargs.pop("units")
        else:
            units = "molecule/ms"
        if len(kwargs):
            raise RxDException("Unknown keyword arguments: %r" %
                               list(kwargs.keys()))
        # take the value, divide by scale to get mM um^3
        # once this is done, we need to divide by volume to get mM
        # TODO: is division still slower than multiplication? Switch to mult.
        if units == "molecule/ms":
            scale = constants.molecules_per_mM_um3()
        elif units == "mol/ms":
            # You have: mol
            # You want: (millimol/L) * um^3
            #    * 1e+18
            #    / 1e-18
            scale = 1e-18
        elif units in ("mmol/ms", "millimol/ms", "mol/s"):
            # You have: millimol
            # You want: (millimol/L)*um^3
            #    * 1e+15
            #    / 1e-15
            scale = 1e-15
        else:
            raise RxDException("unknown unit: %r" % units)

        if len(args) == 1 and isinstance(args[0], hoc.HocObject):
            source = args[0]
            flux_type = 1
            try:
                # just a test access
                source[0]
            except:
                raise RxDException("HocObject must be a pointer")
        elif len(args) == 1 and isinstance(args[0], collections.Callable):
            flux_type = 2
            source = args[0]
            warnings.warn(
                "Adding a python callback may slow down execution. Consider using a Rate and Parameter."
            )
        elif len(args) == 2:
            flux_type = 1
            try:
                source = getattr(args[0], "_ref_" + args[1])
            except:
                raise RxDException("Invalid two parameter form")

            # TODO: figure out a units checking solution that works
            # source_units = h.units(source)
            # if source_units and source_units != units:
            #    warnings.warn('Possible units conflict. NEURON says %r, but specified as %r.' % (source_units, units))
        else:
            success = False
            if len(args) == 1:
                try:
                    f = float(args[0])
                    source = f
                    flux_type = 3
                    success = True
                except:
                    pass
            if not success:
                raise RxDException("unsupported flux form")
        _node_fluxes["index"].append(self._index)
        if isinstance(self, Node1D):
            _node_fluxes["type"].append(-1)
        else:
            _node_fluxes["type"].append(self._grid_id)
        _node_fluxes["source"].append(source)
        _node_fluxes["scale"].append(scale)
        if isinstance(self, Node1D):
            _node_fluxes["region"].append(None)
        else:
            _node_fluxes["region"].append(weakref.ref(self._r))
        from .rxd import _structure_change_count

        _structure_change_count.value += 1
        _has_node_fluxes = True
Exemple #33
0
 def add_jitcell_token(self, token):
     assert isinstance(token, JitCellToken)
     self.jitcell_token_wrefs.append(weakref.ref(token))
Exemple #34
0
    def test_mode_slide_player(self):
        # set a baseline slide
        self.mc.events.post('show_slide_1')
        self.advance_time()
        self.assertEqual(self.mc.targets['display1'].current_slide_name,
                         'machine_slide_1')

        # post the slide_player event from the mode. Should not show the slide
        # since the mode is not running
        self.mc.events.post('show_mode1_slide')
        self.advance_time()
        self.assertEqual(self.mc.targets['display1'].current_slide_name,
                         'machine_slide_1')

        # start the mode and then post that event again. The slide should
        # switch
        self.mc.modes['mode1'].start()
        self.mc.events.post('show_mode1_slide')
        self.advance_time()
        self.assertEqual(self.mc.targets['display1'].current_slide_name,
                         'mode1_slide')
        slide = weakref.ref(self.mc.targets['display1'].current_slide)
        self.assertTrue(slide())
        # stop the mode and make sure the slide is removed
        num_slides = len(self.mc.targets['display1'].slides)
        self.mc.modes['mode1'].stop()
        self.assertEqual(self.mc.targets['display1'].current_slide_name,
                         'machine_slide_1')
        self.assertEqual(len(self.mc.targets['display1'].slides),
                         num_slides - 1)

        self.advance_time(.1)

        gc.collect()
        self.assertFalse(slide())

        # post the slide_player event from the mode. Should not show the slide
        # since the mode is not running
        self.mc.events.post('show_mode1_slide')
        self.advance_time()
        self.assertEqual(self.mc.targets['display1'].current_slide_name,
                         'machine_slide_1')

        # show a priority 200 slide from the machine config
        self.mc.events.post('show_slide_4_p200')
        self.advance_time()
        self.assertEqual(self.mc.targets['display1'].current_slide_name,
                         'machine_slide_4')
        self.assertEqual(self.mc.targets['display1'].current_slide.priority,
                         200)

        # start the mode again (priority 500)
        self.mc.modes['mode1'].start()

        # show a slide, but priority 150 which means the slide will not be
        # shown
        self.mc.events.post('show_mode1_slide_2')
        self.advance_time()
        self.assertEqual(self.mc.targets['display1'].current_slide_name,
                         'machine_slide_4')
        self.assertEqual(self.mc.targets['display1'].current_slide.priority,
                         200)

        # now kill the current slide and the mode slide should show
        self.mc.targets['display1'].remove_slide('machine_slide_4')
        self.assertEqual(self.mc.targets['display1'].current_slide_name,
                         'mode1_slide_2')
        self.assertEqual(self.mc.targets['display1'].current_slide.priority,
                         150)
Exemple #35
0
 def _set_singleton(cls, val):
     cls.logger.debug('Setting singleton dispatcher as %s', val)
     cls.__singleton = weakref.ref(val) if val else None
 def parent(self, parent):
     self.weak_parent = parent and weakref.ref(parent)
Exemple #37
0
    def test_gc(self):
        """refcount agnostic check that contained classes are freed"""
        def before_callback(parent):
            return r[0]

        def after_callback(parent):
            return r[1]

        class Obj(object):
            pass

        p = Obj()
        a = Obj()
        r = [Obj(), Obj()]
        weak_p = weakref.ref(p)
        weak_a = weakref.ref(a)
        weak_r0 = weakref.ref(r[0])
        weak_r1 = weakref.ref(r[1])
        weak_before = weakref.ref(before_callback)
        weak_after = weakref.ref(after_callback)
        kwds = dict(self.view_keywords)
        kwds['parent'] = p
        kwds['before'] = before_callback
        kwds['after'] = after_callback
        v = BufferProxy(kwds)
        v.some_attribute = a
        weak_v = weakref.ref(v)
        kwds = p = a = before_callback = after_callback = None
        gc.collect()
        self.assertTrue(weak_p() is not None)
        self.assertTrue(weak_a() is not None)
        self.assertTrue(weak_before() is not None)
        self.assertTrue(weak_after() is not None)
        v = None
        [gc.collect() for x in range(4)]
        self.assertTrue(weak_v() is None)
        self.assertTrue(weak_p() is None)
        self.assertTrue(weak_a() is None)
        self.assertTrue(weak_before() is None)
        self.assertTrue(weak_after() is None)
        self.assertTrue(weak_r0() is not None)
        self.assertTrue(weak_r1() is not None)
        r = None
        gc.collect()
        self.assertTrue(weak_r0() is None)
        self.assertTrue(weak_r1() is None)

        # Cycle removal
        kwds = dict(self.view_keywords)
        kwds['parent'] = []
        v = BufferProxy(kwds)
        v.some_attribute = v
        tracked = True
        for o in gc.get_objects():
            if o is v:
                break
        else:
            tracked = False
        self.assertTrue(tracked)
        kwds['parent'].append(v)
        kwds = None
        gc.collect()
        n1 = len(gc.garbage)
        v = None
        gc.collect()
        n2 = len(gc.garbage)
        self.assertEqual(n2, n1)
Exemple #38
0
 def issuperset(self, other):
     return self.data.issuperset(ref(item) for item in other)
Exemple #39
0
 def discard(self, item):
     if self._pending_removals:
         self._commit_removals()
     self.data.discard(ref(item))
Exemple #40
0
 def __contains__(self, item):
     try:
         wr = ref(item)
     except TypeError:
         return False
     return wr in self.data
Exemple #41
0
 def __init__(self, container):
     self.ref = ref(container)
     self.normal = None
     self.selected = None
     self.alerted = False
Exemple #42
0
 def __iand__(self, other):
     if self._pending_removals:
         self._commit_removals()
     self.data.intersection_update(ref(item) for item in other)
     return self
Exemple #43
0
 def g():
     b = B()
     return weakref.ref(b)
Exemple #44
0
 def remove(self, item):
     if self._pending_removals:
         self._commit_removals()
     self.data.remove(ref(item))
Exemple #45
0
 def __init__(self, value, owner):
     self.value = value
     self.owner = weakref.ref(owner)
Exemple #46
0
 def cleanup_once_done(self, response, filepath):
     wr = weakref.ref(response, self.do_cleanup)
     self.weak_refs[wr] = filepath
 def add_tests_to_suite(cls, mod_name, filepath, suite, testloader,
                        marionette, testvars, **kwargs):
     suite.addTest(cls(weakref.ref(marionette), jsFile=filepath, **kwargs))
Exemple #48
0
 def g():
     a = A()
     return weakref.ref(a)
Exemple #49
0
    def _matches(self, function: Callable[..., Any]) -> bool:
        if not callable(function):
            return False

        self.function = cast(Callable[..., Any], ref(function))
        return self._call_function(function)
Exemple #50
0
    def LoadVolume(self):
        proj = prj.Project()
        #image = imagedata_utils.to_vtk(n_array, spacing, slice_number, orientation)

        if not self.loaded_image:
            self.LoadImage()
            self.loaded_image = 1

        image = self.image

        number_filters = len(self.config['convolutionFilters'])

        if (prj.Project().original_orientation == const.AXIAL):
            flip_image = True
        else:
            flip_image = False

        #if (flip_image):
        update_progress = vtk_utils.ShowProgress(2 + number_filters)
        # Flip original vtkImageData
        flip = vtk.vtkImageFlip()
        flip.SetInputData(image)
        flip.SetFilteredAxis(1)
        flip.FlipAboutOriginOn()
        #  flip.ReleaseDataFlagOn()

        flip_ref = weakref.ref(flip)
        flip_ref().AddObserver(
            "ProgressEvent",
            lambda obj, evt: update_progress(flip_ref(), "Rendering..."))
        flip.Update()
        image = flip.GetOutput()

        scale = image.GetScalarRange()
        self.scale = scale

        cast = vtk.vtkImageShiftScale()
        cast.SetInputData(image)
        cast.SetShift(abs(scale[0]))
        cast.SetOutputScalarTypeToUnsignedShort()
        #  cast.ReleaseDataFlagOn()
        cast_ref = weakref.ref(cast)
        cast_ref().AddObserver(
            "ProgressEvent",
            lambda obj, evt: update_progress(cast_ref(), "Rendering..."))
        cast.Update()
        image2 = cast

        self.imagedata = image2
        if self.config['advancedCLUT']:
            self.Create16bColorTable(scale)
            self.CreateOpacityTable(scale)
        else:
            self.Create8bColorTable(scale)
            self.Create8bOpacityTable(scale)

        image2 = self.ApplyConvolution(image2.GetOutput(), update_progress)
        self.final_imagedata = image2

        # Changed the vtkVolumeRayCast to vtkFixedPointVolumeRayCastMapper
        # because it's faster and the image is better
        # TODO: To test if it's true.
        if const.TYPE_RAYCASTING_MAPPER:
            volume_mapper = vtk.vtkVolumeRayCastMapper()
            #volume_mapper.AutoAdjustSampleDistancesOff()
            #volume_mapper.SetInput(image2)
            #volume_mapper.SetVolumeRayCastFunction(composite_function)
            #volume_mapper.SetGradientEstimator(gradientEstimator)
            volume_mapper.IntermixIntersectingGeometryOn()
            self.volume_mapper = volume_mapper
        else:

            if int(ses.Session().rendering) == 0:
                volume_mapper = vtk.vtkFixedPointVolumeRayCastMapper()
                #volume_mapper.AutoAdjustSampleDistancesOff()
                self.volume_mapper = volume_mapper
                volume_mapper.IntermixIntersectingGeometryOn()
            else:
                volume_mapper = vtk.vtkGPUVolumeRayCastMapper()
                volume_mapper.UseJitteringOn()
                self.volume_mapper = volume_mapper

        self.SetTypeRaycasting()
        volume_mapper.SetInputData(image2)

        # TODO: Look to this
        #volume_mapper_hw = vtk.vtkVolumeTextureMapper3D()
        #volume_mapper_hw.SetInput(image2)

        #Cut Plane
        #CutPlane(image2, volume_mapper)

        #self.color_transfer = color_transfer

        volume_properties = vtk.vtkVolumeProperty()
        #volume_properties.IndependentComponentsOn()
        volume_properties.SetInterpolationTypeToLinear()
        volume_properties.SetColor(self.color_transfer)

        try:
            volume_properties.SetScalarOpacity(self.opacity_transfer_func)
        except NameError:
            pass

        if not self.volume_mapper.IsA("vtkGPUVolumeRayCastMapper"):
            # Using these lines to improve the raycasting quality. These values
            # seems related to the distance from ray from raycasting.
            # TODO: Need to see values that improve the quality and don't decrease
            # the performance. 2.0 seems to be a good value to pix_diag
            pix_diag = 2.0
            volume_mapper.SetImageSampleDistance(0.25)
            volume_mapper.SetSampleDistance(pix_diag / 5.0)
            volume_properties.SetScalarOpacityUnitDistance(pix_diag)

        self.volume_properties = volume_properties

        self.SetShading()

        volume = vtk.vtkVolume()
        volume.SetMapper(volume_mapper)
        volume.SetProperty(volume_properties)
        self.volume = volume

        colour = self.GetBackgroundColour()

        self.exist = 1

        if self.plane:
            self.plane.SetVolumeMapper(volume_mapper)

        Publisher.sendMessage('Load volume into viewer',
                              volume=volume,
                              colour=colour,
                              ww=self.ww,
                              wl=self.wl)

        del flip
        del cast
Exemple #51
0
 def traceback(self):
     """the traceback"""
     if self._traceback is None:
         self._traceback = Traceback(self.tb, excinfo=ref(self))
     return self._traceback
Exemple #52
0
 def _set_glyph(self, glyph):
     # Set the weakref to the glyph
     self._glyph = weakref.ref(glyph)
Exemple #53
0
try:
    import __builtin__ as builtins
except ImportError:
    import builtins

try:
    from inspect import getattr_static
except ImportError:
    from .backports.inspect import getattr_static

try:
    from threading import main_thread
except ImportError:
    from threading import _shutdown

    get_main_thread = weakref.ref(
        _shutdown.__self__ if hasattr(_shutdown, '__self__') else _shutdown.im_self)
    del _shutdown
else:
    get_main_thread = weakref.ref(main_thread())

try:
    from types import InstanceType
except ImportError:
    InstanceType = type(object())

try:
    from re import Pattern
except ImportError:
    Pattern = type(re.compile(''))

Exemple #54
0
    def ShowPropertiesForObject(self, uiObject):
        if uiObject is None:
            return
        try:
            len(uiObject)
            self.attributeScroll.LoadContent(contentList=[])
            return
        except:
            pass

        self._selectedObject = weakref.ref(uiObject)
        level = 0
        newNodes = []
        if isinstance(uiObject, Base):
            combined = []
            if hasattr(uiObject, 'color'):
                combined.append(('color', ('color.r', 'color.g', 'color.b', 'color.a')))
            for propertyName, subs in combined:
                propertyNode = ScrollEntryNode(decoClass=UITreePropertyEntry, uiObject=uiObject, level=level, propertyName=propertyName, combineProperties=subs)
                newNodes.append(propertyNode)

            basics = ['name',
             'pos',
             'opacity',
             'padding',
             'displayRect',
             'display',
             'pickState',
             'align',
             'clipChildren',
             'pickRadius',
             'absoluteCoordinates',
             'cacheContents',
             'text',
             'blendMode',
             'spriteEffect',
             'effectAmount',
             'effectAmount2',
             'glowColor',
             'glowFactor',
             'glowExpand',
             'useSizeFromTexture',
             'rotation',
             'rotationCenter',
             'scale',
             'scalingCenter',
             'scalingRotation',
             '',
             '__guid__',
             '__class__']
            for propertyName in basics:
                prop = getattr(uiObject, propertyName, '_!_')
                if prop == '_!_':
                    continue
                propertyNode = ScrollEntryNode(decoClass=UITreePropertyEntry, uiObject=uiObject, level=level, propertyName=propertyName)
                newNodes.append(propertyNode)

        else:
            for attr in dir(uiObject):
                if attr[0] == attr[0].upper():
                    continue
                if attr[0] == '_':
                    continue
                if attr in ('children', 'background'):
                    continue
                propertyNode = ScrollEntryNode(decoClass=UITreePropertyEntry, uiObject=uiObject, level=level, propertyName=attr)
                newNodes.append((attr, propertyNode))

            newNodes = SortListOfTuples(newNodes)
        self.ReloadUIRoots()
        self.attributeScroll.LoadContent(contentList=newNodes)
Exemple #55
0
 def fset(self, value):
     if value is not None:
         value = weakref.ref(value)
     self._element_ref = value
Exemple #56
0
 def set_session(self, session):
     self._session_ref = weakref.ref(session)
Exemple #57
0
 def setPManager(self, pmanager):
     self._pmanager = weakref.ref(pmanager)
Exemple #58
0
 def __init__(self, main_window):
     self.wref = weakref.ref(main_window)
def checkpoint_wrapper(module: nn.Module,
                       offload_to_cpu: bool = False,
                       maintain_forward_counter: bool = False) -> nn.Module:
    """
    A friendlier wrapper for performing activation checkpointing.

    Compared to the PyTorch version, this version:

        - wraps an nn.Module, so that all subsequent calls will use checkpointing
        - handles keyword arguments in the forward
        - handles non-Tensor outputs from the forward
        - supports offloading activations to CPU

    Usage::

        checkpointed_module = checkpoint_wrapper(my_module, offload_to_cpu=True)
        a, b = checkpointed_module(x, y=3, z=torch.Tensor([1]))

    To understand the benefits of checkpointing and the `offload_to_cpu` flag,
    let's divide activations into 2 types: inner activations and outer
    activations w.r.t. the checkpointed modules. The inner ones are saved
    by activation checkpointing, the outer ones are saved by offload_to_cpu.

    In terms of GPU memory savings:

        - When inner ones are large in size and outer ones are small,
          checkpointing helps a lot, offload_to_cpu may help a little.
        - When inner ones are small and outer ones are large,
          checkpointing helps little, offload_to_cpu helps a lot.
        - When both inner and outer are large, both help and the
          benefit is additive.

    ..Note::

        The first and last layers are not likely to benefit from the `offload_to_cpu` flag
        because (1) there are typically other references to the first layer's input, so
        the GPU memory won't be freed; (2) the input to the last layer is immediately
        used by the backward pass and won't result in memory savings.

    Args:
        module (nn.Module):
            The module to be wrapped
        offload_to_cpu (bool):
            Whether to offload activations to CPU.
        maintain_forward_counter (bool):
            If True, maintain a forward counter per inner module. The counter will first
            increases in forward calls of outer forward pass and then decreases in the
            forward calls of outer backward pass. It is used by FullyShardedDataParallel.

    Returns:
        (nn.Module):
            Wrapped module
    """
    # Patch the batchnorm layers in case there are any in this module.
    patch_batchnorm(module)

    if maintain_forward_counter:
        init_counter(module)

    # The use of weakref here is to prevent creating a ref cycle: m -> m.forward -> m.
    # When such cycle exists, gc won't collect the module when the module is freed.
    # That causes GPU memory to be leaked. See the unit test for how we catch that.
    #
    # We prefer this over a class wrapper since the class wrapper would have to
    # proxy a lot of fields and methods.
    module.forward = functools.partial(  # type: ignore
        _checkpointed_forward,
        type(module).forward, weakref.ref(module), offload_to_cpu)
    return module
Exemple #60
0
 def __init__(self, properties):
     # use weakref here to avoid a reference loop
     self.properties = weakref.ref(properties)
     self.temp_vals = {}