<commit_before>/**
 * _watchdog_util.c: Common routines and global data used by _watchdog_fsevents.
 *
 * Copyright (C) 2009, 2010 <NAME> <<EMAIL>>
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "_watchdog_fsevents.h"

/**
 * Dictionary that maps an emitter thread to a CFRunLoop.
 */
static PyObject *g__runloop_for_emitter = NULL;

/**
 * Dictionary that maps an ObservedWatch to a FSEvent stream.
 */
static PyObject *g__stream_for_watch = NULL;


void
WatchdogFSEvents_Init(void)
{
    g__runloop_for_emitter = PyDict_New();
    g__stream_for_watch = PyDict_New();
}

/**
 * Obtains the CFRunLoopRef for a given emitter thread.
 */
CFRunLoopRef
CFRunLoopForEmitter_GetItem(PyObject *emitter_thread)
{
    PyObject *py_runloop = PyDict_GetItem(g__runloop_for_emitter,
                                          emitter_thread);
    CFRunLoopRef runloop = PyCObject_AsVoidPtr(py_runloop);
    return runloop;
}

PyObject *
CFRunLoopForEmitter_SetItem(PyObject *emitter_thread, CFRunLoopRef runloop)
{
    PyObject *emitter_runloop = PyCObject_FromVoidPtr(runloop, PyMem_Free);
    if (0 > PyDict_SetItem(g__runloop_for_emitter,
                           emitter_thread,
                           emitter_runloop))
        {
            Py_DECREF(emitter_runloop);
            return NULL;
        }

    return emitter_runloop;
    //Py_INCREF(emitter_thread);
    //Py_INCREF(emitter_runloop);
}

int
CFRunLoopForEmitter_DelItem(PyObject *emitter_thread)
{
    return PyDict_DelItem(g__runloop_for_emitter, emitter_thread);
    /*if (0 == PyDict_DelItem(g__runloop_for_emitter, emitter_thread))
     {
     Py_DECREF(emitter_thread);
     Py_DECREF(emitter_runloop);
     }*/
}

int
CFRunLoopForEmitter_Contains(PyObject *emitter_thread)
{
    return PyDict_Contains(g__runloop_for_emitter, emitter_thread);
}

/**
 * Get runloop reference from emitter info data or current runloop.
 *
 * @param loops
 *      The dictionary of loops from which to obtain the loop
 *      for the given thread.
 * @return A pointer CFRunLookRef to a runloop.
 */
CFRunLoopRef
CFRunLoopForEmitter_GetItemOrDefault(PyObject *emitter_thread)
{
    PyObject *py_runloop = NULL;
    CFRunLoopRef runloop = NULL;

    py_runloop = PyDict_GetItem(g__runloop_for_emitter, emitter_thread);
    if (NULL == py_runloop)
        {
            runloop = CFRunLoopGetCurrent();
        }
    else
        {
            runloop = /*(CFRunLoopRef)*/PyCObject_AsVoidPtr(py_runloop);
        }

    return runloop;
}

PyObject *
StreamForWatch_SetItem(PyObject *watch, FSEventStreamRef stream)
{
    PyObject *py_stream = PyCObject_FromVoidPtr(stream, PyMem_Free);
    if (0 > PyDict_SetItem(g__stream_for_watch, watch, py_stream))
        {
            Py_DECREF(py_stream);
            return NULL;
        }
    return py_stream;
}

FSEventStreamRef
StreamForWatch_GetItem(PyObject *watch)
{
    PyObject *py_stream = PyDict_GetItem(g__stream_for_watch, watch);
    FSEventStreamRef stream = PyCObject_AsVoidPtr(py_stream);
    return stream;
}

int<commit_msg>Casting is rarely required in C with void *<commit_after>/**
 * _watchdog_util.c: Common routines and global data used by _watchdog_fsevents.
 *
 * Copyright (C) 2009, 2010 <NAME> <<EMAIL>>
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "_watchdog_fsevents.h"

/**
 * Dictionary that maps an emitter thread to a CFRunLoop.
 */
static PyObject *g__runloop_for_emitter = NULL;

/**
 * Dictionary that maps an ObservedWatch to a FSEvent stream.
 */
static PyObject *g__stream_for_watch = NULL;


void
WatchdogFSEvents_Init(void)
{
    g__runloop_for_emitter = PyDict_New();
    g__stream_for_watch = PyDict_New();
}

/**
 * Obtains the CFRunLoopRef for a given emitter thread.
 */
CFRunLoopRef
CFRunLoopForEmitter_GetItem(PyObject *emitter_thread)
{
    PyObject *py_runloop = PyDict_GetItem(g__runloop_for_emitter,
                                          emitter_thread);
    CFRunLoopRef runloop = PyCObject_AsVoidPtr(py_runloop);
    return runloop;
}

PyObject *
CFRunLoopForEmitter_SetItem(PyObject *emitter_thread, CFRunLoopRef runloop)
{
    PyObject *emitter_runloop = PyCObject_FromVoidPtr(runloop, PyMem_Free);
    if (0 > PyDict_SetItem(g__runloop_for_emitter,
                           emitter_thread,
                           emitter_runloop))
        {
            Py_DECREF(emitter_runloop);
            return NULL;
        }

    return emitter_runloop;
    //Py_INCREF(emitter_thread);
    //Py_INCREF(emitter_runloop);
}

int
CFRunLoopForEmitter_DelItem(PyObject *emitter_thread)
{
    return PyDict_DelItem(g__runloop_for_emitter, emitter_thread);
    /*if (0 == PyDict_DelItem(g__runloop_for_emitter, emitter_thread))
     {
     Py_DECREF(emitter_thread);
     Py_DECREF(emitter_runloop);
     }*/
}

int
CFRunLoopForEmitter_Contains(PyObject *emitter_thread)
{
    return PyDict_Contains(g__runloop_for_emitter, emitter_thread);
}

/**
 * Get runloop reference from emitter info data or current runloop.
 *
 * @param loops
 *      The dictionary of loops from which to obtain the loop
 *      for the given thread.
 * @return A pointer CFRunLookRef to a runloop.
 */
CFRunLoopRef
CFRunLoopForEmitter_GetItemOrDefault(PyObject *emitter_thread)
{
    PyObject *py_runloop = NULL;
    CFRunLoopRef runloop = NULL;

    py_runloop = PyDict_GetItem(g__runloop_for_emitter, emitter_thread);
    if (NULL == py_runloop)
        {
            runloop = CFRunLoopGetCurrent();
        }
    else
        {
            runloop = PyCObject_AsVoidPtr(py_runloop);
        }

    return runloop;
}

PyObject *
StreamForWatch_SetItem(PyObject *watch, FSEventStreamRef stream)
{
    PyObject *py_stream = PyCObject_FromVoidPtr(stream, PyMem_Free);
    if (0 > PyDict_SetItem(g__stream_for_watch, watch, py_stream))
        {
            Py_DECREF(py_stream);
            return NULL;
        }
    return py_stream;
}

FSEventStreamRef
StreamForWatch_GetItem(PyObject *watch)
{
    PyObject *py_stream = PyDict_GetItem(g__stream_for_watch, watch);
    FSEventStreamRef stream = PyCObject_AsVoidPtr(py_stream);
    return stream;
}

int
{'fraction_non_alphanumeric': 0.07536407766990291, 'fraction_numerical': 0.0036407766990291263, 'mean_word_length': 3.5910863509749302, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4861166', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2534, 'n_words': 733}