Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Python/codecs.c

    r2 r388  
    1515
    1616/* Import the standard encodings package which will register the first
    17    codec search function. 
     17   codec search function.
    1818
    1919   This is done in a lazy way so that the Unicode implementation does
     
    3131    PyInterpreterState *interp = PyThreadState_GET()->interp;
    3232    if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
    33         goto onError;
     33        goto onError;
    3434    if (search_function == NULL) {
    35         PyErr_BadArgument();
    36         goto onError;
     35        PyErr_BadArgument();
     36        goto onError;
    3737    }
    3838    if (!PyCallable_Check(search_function)) {
    39         PyErr_SetString(PyExc_TypeError, "argument must be callable");
    40         goto onError;
     39        PyErr_SetString(PyExc_TypeError, "argument must be callable");
     40        goto onError;
    4141    }
    4242    return PyList_Append(interp->codec_search_path, search_function);
     
    5656    char *p;
    5757    PyObject *v;
    58    
     58
    5959    if (len > PY_SSIZE_T_MAX) {
    60         PyErr_SetString(PyExc_OverflowError, "string is too large");
    61         return NULL;
    62     }
    63        
     60        PyErr_SetString(PyExc_OverflowError, "string is too large");
     61        return NULL;
     62    }
     63
    6464    v = PyString_FromStringAndSize(NULL, len);
    6565    if (v == NULL)
    66         return NULL;
     66        return NULL;
    6767    p = PyString_AS_STRING(v);
    6868    for (i = 0; i < len; i++) {
     
    7171            ch = '-';
    7272        else
    73             ch = tolower(Py_CHARMASK(ch));
    74         p[i] = ch;
     73            ch = Py_TOLOWER(Py_CHARMASK(ch));
     74        p[i] = ch;
    7575    }
    7676    return v;
     
    8484   effectively case-insensitive.
    8585
    86    If no codec is found, a LookupError is set and NULL returned. 
     86   If no codec is found, a LookupError is set and NULL returned.
    8787
    8888   As side effect, this tries to load the encodings package, if not
     
    9999
    100100    if (encoding == NULL) {
    101         PyErr_BadArgument();
    102         goto onError;
     101        PyErr_BadArgument();
     102        goto onError;
    103103    }
    104104
    105105    interp = PyThreadState_GET()->interp;
    106106    if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
    107         goto onError;
     107        goto onError;
    108108
    109109    /* Convert the encoding to a normalized Python string: all
     
    112112    v = normalizestring(encoding);
    113113    if (v == NULL)
    114         goto onError;
     114        goto onError;
    115115    PyString_InternInPlace(&v);
    116116
     
    118118    result = PyDict_GetItem(interp->codec_search_cache, v);
    119119    if (result != NULL) {
    120         Py_INCREF(result);
    121         Py_DECREF(v);
    122         return result;
    123     }
    124    
     120        Py_INCREF(result);
     121        Py_DECREF(v);
     122        return result;
     123    }
     124
    125125    /* Next, scan the search functions in order of registration */
    126126    args = PyTuple_New(1);
    127127    if (args == NULL)
    128         goto onError;
     128        goto onError;
    129129    PyTuple_SET_ITEM(args,0,v);
    130130
    131131    len = PyList_Size(interp->codec_search_path);
    132132    if (len < 0)
    133         goto onError;
     133        goto onError;
    134134    if (len == 0) {
    135         PyErr_SetString(PyExc_LookupError,
    136                         "no codec search functions registered: "
    137                         "can't find encoding");
    138         goto onError;
     135        PyErr_SetString(PyExc_LookupError,
     136                        "no codec search functions registered: "
     137                        "can't find encoding");
     138        goto onError;
    139139    }
    140140
    141141    for (i = 0; i < len; i++) {
    142         PyObject *func;
    143        
    144         func = PyList_GetItem(interp->codec_search_path, i);
    145         if (func == NULL)
    146             goto onError;
    147         result = PyEval_CallObject(func, args);
    148         if (result == NULL)
    149             goto onError;
    150         if (result == Py_None) {
    151             Py_DECREF(result);
    152             continue;
    153         }
    154         if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
    155             PyErr_SetString(PyExc_TypeError,
    156                             "codec search functions must return 4-tuples");
    157             Py_DECREF(result);
    158             goto onError;
    159         }
    160         break;
     142        PyObject *func;
     143
     144        func = PyList_GetItem(interp->codec_search_path, i);
     145        if (func == NULL)
     146            goto onError;
     147        result = PyEval_CallObject(func, args);
     148        if (result == NULL)
     149            goto onError;
     150        if (result == Py_None) {
     151            Py_DECREF(result);
     152            continue;
     153        }
     154        if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
     155            PyErr_SetString(PyExc_TypeError,
     156                            "codec search functions must return 4-tuples");
     157            Py_DECREF(result);
     158            goto onError;
     159        }
     160        break;
    161161    }
    162162    if (i == len) {
    163         /* XXX Perhaps we should cache misses too ? */
    164         PyErr_Format(PyExc_LookupError,
     163        /* XXX Perhaps we should cache misses too ? */
     164        PyErr_Format(PyExc_LookupError,
    165165                     "unknown encoding: %s", encoding);
    166         goto onError;
     166        goto onError;
    167167    }
    168168
     
    179179static
    180180PyObject *args_tuple(PyObject *object,
    181                      const char *errors)
     181                     const char *errors)
    182182{
    183183    PyObject *args;
    184    
     184
    185185    args = PyTuple_New(1 + (errors != NULL));
    186186    if (args == NULL)
    187         return NULL;
     187        return NULL;
    188188    Py_INCREF(object);
    189189    PyTuple_SET_ITEM(args,0,object);
    190190    if (errors) {
    191         PyObject *v;
    192        
    193         v = PyString_FromString(errors);
    194         if (v == NULL) {
    195             Py_DECREF(args);
    196             return NULL;
    197         }
    198         PyTuple_SET_ITEM(args, 1, v);
     191        PyObject *v;
     192
     193        v = PyString_FromString(errors);
     194        if (v == NULL) {
     195            Py_DECREF(args);
     196            return NULL;
     197        }
     198        PyTuple_SET_ITEM(args, 1, v);
    199199    }
    200200    return args;
     
    211211    codecs = _PyCodec_Lookup(encoding);
    212212    if (codecs == NULL)
    213         return NULL;
     213        return NULL;
    214214    v = PyTuple_GET_ITEM(codecs, index);
    215215    Py_DECREF(codecs);
     
    222222static
    223223PyObject *codec_getincrementalcodec(const char *encoding,
    224                                     const char *errors,
    225                                     const char *attrname)
     224                                    const char *errors,
     225                                    const char *attrname)
    226226{
    227227    PyObject *codecs, *ret, *inccodec;
     
    229229    codecs = _PyCodec_Lookup(encoding);
    230230    if (codecs == NULL)
    231         return NULL;
     231        return NULL;
    232232    inccodec = PyObject_GetAttrString(codecs, attrname);
    233233    Py_DECREF(codecs);
    234234    if (inccodec == NULL)
    235         return NULL;
     235        return NULL;
    236236    if (errors)
    237         ret = PyObject_CallFunction(inccodec, "s", errors);
     237        ret = PyObject_CallFunction(inccodec, "s", errors);
    238238    else
    239         ret = PyObject_CallFunction(inccodec, NULL);
     239        ret = PyObject_CallFunction(inccodec, NULL);
    240240    Py_DECREF(inccodec);
    241241    return ret;
     
    246246static
    247247PyObject *codec_getstreamcodec(const char *encoding,
    248                                PyObject *stream,
    249                                const char *errors,
    250                                const int index)
     248                               PyObject *stream,
     249                               const char *errors,
     250                               const int index)
    251251{
    252252    PyObject *codecs, *streamcodec, *codeccls;
     
    254254    codecs = _PyCodec_Lookup(encoding);
    255255    if (codecs == NULL)
    256         return NULL;
     256        return NULL;
    257257
    258258    codeccls = PyTuple_GET_ITEM(codecs, index);
    259259    if (errors != NULL)
    260         streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
     260        streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
    261261    else
    262         streamcodec = PyObject_CallFunction(codeccls, "O", stream);
     262        streamcodec = PyObject_CallFunction(codeccls, "O", stream);
    263263    Py_DECREF(codecs);
    264264    return streamcodec;
    265265}
    266266
    267 /* Convenience APIs to query the Codec registry. 
    268    
     267/* Convenience APIs to query the Codec registry.
     268
    269269   All APIs return a codec object with incremented refcount.
    270    
     270
    271271 */
    272272
     
    282282
    283283PyObject *PyCodec_IncrementalEncoder(const char *encoding,
    284                                      const char *errors)
     284                                     const char *errors)
    285285{
    286286    return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
     
    288288
    289289PyObject *PyCodec_IncrementalDecoder(const char *encoding,
    290                                      const char *errors)
     290                                     const char *errors)
    291291{
    292292    return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
     
    294294
    295295PyObject *PyCodec_StreamReader(const char *encoding,
    296                                PyObject *stream,
    297                                const char *errors)
     296                               PyObject *stream,
     297                               const char *errors)
    298298{
    299299    return codec_getstreamcodec(encoding, stream, errors, 2);
     
    301301
    302302PyObject *PyCodec_StreamWriter(const char *encoding,
    303                                PyObject *stream,
    304                                const char *errors)
     303                               PyObject *stream,
     304                               const char *errors)
    305305{
    306306    return codec_getstreamcodec(encoding, stream, errors, 3);
     
    313313
    314314PyObject *PyCodec_Encode(PyObject *object,
    315                         const char *encoding,
    316                         const char *errors)
     315                        const char *encoding,
     316                        const char *errors)
    317317{
    318318    PyObject *encoder = NULL;
     
    322322    encoder = PyCodec_Encoder(encoding);
    323323    if (encoder == NULL)
    324         goto onError;
     324        goto onError;
    325325
    326326    args = args_tuple(object, errors);
    327327    if (args == NULL)
    328         goto onError;
    329    
     328        goto onError;
     329
    330330    result = PyEval_CallObject(encoder,args);
    331331    if (result == NULL)
    332         goto onError;
    333 
    334     if (!PyTuple_Check(result) || 
    335         PyTuple_GET_SIZE(result) != 2) {
    336         PyErr_SetString(PyExc_TypeError,
    337                         "encoder must return a tuple (object,integer)");
    338         goto onError;
     332        goto onError;
     333
     334    if (!PyTuple_Check(result) ||
     335        PyTuple_GET_SIZE(result) != 2) {
     336        PyErr_SetString(PyExc_TypeError,
     337                        "encoder must return a tuple (object,integer)");
     338        goto onError;
    339339    }
    340340    v = PyTuple_GET_ITEM(result,0);
     
    346346    Py_DECREF(result);
    347347    return v;
    348        
     348
    349349 onError:
    350350    Py_XDECREF(result);
     
    360360
    361361PyObject *PyCodec_Decode(PyObject *object,
    362                         const char *encoding,
    363                         const char *errors)
     362                        const char *encoding,
     363                        const char *errors)
    364364{
    365365    PyObject *decoder = NULL;
     
    369369    decoder = PyCodec_Decoder(encoding);
    370370    if (decoder == NULL)
    371         goto onError;
     371        goto onError;
    372372
    373373    args = args_tuple(object, errors);
    374374    if (args == NULL)
    375         goto onError;
    376    
     375        goto onError;
     376
    377377    result = PyEval_CallObject(decoder,args);
    378378    if (result == NULL)
    379         goto onError;
    380     if (!PyTuple_Check(result) || 
    381         PyTuple_GET_SIZE(result) != 2) {
    382         PyErr_SetString(PyExc_TypeError,
    383                         "decoder must return a tuple (object,integer)");
    384         goto onError;
     379        goto onError;
     380    if (!PyTuple_Check(result) ||
     381        PyTuple_GET_SIZE(result) != 2) {
     382        PyErr_SetString(PyExc_TypeError,
     383                        "decoder must return a tuple (object,integer)");
     384        goto onError;
    385385    }
    386386    v = PyTuple_GET_ITEM(result,0);
     
    392392    Py_DECREF(result);
    393393    return v;
    394        
     394
    395395 onError:
    396396    Py_XDECREF(args);
     
    410410    PyInterpreterState *interp = PyThreadState_GET()->interp;
    411411    if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
    412         return -1;
     412        return -1;
    413413    if (!PyCallable_Check(error)) {
    414         PyErr_SetString(PyExc_TypeError, "handler must be callable");
    415         return -1;
     414        PyErr_SetString(PyExc_TypeError, "handler must be callable");
     415        return -1;
    416416    }
    417417    return PyDict_SetItemString(interp->codec_error_registry,
    418                                 (char *)name, error);
     418                                (char *)name, error);
    419419}
    420420
     
    428428    PyInterpreterState *interp = PyThreadState_GET()->interp;
    429429    if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
    430         return NULL;
     430        return NULL;
    431431
    432432    if (name==NULL)
    433         name = "strict";
     433        name = "strict";
    434434    handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);
    435435    if (!handler)
    436         PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
     436        PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
    437437    else
    438         Py_INCREF(handler);
     438        Py_INCREF(handler);
    439439    return handler;
    440440}
     
    444444    PyObject *type = PyObject_GetAttrString(exc, "__class__");
    445445    if (type != NULL) {
    446         PyObject *name = PyObject_GetAttrString(type, "__name__");
    447         Py_DECREF(type);
    448         if (name != NULL) {
    449             PyObject *string = PyObject_Str(name);
    450             Py_DECREF(name);
    451             if (string != NULL) {
    452                 PyErr_Format(PyExc_TypeError,
    453                     "don't know how to handle %.400s in error callback",
    454                     PyString_AS_STRING(string));
    455                 Py_DECREF(string);
    456             }
    457         }
     446        PyObject *name = PyObject_GetAttrString(type, "__name__");
     447        Py_DECREF(type);
     448        if (name != NULL) {
     449            PyObject *string = PyObject_Str(name);
     450            Py_DECREF(name);
     451            if (string != NULL) {
     452                PyErr_Format(PyExc_TypeError,
     453                    "don't know how to handle %.400s in error callback",
     454                    PyString_AS_STRING(string));
     455                Py_DECREF(string);
     456            }
     457        }
    458458    }
    459459}
     
    464464        PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
    465465    else
    466         PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
     466        PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
    467467    return NULL;
    468468}
     
    474474    Py_ssize_t end;
    475475    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
    476         if (PyUnicodeEncodeError_GetEnd(exc, &end))
    477             return NULL;
     476        if (PyUnicodeEncodeError_GetEnd(exc, &end))
     477            return NULL;
    478478    }
    479479    else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
    480         if (PyUnicodeDecodeError_GetEnd(exc, &end))
    481             return NULL;
     480        if (PyUnicodeDecodeError_GetEnd(exc, &end))
     481            return NULL;
    482482    }
    483483    else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
    484         if (PyUnicodeTranslateError_GetEnd(exc, &end))
    485             return NULL;
     484        if (PyUnicodeTranslateError_GetEnd(exc, &end))
     485            return NULL;
    486486    }
    487487    else {
    488         wrong_exception_type(exc);
    489         return NULL;
     488        wrong_exception_type(exc);
     489        return NULL;
    490490    }
    491491    /* ouch: passing NULL, 0, pos gives None instead of u'' */
     
    502502
    503503    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
    504         PyObject *res;
    505         Py_UNICODE *p;
    506         if (PyUnicodeEncodeError_GetStart(exc, &start))
    507             return NULL;
    508         if (PyUnicodeEncodeError_GetEnd(exc, &end))
    509             return NULL;
    510         res = PyUnicode_FromUnicode(NULL, end-start);
    511         if (res == NULL)
    512             return NULL;
    513         for (p = PyUnicode_AS_UNICODE(res), i = start;
    514             i<end; ++p, ++i)
    515             *p = '?';
    516         restuple = Py_BuildValue("(On)", res, end);
    517         Py_DECREF(res);
    518         return restuple;
     504        PyObject *res;
     505        Py_UNICODE *p;
     506        if (PyUnicodeEncodeError_GetStart(exc, &start))
     507            return NULL;
     508        if (PyUnicodeEncodeError_GetEnd(exc, &end))
     509            return NULL;
     510        res = PyUnicode_FromUnicode(NULL, end-start);
     511        if (res == NULL)
     512            return NULL;
     513        for (p = PyUnicode_AS_UNICODE(res), i = start;
     514            i<end; ++p, ++i)
     515            *p = '?';
     516        restuple = Py_BuildValue("(On)", res, end);
     517        Py_DECREF(res);
     518        return restuple;
    519519    }
    520520    else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
    521         Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
    522         if (PyUnicodeDecodeError_GetEnd(exc, &end))
    523             return NULL;
    524         return Py_BuildValue("(u#n)", &res, 1, end);
     521        Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
     522        if (PyUnicodeDecodeError_GetEnd(exc, &end))
     523            return NULL;
     524        return Py_BuildValue("(u#n)", &res, (Py_ssize_t)1, end);
    525525    }
    526526    else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
    527         PyObject *res;
    528         Py_UNICODE *p;
    529         if (PyUnicodeTranslateError_GetStart(exc, &start))
    530             return NULL;
    531         if (PyUnicodeTranslateError_GetEnd(exc, &end))
    532             return NULL;
    533         res = PyUnicode_FromUnicode(NULL, end-start);
    534         if (res == NULL)
    535             return NULL;
    536         for (p = PyUnicode_AS_UNICODE(res), i = start;
    537             i<end; ++p, ++i)
    538             *p = Py_UNICODE_REPLACEMENT_CHARACTER;
    539         restuple = Py_BuildValue("(On)", res, end);
    540         Py_DECREF(res);
    541         return restuple;
     527        PyObject *res;
     528        Py_UNICODE *p;
     529        if (PyUnicodeTranslateError_GetStart(exc, &start))
     530            return NULL;
     531        if (PyUnicodeTranslateError_GetEnd(exc, &end))
     532            return NULL;
     533        res = PyUnicode_FromUnicode(NULL, end-start);
     534        if (res == NULL)
     535            return NULL;
     536        for (p = PyUnicode_AS_UNICODE(res), i = start;
     537            i<end; ++p, ++i)
     538            *p = Py_UNICODE_REPLACEMENT_CHARACTER;
     539        restuple = Py_BuildValue("(On)", res, end);
     540        Py_DECREF(res);
     541        return restuple;
    542542    }
    543543    else {
    544         wrong_exception_type(exc);
    545         return NULL;
     544        wrong_exception_type(exc);
     545        return NULL;
    546546    }
    547547}
     
    550550{
    551551    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
    552         PyObject *restuple;
    553         PyObject *object;
    554         Py_ssize_t start;
    555         Py_ssize_t end;
    556         PyObject *res;
    557         Py_UNICODE *p;
    558         Py_UNICODE *startp;
    559         Py_UNICODE *outp;
    560         int ressize;
    561         if (PyUnicodeEncodeError_GetStart(exc, &start))
    562             return NULL;
    563         if (PyUnicodeEncodeError_GetEnd(exc, &end))
    564             return NULL;
    565         if (!(object = PyUnicodeEncodeError_GetObject(exc)))
    566             return NULL;
    567         startp = PyUnicode_AS_UNICODE(object);
    568         for (p = startp+start, ressize = 0; p < startp+end; ++p) {
    569             if (*p<10)
    570                 ressize += 2+1+1;
    571             else if (*p<100)
    572                 ressize += 2+2+1;
    573             else if (*p<1000)
    574                 ressize += 2+3+1;
    575             else if (*p<10000)
    576                 ressize += 2+4+1;
     552        PyObject *restuple;
     553        PyObject *object;
     554        Py_ssize_t start;
     555        Py_ssize_t end;
     556        PyObject *res;
     557        Py_UNICODE *p;
     558        Py_UNICODE *startp;
     559        Py_UNICODE *e;
     560        Py_UNICODE *outp;
     561        int ressize;
     562        if (PyUnicodeEncodeError_GetStart(exc, &start))
     563            return NULL;
     564        if (PyUnicodeEncodeError_GetEnd(exc, &end))
     565            return NULL;
     566        if (!(object = PyUnicodeEncodeError_GetObject(exc)))
     567            return NULL;
     568        startp = PyUnicode_AS_UNICODE(object);
     569        e = startp + end;
     570        for (p = startp+start, ressize = 0; p < e;) {
     571            Py_UCS4 ch = *p++;
    577572#ifndef Py_UNICODE_WIDE
    578             else
    579                 ressize += 2+5+1;
    580 #else
    581             else if (*p<100000)
    582                 ressize += 2+5+1;
    583             else if (*p<1000000)
    584                 ressize += 2+6+1;
    585             else
    586                 ressize += 2+7+1;
     573            if ((0xD800 <= ch && ch <= 0xDBFF) &&
     574                (p < e) &&
     575                (0xDC00 <= *p && *p <= 0xDFFF)) {
     576                ch = ((((ch & 0x03FF) << 10) |
     577                       ((Py_UCS4)*p++ & 0x03FF)) + 0x10000);
     578            }
    587579#endif
    588         }
    589         /* allocate replacement */
    590         res = PyUnicode_FromUnicode(NULL, ressize);
    591         if (res == NULL) {
    592             Py_DECREF(object);
    593             return NULL;
    594         }
    595         /* generate replacement */
    596         for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
    597             p < startp+end; ++p) {
    598             Py_UNICODE c = *p;
    599             int digits;
    600             int base;
    601             *outp++ = '&';
    602             *outp++ = '#';
    603             if (*p<10) {
    604                 digits = 1;
    605                 base = 1;
    606             }
    607             else if (*p<100) {
    608                 digits = 2;
    609                 base = 10;
    610             }
    611             else if (*p<1000) {
    612                 digits = 3;
    613                 base = 100;
    614             }
    615             else if (*p<10000) {
    616                 digits = 4;
    617                 base = 1000;
    618             }
     580            if (ch < 10)
     581                ressize += 2+1+1;
     582            else if (ch < 100)
     583                ressize += 2+2+1;
     584            else if (ch < 1000)
     585                ressize += 2+3+1;
     586            else if (ch < 10000)
     587                ressize += 2+4+1;
     588            else if (ch < 100000)
     589                ressize += 2+5+1;
     590            else if (ch < 1000000)
     591                ressize += 2+6+1;
     592            else
     593                ressize += 2+7+1;
     594        }
     595        /* allocate replacement */
     596        res = PyUnicode_FromUnicode(NULL, ressize);
     597        if (res == NULL) {
     598            Py_DECREF(object);
     599            return NULL;
     600        }
     601        /* generate replacement */
     602        for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); p < e;) {
     603            int digits;
     604            int base;
     605            Py_UCS4 ch = *p++;
    619606#ifndef Py_UNICODE_WIDE
    620             else {
    621                 digits = 5;
    622                 base = 10000;
    623             }
    624 #else
    625             else if (*p<100000) {
    626                 digits = 5;
    627                 base = 10000;
    628             }
    629             else if (*p<1000000) {
    630                 digits = 6;
    631                 base = 100000;
    632             }
    633             else {
    634                 digits = 7;
    635                 base = 1000000;
    636             }
     607            if ((0xD800 <= ch && ch <= 0xDBFF) &&
     608                (p < startp+end) &&
     609                (0xDC00 <= *p && *p <= 0xDFFF)) {
     610                ch = ((((ch & 0x03FF) << 10) |
     611                       ((Py_UCS4)*p++ & 0x03FF)) + 0x10000);
     612            }
    637613#endif
    638             while (digits-->0) {
    639                 *outp++ = '0' + c/base;
    640                 c %= base;
    641                 base /= 10;
    642             }
    643             *outp++ = ';';
    644         }
    645         restuple = Py_BuildValue("(On)", res, end);
    646         Py_DECREF(res);
    647         Py_DECREF(object);
    648         return restuple;
     614            *outp++ = '&';
     615            *outp++ = '#';
     616            if (ch < 10) {
     617                digits = 1;
     618                base = 1;
     619            }
     620            else if (ch < 100) {
     621                digits = 2;
     622                base = 10;
     623            }
     624            else if (ch < 1000) {
     625                digits = 3;
     626                base = 100;
     627            }
     628            else if (ch < 10000) {
     629                digits = 4;
     630                base = 1000;
     631            }
     632            else if (ch < 100000) {
     633                digits = 5;
     634                base = 10000;
     635            }
     636            else if (ch < 1000000) {
     637                digits = 6;
     638                base = 100000;
     639            }
     640            else {
     641                digits = 7;
     642                base = 1000000;
     643            }
     644            while (digits-->0) {
     645                *outp++ = '0' + ch/base;
     646                ch %= base;
     647                base /= 10;
     648            }
     649            *outp++ = ';';
     650        }
     651        restuple = Py_BuildValue("(On)", res, end);
     652        Py_DECREF(res);
     653        Py_DECREF(object);
     654        return restuple;
    649655    }
    650656    else {
    651         wrong_exception_type(exc);
    652         return NULL;
     657        wrong_exception_type(exc);
     658        return NULL;
    653659    }
    654660}
     
    662668{
    663669    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
    664         PyObject *restuple;
    665         PyObject *object;
    666         Py_ssize_t start;
    667         Py_ssize_t end;
    668         PyObject *res;
    669         Py_UNICODE *p;
    670         Py_UNICODE *startp;
    671         Py_UNICODE *outp;
    672         int ressize;
    673         if (PyUnicodeEncodeError_GetStart(exc, &start))
    674             return NULL;
    675         if (PyUnicodeEncodeError_GetEnd(exc, &end))
    676             return NULL;
    677         if (!(object = PyUnicodeEncodeError_GetObject(exc)))
    678             return NULL;
    679         startp = PyUnicode_AS_UNICODE(object);
    680         for (p = startp+start, ressize = 0; p < startp+end; ++p) {
     670        PyObject *restuple;
     671        PyObject *object;
     672        Py_ssize_t start;
     673        Py_ssize_t end;
     674        PyObject *res;
     675        Py_UNICODE *p;
     676        Py_UNICODE *startp;
     677        Py_UNICODE *outp;
     678        int ressize;
     679        if (PyUnicodeEncodeError_GetStart(exc, &start))
     680            return NULL;
     681        if (PyUnicodeEncodeError_GetEnd(exc, &end))
     682            return NULL;
     683        if (!(object = PyUnicodeEncodeError_GetObject(exc)))
     684            return NULL;
     685        startp = PyUnicode_AS_UNICODE(object);
     686        for (p = startp+start, ressize = 0; p < startp+end; ++p) {
    681687#ifdef Py_UNICODE_WIDE
    682             if (*p >= 0x00010000)
    683                 ressize += 1+1+8;
    684             else
     688            if (*p >= 0x00010000)
     689                ressize += 1+1+8;
     690            else
    685691#endif
    686             if (*p >= 0x100) {
    687                 ressize += 1+1+4;
    688             }
    689             else
    690                 ressize += 1+1+2;
    691         }
    692         res = PyUnicode_FromUnicode(NULL, ressize);
    693         if (res==NULL)
    694             return NULL;
    695         for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
    696             p < startp+end; ++p) {
    697             Py_UNICODE c = *p;
    698             *outp++ = '\\';
     692            if (*p >= 0x100) {
     693                ressize += 1+1+4;
     694            }
     695            else
     696                ressize += 1+1+2;
     697        }
     698        res = PyUnicode_FromUnicode(NULL, ressize);
     699        if (res==NULL)
     700            return NULL;
     701        for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
     702            p < startp+end; ++p) {
     703            Py_UNICODE c = *p;
     704            *outp++ = '\\';
    699705#ifdef Py_UNICODE_WIDE
    700             if (c >= 0x00010000) {
    701                 *outp++ = 'U';
    702                 *outp++ = hexdigits[(c>>28)&0xf];
    703                 *outp++ = hexdigits[(c>>24)&0xf];
    704                 *outp++ = hexdigits[(c>>20)&0xf];
    705                 *outp++ = hexdigits[(c>>16)&0xf];
    706                 *outp++ = hexdigits[(c>>12)&0xf];
    707                 *outp++ = hexdigits[(c>>8)&0xf];
    708             }
    709             else
     706            if (c >= 0x00010000) {
     707                *outp++ = 'U';
     708                *outp++ = hexdigits[(c>>28)&0xf];
     709                *outp++ = hexdigits[(c>>24)&0xf];
     710                *outp++ = hexdigits[(c>>20)&0xf];
     711                *outp++ = hexdigits[(c>>16)&0xf];
     712                *outp++ = hexdigits[(c>>12)&0xf];
     713                *outp++ = hexdigits[(c>>8)&0xf];
     714            }
     715            else
    710716#endif
    711             if (c >= 0x100) {
    712                 *outp++ = 'u';
    713                 *outp++ = hexdigits[(c>>12)&0xf];
    714                 *outp++ = hexdigits[(c>>8)&0xf];
    715             }
    716             else
    717                 *outp++ = 'x';
    718             *outp++ = hexdigits[(c>>4)&0xf];
    719             *outp++ = hexdigits[c&0xf];
    720         }
    721 
    722         restuple = Py_BuildValue("(On)", res, end);
    723         Py_DECREF(res);
    724         Py_DECREF(object);
    725         return restuple;
     717            if (c >= 0x100) {
     718                *outp++ = 'u';
     719                *outp++ = hexdigits[(c>>12)&0xf];
     720                *outp++ = hexdigits[(c>>8)&0xf];
     721            }
     722            else
     723                *outp++ = 'x';
     724            *outp++ = hexdigits[(c>>4)&0xf];
     725            *outp++ = hexdigits[c&0xf];
     726        }
     727
     728        restuple = Py_BuildValue("(On)", res, end);
     729        Py_DECREF(res);
     730        Py_DECREF(object);
     731        return restuple;
    726732    }
    727733    else {
    728         wrong_exception_type(exc);
    729         return NULL;
     734        wrong_exception_type(exc);
     735        return NULL;
    730736    }
    731737}
     
    766772{
    767773    static struct {
    768         char *name;
    769         PyMethodDef def;
     774        char *name;
     775        PyMethodDef def;
    770776    } methods[] =
    771777    {
    772         {
    773             "strict",
    774             {
    775                 "strict_errors",
    776                 strict_errors,
    777                 METH_O,
    778                 PyDoc_STR("Implements the 'strict' error handling, which "
    779                           "raises a UnicodeError on coding errors.")
    780             }
    781         },
     778        {
     779            "strict",
     780            {
     781                "strict_errors",
     782                strict_errors,
     783                METH_O,
     784                PyDoc_STR("Implements the 'strict' error handling, which "
     785                          "raises a UnicodeError on coding errors.")
     786            }
     787        },
    782788#ifdef Py_USING_UNICODE
    783         {
    784             "ignore",
    785             {
    786                 "ignore_errors",
    787                 ignore_errors,
    788                 METH_O,
    789                 PyDoc_STR("Implements the 'ignore' error handling, which "
    790                           "ignores malformed data and continues.")
    791             }
    792         },
    793         {
    794             "replace",
    795             {
    796                 "replace_errors",
    797                 replace_errors,
    798                 METH_O,
    799                 PyDoc_STR("Implements the 'replace' error handling, which "
    800                           "replaces malformed data with a replacement marker.")
    801             }
    802         },
    803         {
    804             "xmlcharrefreplace",
    805             {
    806                 "xmlcharrefreplace_errors",
    807                 xmlcharrefreplace_errors,
    808                 METH_O,
    809                 PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "
    810                           "which replaces an unencodable character with the "
    811                           "appropriate XML character reference.")
    812             }
    813         },
    814         {
    815             "backslashreplace",
    816             {
    817                 "backslashreplace_errors",
    818                 backslashreplace_errors,
    819                 METH_O,
    820                 PyDoc_STR("Implements the 'backslashreplace' error handling, "
    821                           "which replaces an unencodable character with a "
    822                           "backslashed escape sequence.")
    823             }
    824         }
     789        {
     790            "ignore",
     791            {
     792                "ignore_errors",
     793                ignore_errors,
     794                METH_O,
     795                PyDoc_STR("Implements the 'ignore' error handling, which "
     796                          "ignores malformed data and continues.")
     797            }
     798        },
     799        {
     800            "replace",
     801            {
     802                "replace_errors",
     803                replace_errors,
     804                METH_O,
     805                PyDoc_STR("Implements the 'replace' error handling, which "
     806                          "replaces malformed data with a replacement marker.")
     807            }
     808        },
     809        {
     810            "xmlcharrefreplace",
     811            {
     812                "xmlcharrefreplace_errors",
     813                xmlcharrefreplace_errors,
     814                METH_O,
     815                PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "
     816                          "which replaces an unencodable character with the "
     817                          "appropriate XML character reference.")
     818            }
     819        },
     820        {
     821            "backslashreplace",
     822            {
     823                "backslashreplace_errors",
     824                backslashreplace_errors,
     825                METH_O,
     826                PyDoc_STR("Implements the 'backslashreplace' error handling, "
     827                          "which replaces an unencodable character with a "
     828                          "backslashed escape sequence.")
     829            }
     830        }
    825831#endif
    826832    };
     
    831837
    832838    if (interp->codec_search_path != NULL)
    833         return 0;
     839        return 0;
    834840
    835841    interp->codec_search_path = PyList_New(0);
     
    838844
    839845    if (interp->codec_error_registry) {
    840         for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) {
    841             PyObject *func = PyCFunction_New(&methods[i].def, NULL);
    842             int res;
    843             if (!func)
    844                 Py_FatalError("can't initialize codec error registry");
    845             res = PyCodec_RegisterError(methods[i].name, func);
    846             Py_DECREF(func);
    847             if (res)
    848                 Py_FatalError("can't initialize codec error registry");
    849         }
     846        for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) {
     847            PyObject *func = PyCFunction_New(&methods[i].def, NULL);
     848            int res;
     849            if (!func)
     850                Py_FatalError("can't initialize codec error registry");
     851            res = PyCodec_RegisterError(methods[i].name, func);
     852            Py_DECREF(func);
     853            if (res)
     854                Py_FatalError("can't initialize codec error registry");
     855        }
    850856    }
    851857
    852858    if (interp->codec_search_path == NULL ||
    853         interp->codec_search_cache == NULL ||
    854         interp->codec_error_registry == NULL)
    855         Py_FatalError("can't initialize codec registry");
     859        interp->codec_search_cache == NULL ||
     860        interp->codec_error_registry == NULL)
     861        Py_FatalError("can't initialize codec registry");
    856862
    857863    mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0);
    858864    if (mod == NULL) {
    859         if (PyErr_ExceptionMatches(PyExc_ImportError)) {
    860             /* Ignore ImportErrors... this is done so that
    861                distributions can disable the encodings package. Note
    862                that other errors are not masked, e.g. SystemErrors
    863                raised to inform the user of an error in the Python
    864                configuration are still reported back to the user. */
    865             PyErr_Clear();
    866             return 0;
    867         }
    868         return -1;
     865        if (PyErr_ExceptionMatches(PyExc_ImportError)) {
     866            /* Ignore ImportErrors... this is done so that
     867               distributions can disable the encodings package. Note
     868               that other errors are not masked, e.g. SystemErrors
     869               raised to inform the user of an error in the Python
     870               configuration are still reported back to the user. */
     871            PyErr_Clear();
     872            return 0;
     873        }
     874        return -1;
    869875    }
    870876    Py_DECREF(mod);
Note: See TracChangeset for help on using the changeset viewer.