<commit_before># -*- Mode: Python -*-

from __future__ import absolute_import

import sys
import gc
import unittest
import warnings

import pytest

from gi.repository import GObject, GLib, Gio
from gi import PyGIDeprecationWarning
from gi.module import get_introspection_module
from gi import _gi

import testhelper
from .helper import capture_glib_deprecation_warnings


class TestGObjectAPI(unittest.TestCase):

    def test_call_method_uninitialized_instance(self):
        obj = GObject.Object.__new__(GObject.Object)
        with self.assertRaisesRegex(RuntimeError, '.*is not initialized'):
            obj.notify("foo")

    def test_gobject_inheritance(self):
        # GObject.Object is a class hierarchy as follows:
        # overrides.Object -> introspection.Object -> static.GObject
        GIObjectModule = get_introspection_module('GObject')
        self.assertTrue(issubclass(GObject.Object, GIObjectModule.Object))
        self.assertTrue(issubclass(GIObjectModule.Object, _gi.GObject))<commit_msg>tests: add tests for GObject.Object.weak_ref<commit_after># -*- Mode: Python -*-

from __future__ import absolute_import

import sys
import gc
import unittest
import warnings
import weakref
import platform

import pytest

from gi.repository import GObject, GLib, Gio
from gi import PyGIDeprecationWarning
from gi.module import get_introspection_module
from gi import _gi

import testhelper
from .helper import capture_glib_deprecation_warnings


@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="crashes")
def test_gobject_weak_ref():

    called = []

    def callback(*args):
        called.extend(args)

    # object gets finalized
    obj = GObject.Object()
    obj.weak_ref(callback, 1)
    del obj
    gc.collect()
    gc.collect()
    assert called == [1]
    del called[:]

    # wrapper gets finalized first
    obj = GObject.Object()
    pyref = weakref.ref(obj, lambda x: callback(-2))
    value = GObject.Value(GObject.Object, obj)
    ref = obj.weak_ref(callback, 2)
    del obj
    gc.collect()
    assert called == [-2]
    del pyref
    value.unset()
    gc.collect()
    assert called == [-2, 2]
    del called[:]

    # weakref gets unregistered first
    obj = GObject.Object()
    ref = obj.weak_ref(callback, 3)
    ref.unref()
    del obj
    gc.collect()
    assert not called

    # weakref gets GCed
    obj = GObject.Object()
    obj.weak_ref(callback, 4)
    gc.collect()
    del obj
    assert called == [4]


class TestGObjectAPI(unittest.TestCase):

    def test_call_method_uninitialized_instance(self):
        obj = GObject.Object.__new__(GObject.Object)
        with self.assertRaisesRegex(RuntimeError, '.*is not initialized'):
            obj.notify("foo")

    def test_gobject_inheritance(self):
        # GObject.Object is a class hierarchy as follows:
        # overrides.Object -> introspection.Object -> static.GObject
        GIObjectModule = get_introspection_module('GObject')
        self.assertTrue(issubclass(GObject.Object, GIObjectModule.Object))
        self.assertTrue(issubclass(GIObjectModule.Object, _gi.GObject))
{'fraction_non_alphanumeric': 0.09342783505154639, 'fraction_numerical': 0.0032216494845360823, 'mean_word_length': 3.5594713656387666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1267931', 'n_tokens_mistral': 959, 'n_tokens_neox': 938, 'n_words': 236}