Ticket #4245: urlarg_nostringio.diff

File urlarg_nostringio.diff, 2.1 KB (added by loewis, 5 years ago)
  • twisted/protocols/_c_urlarg.c

     
    1010extern "C" { 
    1111#endif 
    1212#include <Python.h> 
    13 #include <cStringIO.h> 
    1413#ifdef __cplusplus 
    1514} 
    1615#endif 
     
    2322 
    2423static PyObject* UrlargError; 
    2524 
    26 #define OUTPUTCHAR(c,n) PycStringIO->cwrite(output, (const char *)c, n) 
     25#define OUTPUTCHAR(c,n) do{memcpy(output, c, n);output+=n;}while(0) 
    2726 
    2827#define STATE_INITIAL 0 
    2928#define STATE_PERCENT 1 
     
    3837 
    3938static PyObject *unquote(PyObject *self, PyObject *args, PyObject *kwargs) 
    4039{ 
    41     unsigned char *s, *r, *end; 
     40    unsigned char *s, *r, *end, *output_start, *output; 
    4241    unsigned char quotedchar, quotedchartmp = 0, tmp; 
    4342    unsigned char escchar = '%'; /* the character we use to begin %AB sequences */ 
    4443    static char *kwlist[] = {"s", "escchar", NULL}; 
    4544    int state = STATE_INITIAL, length; 
    46     PyObject *output, *str; 
     45    PyObject *str; 
    4746    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|c:unquote", kwlist, &s, &length, &escchar)) { 
    4847        return NULL; 
    4948    } 
    5049    if (length == 0) { 
    5150        return PyString_FromStringAndSize("", 0); 
    5251    } 
    53     /* output = cStringIO() */ 
    54     output = PycStringIO->NewOutput(length); 
    55     if (output == NULL) { 
     52    /* Allocating an output buffer of length will be sufficient, 
     53       as the output can only be smaller. We resize the output in the end. */ 
     54    str = PyString_FromStringAndSize(NULL, length); 
     55    if (str == NULL) { 
    5656        return NULL; 
    5757    } 
     58    output = output_start = (unsigned char*)PyString_AsString(str); 
    5859    end = s + length; 
    5960    s = s - 1; 
    6061    while ((++s) < end) { 
     
    101102        break; 
    102103    } 
    103104 
    104     /* return output.getvalue() */ 
    105     str = PycStringIO->cgetvalue(output); 
    106     Py_DECREF(output); 
     105    _PyString_Resize(&str, output-output_start); 
    107106    return str; 
    108107} 
    109108 
     
    118117    PyObject* d; 
    119118    unsigned char i; 
    120119 
    121     PycString_IMPORT; 
    122120    m = Py_InitModule("_c_urlarg", _c_urlarg_methods); 
    123121    d = PyModule_GetDict(m); 
    124122