<commit_before>'''
Context management
==================

.. versionadded:: 1.2.0

This class manages a registry of all created graphics instructions. It has
the ability to flush and delete them.

You can read more about Kivy graphics contexts in the :doc:`api-kivy.graphics`
module documentation. These are based on
`OpenGL graphics contexts <http://www.opengl.org/wiki/OpenGL_Context>`_.
'''

__all__ = ('Context',)

include "../include/config.pxi"

from cpython.array cimport array
import gc
from os import environ
from weakref import ref
from kivy.graphics.instructions cimport Canvas
from kivy.graphics.texture cimport Texture, TextureRegion
from kivy.graphics.vbo cimport VBO, VertexBatch
from kivy.logger import Logger
from kivy.clock import Clock

from kivy.graphics.cgl cimport *

from kivy.weakmethod import WeakMethod
from time import time
from kivy.cache import Cache

cdef Context context = None

cdef class Context:
    """
    The Context class manages groups of graphics instructions. It can also be used to manage
    observer callbacks. See :meth:`add_reload_observer` and :meth:`remove_reload_observer`
    for more information.
    """
    def __init__(self):
        self.observers = []
        self.observers_before = []
        self.l_texture = []
        self.l_canvas = []
        self.l_fbo = []
        self.flush()

    def trigger_gl_dealloc(self):
        Clock.schedule_del_safe(self.gl_dealloc)

    cpdef void flush(self):
        gc.collect()
        self.lr_texture = array('I')
        self.lr_canvas = []
        self.lr_vbo = array('I')
        self.lr_fbo_rb = array('I')
        self.lr_fbo_fb = array('I')
        self.lr_shadersource = array('I')
        self.lr_shader = []
<commit_msg>Fix #5986 Error in sys.excepthook.<commit_after>'''
Context management
==================

.. versionadded:: 1.2.0

This class manages a registry of all created graphics instructions. It has
the ability to flush and delete them.

You can read more about Kivy graphics contexts in the :doc:`api-kivy.graphics`
module documentation. These are based on
`OpenGL graphics contexts <http://www.opengl.org/wiki/OpenGL_Context>`_.
'''

__all__ = ('Context',)

include "../include/config.pxi"

from cpython.array cimport array
import gc
from os import environ
from weakref import ref
from kivy.graphics.instructions cimport Canvas
from kivy.graphics.texture cimport Texture, TextureRegion
from kivy.graphics.vbo cimport VBO, VertexBatch
from kivy.logger import Logger
from kivy.clock import Clock

from kivy.graphics.cgl cimport *

from kivy.weakmethod import WeakMethod
from time import time
from kivy.cache import Cache

cdef Context context = None

cdef class Context:
    """
    The Context class manages groups of graphics instructions. It can also be used to manage
    observer callbacks. See :meth:`add_reload_observer` and :meth:`remove_reload_observer`
    for more information.
    """
    def __init__(self):
        self.observers = []
        self.observers_before = []
        self.l_texture = []
        self.l_canvas = []
        self.l_fbo = []
        self.flush()

    def trigger_gl_dealloc(self):
        # see https://github.com/kivy/kivy/issues/5986
        # Somehow, the try/except fixes that issue. Calling `self.gl_dealloc()`
        # directly also caused the `Error in sys.excepthook:`, so it's not just
        # the Clock. Perhaps this is called after everything has been deleted
        # and cython objects destroyed, which like a bug in cython/python
        try:
            Clock.schedule_del_safe(self.gl_dealloc)
        except Exception:
            pass

    cpdef void flush(self):
        gc.collect()
        self.lr_texture = array('I')
        self.lr_canvas = []
        self.lr_vbo = array('I')
        self.lr_fbo_rb = array('I')
        self.lr_fbo_fb = array('I')
        self.lr_shadersource = array('I')
        self.lr_shader = []

{'fraction_non_alphanumeric': 0.10061443932411675, 'fraction_numerical': 0.0035842293906810036, 'mean_word_length': 3.125659978880676, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '6168552', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1182, 'n_words': 366}