<commit_before>/*-*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2013 <NAME>, <NAME>

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <Python.h>
#include <structmember.h>
#include <datetime.h>
#include <time.h>
#include <stdio.h>

#include <systemd/sd-journal.h>

#include "pyutil.h"
#include "macro.h"
#include "util.h"
#include "strv.h"
#include "build.h"

typedef struct {
    PyObject_HEAD
    sd_journal *j;
} Reader;
static PyTypeObject ReaderType;


PyDoc_STRVAR(module__doc__,
             "Class to reads the systemd journal similar to journalctl.");


#if PY_MAJOR_VERSION >= 3
static PyTypeObject MonotonicType;

PyDoc_STRVAR(MonotonicType__doc__,
             "A tuple of (timestamp, bootid) for holding monotonic timestamps");

static PyStructSequence_Field MonotonicType_fields[] = {
    {(char*) "timestamp", (char*) "Time"},
    {(char*) "bootid", (char*) "Unique identifier of the boot"},
    {} /* Sentinel */
};

static PyStructSequence_Desc Monotonic_desc = {
    (char*) "journal.Monotonic",
    MonotonicType__doc__,
    MonotonicType_fields,
    2,
};
#endif

static int strv_converter(PyObject* obj, void *_result) {
        char ***result = _result;
        Py_ssize_t i, len;

        assert(result);

        if (!obj)
            goto cleanup;

        if (!PySequence_Check(obj))
            return 0;

        len = PySequence_Length(obj);
        *result = new0(char*, len + 1);

        for (i = 0; i < len; i++) {
            PyObject *item;
#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
            int r;
            PyObject *bytes;
#endif
            char *s, *s2;

            item = PySequence_ITEM(obj, i);
#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
            r = PyUnicode_FSConverter(item, &bytes);
            if (r == 0)<commit_msg>systemd-python: fix initialization of _Reader objects<commit_after>/*-*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2013 <NAME>, <NAME>

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <Python.h>
#include <structmember.h>
#include <datetime.h>
#include <time.h>
#include <stdio.h>

#include <systemd/sd-journal.h>

#include "pyutil.h"
#include "macro.h"
#include "util.h"
#include "strv.h"
#include "build.h"

typedef struct {
    PyObject_HEAD
    sd_journal *j;
} Reader;
static PyTypeObject ReaderType;


PyDoc_STRVAR(module__doc__,
             "Class to reads the systemd journal similar to journalctl.");


#if PY_MAJOR_VERSION >= 3
static PyTypeObject MonotonicType;

PyDoc_STRVAR(MonotonicType__doc__,
             "A tuple of (timestamp, bootid) for holding monotonic timestamps");

static PyStructSequence_Field MonotonicType_fields[] = {
    {(char*) "timestamp", (char*) "Time"},
    {(char*) "bootid", (char*) "Unique identifier of the boot"},
    {} /* Sentinel */
};

static PyStructSequence_Desc Monotonic_desc = {
    (char*) "journal.Monotonic",
    MonotonicType__doc__,
    MonotonicType_fields,
    2,
};
#endif

/**
 * Convert a Python sequence object into a strv (char**), and
 * None into a NULL pointer.
 */
static int strv_converter(PyObject* obj, void *_result) {
        char ***result = _result;
        Py_ssize_t i, len;

        assert(result);

        if (!obj)
            goto cleanup;

        if (obj == Py_None) {
            *result = NULL;
            return 1;
        }

        if (!PySequence_Check(obj))
            return 0;

        len = PySequence_Length(obj);
        *result = new0(char*, len + 1);

        for (i = 0; i < len; i++) {
            PyObject *item;
#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
            int r;
            PyObject *bytes;
#endif
            char *s, *s2;

            item = PySequence_ITEM(obj, i);
#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
            r = PyUnicode_FSConverter(item, &bytes);
            if (r == 0)
{'fraction_non_alphanumeric': 0.11100311041990668, 'fraction_numerical': 0.007581648522550544, 'mean_word_length': 2.93348623853211, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 0, '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': '463012', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1596, 'n_words': 546}