<commit_before>/* $Id: curl.c,v 1.82 2002/06/19 13:49:10 kjetilja Exp $ */

/* PycURL -- cURL Python module
 *
 * Author:
 *  <NAME> <kjetilja at cs.uit.no>
 *
 * Contributions:
 *  <NAME> <markus at oberhumer.com>
 *  <NAME> <Tino.Lange at gmx.de>
 *  <NAME> <matt at gnik.com>
 *
 * See file COPYING for license information.
 *
 */
#include "Python.h"
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

/* Ensure we have an updated libcurl */
#if LIBCURL_VERSION_NUM < 0x070908
  #error "Need libcurl version 7.9.8 or greater to compile pycurl."
#endif

static PyObject *ErrorObject;

typedef struct {
    PyObject_HEAD
    CURL *handle;
    struct HttpPost *httppost;
    struct curl_slist *httpheader;
    struct curl_slist *quote;
    struct curl_slist *postquote;
    struct curl_slist *prequote;
    PyObject *w_cb;
    PyObject *h_cb;
    PyObject *r_cb;
    PyObject *pro_cb;
    PyObject *pwd_cb;
    PyObject *d_cb;
    PyObject *readdata;
    PyObject *writedata;
    PyObject *writeheader;
    PyThreadState *state;
    int writeheader_set;
    char error[CURL_ERROR_SIZE];
    void *options[CURLOPT_LASTENTRY];
} CurlObject;

#if !defined(__cplusplus)
staticforward PyTypeObject Curl_Type;
#endif

#define CURLERROR() \
{\
    PyObject *v; \
    v = Py_BuildValue("(is)", res, self->error); \
    PyErr_SetObject(ErrorObject, v); \
    Py_DECREF(v); \
    return NULL; \
}

/* --------------------------------------------------------------------- */

static void
self_cleanup(CurlObject *self)
{
    int i;

    if (self->handle == NULL) {
        return;
    }
    /* Free curl handle */
    if (self->handle != NULL) {
        CURL *handle = self->handle;
        self->handle = NULL;
        /* Must be done without the gil */
        Py_BEGIN_ALLOW_THREADS
        curl_easy_cleanup(handle);
        Py_END_ALLOW_THREADS
    }
    /* Free all slists allocated by setopt */
    if (self->httpheader != NULL) {
        curl_slist_free_all(self->httpheader);
        self->httpheader = NULL;
    }
    if (self->quote != NULL) {
        curl_slist_free_all(self->quote);
        self->quote = NULL;
    }
    if (self->postquote != NULL) {
        curl_slist_free_all(self->postquote);
        self->postquote = NULL;
    }
    if (self->prequote != NULL) {
        curl_slist_free_all(self->prequote);
        self->prequote = NULL;
    }
    if (self->httppost != NULL) {
        curl_formfree(self->httppost);
        self->httppost = NULL;
    }
    /* Free all the strings allocated for setopt */
    for (i = 0; i < CURLOPT_LASTENTRY; i++) {
        if (self->options[i] != NULL) {
            free(self->options[i]);
            self->options[i] = NULL;
        }
    }
    /* Zero thread-state to disallow callbacks to be run from now on */
    self->state = NULL;
    /* Decrement refcount for python callbacks */
    Py_XDECREF(self->w_cb);
    Py_XDECREF(self->r_cb);
    Py_XDECREF(self->pro_cb);
    Py_XDECREF(self->pwd_cb);
    Py_XDECREF(self->h_cb);
    Py_XDECREF(self->d_cb);
    /* Decrement refcount for python file objects */
    Py_XDECREF(self->readdata);
    Py_XDECREF(self->writedata);
    Py_XDECREF(self->writeheader);
}


static void
curl_dealloc(CurlObject *self)
{
    /* If this happens, there's a reference bug and we cannot recover */
    assert(self->state != NULL);
    assert(self != NULL);

    self_cleanup(self);
#if (PY_VERSION_HEX < 0x01060000)<commit_msg>removed some bogus asserts<commit_after>/* $Id: curl.c,v 1.83 2002/06/20 07:41:01 kjetilja Exp $ */

/* PycURL -- cURL Python module
 *
 * Author:
 *  <NAME> <kjetilja at cs.uit.no>
 *
 * Contributions:
 *  <NAME> <markus at oberhumer.com>
 *  <NAME> <Tino.Lange at gmx.de>
 *  <NAME> <matt at gnik.com>
 *
 * See file COPYING for license information.
 *
 */
#include "Python.h"
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

/* Ensure we have an updated libcurl */
#if LIBCURL_VERSION_NUM < 0x070908
  #error "Need libcurl version 7.9.8 or greater to compile pycurl."
#endif

static PyObject *ErrorObject;

typedef struct {
    PyObject_HEAD
    CURL *handle;
    struct HttpPost *httppost;
    struct curl_slist *httpheader;
    struct curl_slist *quote;
    struct curl_slist *postquote;
    struct curl_slist *prequote;
    PyObject *w_cb;
    PyObject *h_cb;
    PyObject *r_cb;
    PyObject *pro_cb;
    PyObject *pwd_cb;
    PyObject *d_cb;
    PyObject *readdata;
    PyObject *writedata;
    PyObject *writeheader;
    PyThreadState *state;
    int writeheader_set;
    char error[CURL_ERROR_SIZE];
    void *options[CURLOPT_LASTENTRY];
} CurlObject;

#if !defined(__cplusplus)
staticforward PyTypeObject Curl_Type;
#endif

#define CURLERROR() \
{\
    PyObject *v; \
    v = Py_BuildValue("(is)", res, self->error); \
    PyErr_SetObject(ErrorObject, v); \
    Py_DECREF(v); \
    return NULL; \
}

/* --------------------------------------------------------------------- */

static void
self_cleanup(CurlObject *self)
{
    int i;

    if (self->handle == NULL) {
        return;
    }
    /* Free curl handle */
    if (self->handle != NULL) {
        CURL *handle = self->handle;
        self->handle = NULL;
        /* Must be done without the gil */
        Py_BEGIN_ALLOW_THREADS
        curl_easy_cleanup(handle);
        Py_END_ALLOW_THREADS
    }
    /* Free all slists allocated by setopt */
    if (self->httpheader != NULL) {
        curl_slist_free_all(self->httpheader);
        self->httpheader = NULL;
    }
    if (self->quote != NULL) {
        curl_slist_free_all(self->quote);
        self->quote = NULL;
    }
    if (self->postquote != NULL) {
        curl_slist_free_all(self->postquote);
        self->postquote = NULL;
    }
    if (self->prequote != NULL) {
        curl_slist_free_all(self->prequote);
        self->prequote = NULL;
    }
    if (self->httppost != NULL) {
        curl_formfree(self->httppost);
        self->httppost = NULL;
    }
    /* Free all the strings allocated for setopt */
    for (i = 0; i < CURLOPT_LASTENTRY; i++) {
        if (self->options[i] != NULL) {
            free(self->options[i]);
            self->options[i] = NULL;
        }
    }
    /* Zero thread-state to disallow callbacks to be run from now on */
    self->state = NULL;
    /* Decrement refcount for python callbacks */
    Py_XDECREF(self->w_cb);
    Py_XDECREF(self->r_cb);
    Py_XDECREF(self->pro_cb);
    Py_XDECREF(self->pwd_cb);
    Py_XDECREF(self->h_cb);
    Py_XDECREF(self->d_cb);
    /* Decrement refcount for python file objects */
    Py_XDECREF(self->readdata);
    Py_XDECREF(self->writedata);
    Py_XDECREF(self->writeheader);
}


static void
curl_dealloc(CurlObject *self)
{
    self_cleanup(self);
#if (PY_VERSION_HEX < 0x01060000)
{'fraction_non_alphanumeric': 0.14856136230182032, 'fraction_numerical': 0.01086318261890781, 'mean_word_length': 2.5282237182806835, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 96, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '851991', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2417, 'n_words': 544}