<commit_before>#ifndef PYOSMIUM_CAST_H
#define PYOSMIUM_CAST_H

#include <pybind11/pybind11.h>
#include <datetime.h>
#include <chrono>

namespace pybind11 { namespace detail {
    template <> struct type_caster<osmium::Timestamp> {
    public:
        using type = osmium::Timestamp;

        bool load(handle src, bool) {
            // Lazy initialise the PyDateTime import
            if (!PyDateTimeAPI) { PyDateTime_IMPORT; }

            if (!src) {
                return false;
            }

            if (pybind11::isinstance<pybind11::str>(src)) {
                value = osmium::Timestamp(src.cast<std::string>());
                return true;
            }

            if (!PyDateTime_Check(src.ptr())) {
                return false;
            }

            auto ts = src.attr("timestamp")();
            value = (unsigned) ts.cast<double>();

            return true;
        }

        static handle cast(type const &src, return_value_policy, handle)
        {
            using namespace std::chrono;
            // Lazy initialise the PyDateTime import
            if (!PyDateTimeAPI) { PyDateTime_IMPORT; }

            std::time_t tt = src.seconds_since_epoch();
            std::tm localtime = *std::gmtime(&tt);

            handle pydate = PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
                                                     localtime.tm_mon + 1,
                                                     localtime.tm_mday,
                                                     localtime.tm_hour,
                                                     localtime.tm_min,
                                                     localtime.tm_sec,
                                                     0);

            auto utc = pybind11::module::import("datetime").attr("timezone").attr("utc");
            using namespace pybind11::literals;
            return pydate.attr("replace")("tzinfo"_a=utc).inc_ref();
        }

        PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));
    };
}} // namespace pybind11::detail

#endif // PYOSMIUM_CAST_H<commit_msg>avoid leaking Python handle in timestamp conversion<commit_after>#ifndef PYOSMIUM_CAST_H
#define PYOSMIUM_CAST_H

#include <pybind11/pybind11.h>
#include <datetime.h>
#include <chrono>

namespace pybind11 { namespace detail {
    template <> struct type_caster<osmium::Timestamp> {
    public:
        using type = osmium::Timestamp;

        bool load(handle src, bool) {
            // Lazy initialise the PyDateTime import
            if (!PyDateTimeAPI) { PyDateTime_IMPORT; }

            if (!src) {
                return false;
            }

            if (pybind11::isinstance<pybind11::str>(src)) {
                value = osmium::Timestamp(src.cast<std::string>());
                return true;
            }

            if (!PyDateTime_Check(src.ptr())) {
                return false;
            }

            auto ts = src.attr("timestamp")();
            value = (unsigned) ts.cast<double>();

            return true;
        }

        static handle cast(type const &src, return_value_policy, handle)
        {
            using namespace std::chrono;
            // Lazy initialise the PyDateTime import
            if (!PyDateTimeAPI) { PyDateTime_IMPORT; }

            std::time_t tt = src.seconds_since_epoch();
            std::tm localtime = *std::gmtime(&tt);
            handle pydate = PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
                                                       localtime.tm_mon + 1,
                                                       localtime.tm_mday,
                                                       localtime.tm_hour,
                                                       localtime.tm_min,
                                                       localtime.tm_sec,
                                                       0);

            static auto utc = module::import("datetime").attr("timezone").attr("utc");
            using namespace literals;
            handle with_utc = pydate.attr("replace")("tzinfo"_a=utc).inc_ref();
            pydate.dec_ref();
            return with_utc;
        }

        PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));
    };
}} // namespace pybind11::detail

#endif // PYOSMIUM_CAST_H
{'fraction_non_alphanumeric': 0.10093023255813953, 'fraction_numerical': 0.010232558139534883, 'mean_word_length': 1.4095238095238096, 'pattern_counts': {'":': 0, '<': 19, '<?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': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3126729', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1069, 'n_words': 218}