<commit_before>#include <python2.7/Python.h>

#include "libwbxml/wbxml.h"
#include "wbxml_helpers.h"


static PyObject* WBXMLParseError;

static PyObject* xml_to_wbxml(PyObject* self, PyObject* args, PyObject* kwargs) {

    // initialize return code & error strings
    WBXMLError ret = WBXML_OK;
    char error_context[1024] = {0};
    char error_string[1024] = {0};

    // interpret arguments
    char* keywords[] = {"xml", "version", "preserve_whitespaces", "string_table", "publicid", NULL};
    WB_UTINY *xml;
    WB_ULONG xml_len;
    char* language = NULL;
    int language_len = 0;
    char* version = "1.3";
    int version_len = 3;
    int preserve_whitespaces = 0;
    unsigned int string_table = 0;
    unsigned int publicid = 1;
    if (!PyArg_ParseTupleAndKeywords(
        args, kwargs,
        "s#|s#iII", keywords,
        &xml, &xml_len,
        &version, &version_len,
        &preserve_whitespaces, &string_table, &publicid
    )) {
        return NULL;
    }

    // initialize converter
    WBXMLConvXML2WBXML *conv = NULL;
    ret = wbxml_conv_xml2wbxml_create(&conv);
    if (ret != WBXML_OK) {
        snprintf(error_context, sizeof(error_context), "execution of `wbxml_conv_xml2wbxml_create` failed");
        goto clean_up;
    }
    // configure converter
    wbxml_conv_xml2wbxml_set_version(conv, get_version(version));
    if (preserve_whitespaces) {
        wbxml_conv_xml2wbxml_enable_preserve_whitespaces(conv);
    }
    if (!string_table) {
        wbxml_conv_xml2wbxml_disable_string_table(conv);
    }
    if (!publicid) {
        wbxml_conv_xml2wbxml_disable_public_id(conv);
    }

    // convert
    WB_UTINY *wbxml;
    WB_ULONG wbxml_len = 0;
    ret = wbxml_conv_xml2wbxml_run(conv, xml, xml_len, &wbxml, &wbxml_len);
    if (ret != WBXML_OK) {
        snprintf(error_context, sizeof(error_context), "execution of `wbxml_conv_wbxml2xml_run` failed with wbxml_len=%d", xml_len);
        goto clean_up;
    }

    // the end!
    clean_up:

    if (conv != NULL) {
        wbxml_conv_xml2wbxml_destroy(conv);
    }

    // raise exception
    if (ret != WBXML_OK) {
        if (error_context[0] == '\0') {
            snprintf(error_string, sizeof(error_string), "%s", (const char*)wbxml_errors_string(ret));
        } else {
            snprintf(error_string, sizeof(error_string), "%s (%s)", (const char*)wbxml_errors_string(ret), error_context);
        }
        PyErr_SetString(WBXMLParseError, error_string);
        return NULL;
    }

    // success
    PyObject *value = PyString_FromStringAndSize((const char*)wbxml, wbxml_len);//
    if (wbxml != NULL) {
        wbxml_free(wbxml);
    }
    return value;
}


char* keywords[] = {"wbxml", "version", "preserve_whitespaces", "string_table", "publicid", NULL};<commit_msg>[DEBUG] fixed another memory leak<commit_after>#include <python2.7/Python.h>

#include "libwbxml/wbxml.h"
#include "wbxml_helpers.h"


static PyObject* WBXMLParseError;

static PyObject* xml_to_wbxml(PyObject* self, PyObject* args, PyObject* kwargs) {

    // initialize return code & error strings
    WBXMLError ret = WBXML_OK;
    char error_context[1024] = {0};
    char error_string[1024] = {0};

    // interpret arguments
    char* keywords[] = {"xml", "version", "preserve_whitespaces", "string_table", "publicid", NULL};
    WB_UTINY *xml;
    WB_ULONG xml_len;
    char* language = NULL;
    int language_len = 0;
    char* version = "1.3";
    int version_len = 3;
    int preserve_whitespaces = 0;
    unsigned int string_table = 0;
    unsigned int publicid = 1;
    if (!PyArg_ParseTupleAndKeywords(
        args, kwargs,
        "s#|s#iII", keywords,
        &xml, &xml_len,
        &version, &version_len,
        &preserve_whitespaces, &string_table, &publicid
    )) {
        return NULL;
    }

    // initialize converter
    WBXMLConvXML2WBXML *conv = NULL;
    ret = wbxml_conv_xml2wbxml_create(&conv);
    if (ret != WBXML_OK) {
        snprintf(error_context, sizeof(error_context), "execution of `wbxml_conv_xml2wbxml_create` failed");
        goto clean_up;
    }
    // configure converter
    wbxml_conv_xml2wbxml_set_version(conv, get_version(version));
    if (preserve_whitespaces) {
        wbxml_conv_xml2wbxml_enable_preserve_whitespaces(conv);
    }
    if (!string_table) {
        wbxml_conv_xml2wbxml_disable_string_table(conv);
    }
    if (!publicid) {
        wbxml_conv_xml2wbxml_disable_public_id(conv);
    }

    // convert
    WB_UTINY *wbxml;
    WB_ULONG wbxml_len = 0;
    ret = wbxml_conv_xml2wbxml_run(conv, xml, xml_len, &wbxml, &wbxml_len);
    if (ret != WBXML_OK) {
        snprintf(error_context, sizeof(error_context), "execution of `wbxml_conv_wbxml2xml_run` failed with wbxml_len=%d", xml_len);
        goto clean_up;
    }

    // the end!
    clean_up:

    if (conv != NULL) {
        wbxml_conv_xml2wbxml_destroy(conv);
    }

    // raise exception
    if (ret != WBXML_OK) {
        if (wbxml != NULL) {
            wbxml_free(wbxml);
        }
        if (error_context[0] == '\0') {
            snprintf(error_string, sizeof(error_string), "%s", (const char*)wbxml_errors_string(ret));
        } else {
            snprintf(error_string, sizeof(error_string), "%s (%s)", (const char*)wbxml_errors_string(ret), error_context);
        }
        PyErr_SetString(WBXMLParseError, error_string);
        return NULL;
    }

    // success
    PyObject *value = PyString_FromStringAndSize((const char*)wbxml, wbxml_len);
    if (wbxml != NULL) {
        wbxml_free(wbxml);
    }
    return value;
}


char* keywords[] = {"wbxml", "version", "preserve_whitespaces", "string_table", "publicid", NULL};
{'fraction_non_alphanumeric': 0.12185172046825116, 'fraction_numerical': 0.011351543100390209, 'mean_word_length': 3.104075691411936, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 109}, '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': '5722293', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1943, 'n_words': 391}