<commit_before>//
// Copyright (C) 2011-13, DyND Developers
// BSD 2-Clause License, see LICENSE.txt
//

#include "utility_functions.hpp"
#include "dtype_functions.hpp"
#include "ndobject_functions.hpp"

#include <dynd/exceptions.hpp>
#include <dynd/ndobject.hpp>

#include <Python.h>

using namespace std;
using namespace dynd;
using namespace pydynd;

void pydynd::py_decref_function(void* obj)
{
    // Because dynd in general is intended to do things multi-threaded (eventually),
    // the decref function needs to be threadsafe. The way to do that is to ensure
    // we're holding the GIL. This is slower than normal DECREF, but because the
    // reference count isn't an atomic variable, this appears to be the best we
    // can do.
    if (obj != NULL) {
        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        Py_DECREF((PyObject *)obj);

        PyGILState_Release(gstate);
    }
}

size_t pydynd::pyobject_as_size_t(PyObject *obj)
{
    pyobject_ownref ind_obj(PyNumber_Index(obj));
    size_t result = PyLong_AsSize_t(ind_obj);
    if (result == -1 && PyErr_Occurred()) {
        throw exception();
    }
    return result;
}

intptr_t pydynd::pyobject_as_index(PyObject *index)
{
    pyobject_ownref start_obj(PyNumber_Index(index));
    intptr_t result;
    if (PyLong_Check(start_obj.get())) {
        result = PyLong_AsSsize_t(start_obj.get());
#if PY_VERSION_HEX < 0x03000000
    } else if (PyInt_Check(start_obj.get())) {
        result = PyInt_AS_LONG(start_obj.get());
#endif
    } else {
        throw runtime_error("Value returned from PyNumber_Index is not an int or long");
    }
    if (result == -1 && PyErr_Occurred()) {
        throw exception();
    }
    return result;
}

int pydynd::pyobject_as_int_index(PyObject *index)
{
    pyobject_ownref start_obj(PyNumber_Index(index));
#if PY_VERSION_HEX >= 0x03000000
    long result = PyLong_AsLong(start_obj);<commit_msg>Don't use PyLong_AsSize_t on Python < 3<commit_after>//
// Copyright (C) 2011-13, DyND Developers
// BSD 2-Clause License, see LICENSE.txt
//

#include "utility_functions.hpp"
#include "dtype_functions.hpp"
#include "ndobject_functions.hpp"

#include <dynd/exceptions.hpp>
#include <dynd/ndobject.hpp>

#include <Python.h>

using namespace std;
using namespace dynd;
using namespace pydynd;

void pydynd::py_decref_function(void* obj)
{
    // Because dynd in general is intended to do things multi-threaded (eventually),
    // the decref function needs to be threadsafe. The way to do that is to ensure
    // we're holding the GIL. This is slower than normal DECREF, but because the
    // reference count isn't an atomic variable, this appears to be the best we
    // can do.
    if (obj != NULL) {
        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        Py_DECREF((PyObject *)obj);

        PyGILState_Release(gstate);
    }
}

size_t pydynd::pyobject_as_size_t(PyObject *obj)
{
    pyobject_ownref ind_obj(PyNumber_Index(obj));
#if PY_VERSION_HEX >= 0x03000000
    size_t result = PyLong_AsSize_t(ind_obj);
#else
    size_t result = (size_t)PyLong_AsUnsignedLongLong(ind_obj);
#endif
    if (result == (size_t)-1 && PyErr_Occurred()) {
        throw exception();
    }
    return result;
}

intptr_t pydynd::pyobject_as_index(PyObject *index)
{
    pyobject_ownref start_obj(PyNumber_Index(index));
    intptr_t result;
    if (PyLong_Check(start_obj.get())) {
        result = PyLong_AsSsize_t(start_obj.get());
#if PY_VERSION_HEX < 0x03000000
    } else if (PyInt_Check(start_obj.get())) {
        result = PyInt_AS_LONG(start_obj.get());
#endif
    } else {
        throw runtime_error("Value returned from PyNumber_Index is not an int or long");
    }
    if (result == -1 && PyErr_Occurred()) {
        throw exception();
    }
    return result;
}

int pydynd::pyobject_as_int_index(PyObject *index)
{
    pyobject_ownref start_obj(PyNumber_Index(index));
#if PY_VERSION_HEX >= 0x03000000
    long result = PyLong_AsLong(start_obj);
{'fraction_non_alphanumeric': 0.11091637456184276, 'fraction_numerical': 0.01602403605408112, 'mean_word_length': 3.672514619883041, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1520941', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1381, 'n_words': 344}