cinap_lenrekadd hg and python4bb9e2215 years agoHistory
8067 lines · 210.4 KB · C
RawDownload
    Py_XDECREF(exc);    return NULL; onError:    Py_XDECREF(v);    Py_XDECREF(errorHandler);    Py_XDECREF(exc);    return NULL;} /* Return a Unicode-Escape string version of the Unicode object.    If quotes is true, the string is enclosed in u"" or u'' quotes as   appropriate. */ Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s,                                      Py_ssize_t size,                                      Py_UNICODE ch){    /* like wcschr, but doesn't stop at NULL characters */     while (size-- > 0) {        if (*s == ch)            return s;        s++;    }     return NULL;} staticPyObject *unicodeescape_string(const Py_UNICODE *s,                               Py_ssize_t size,                               int quotes){    PyObject *repr;    char *p;     static const char *hexdigit = "0123456789abcdef";     /* Initial allocation is based on the longest-possible unichr       escape.        In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source       unichr, so in this case it's the longest unichr escape. In       narrow (UTF-16) builds this is five chars per source unichr       since there are two unichrs in the surrogate pair, so in narrow       (UTF-16) builds it's not the longest unichr escape.        In wide or narrow builds '\uxxxx' is 6 chars per source unichr,       so in the narrow (UTF-16) build case it's the longest unichr       escape.    */     repr = PyString_FromStringAndSize(NULL,        2#ifdef Py_UNICODE_WIDE        + 10*size#else        + 6*size#endif        + 1);    if (repr == NULL)        return NULL;     p = PyString_AS_STRING(repr);     if (quotes) {        *p++ = 'u';        *p++ = (findchar(s, size, '\'') &&                !findchar(s, size, '"')) ? '"' : '\'';    }    while (size-- > 0) {        Py_UNICODE ch = *s++;         /* Escape quotes and backslashes */        if ((quotes &&	     ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') {            *p++ = '\\';            *p++ = (char) ch;	    continue;        } #ifdef Py_UNICODE_WIDE        /* Map 21-bit characters to '\U00xxxxxx' */        else if (ch >= 0x10000) {            *p++ = '\\';            *p++ = 'U';            *p++ = hexdigit[(ch >> 28) & 0x0000000F];            *p++ = hexdigit[(ch >> 24) & 0x0000000F];            *p++ = hexdigit[(ch >> 20) & 0x0000000F];            *p++ = hexdigit[(ch >> 16) & 0x0000000F];            *p++ = hexdigit[(ch >> 12) & 0x0000000F];            *p++ = hexdigit[(ch >> 8) & 0x0000000F];            *p++ = hexdigit[(ch >> 4) & 0x0000000F];            *p++ = hexdigit[ch & 0x0000000F];	    continue;        }#else	/* Map UTF-16 surrogate pairs to '\U00xxxxxx' */	else if (ch >= 0xD800 && ch < 0xDC00) {	    Py_UNICODE ch2;	    Py_UCS4 ucs; 	    ch2 = *s++;	    size--;	    if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {		ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;		*p++ = '\\';		*p++ = 'U';		*p++ = hexdigit[(ucs >> 28) & 0x0000000F];		*p++ = hexdigit[(ucs >> 24) & 0x0000000F];		*p++ = hexdigit[(ucs >> 20) & 0x0000000F];		*p++ = hexdigit[(ucs >> 16) & 0x0000000F];		*p++ = hexdigit[(ucs >> 12) & 0x0000000F];		*p++ = hexdigit[(ucs >> 8) & 0x0000000F];		*p++ = hexdigit[(ucs >> 4) & 0x0000000F];		*p++ = hexdigit[ucs & 0x0000000F];		continue;	    }	    /* Fall through: isolated surrogates are copied as-is */	    s--;	    size++;	}#endif         /* Map 16-bit characters to '\uxxxx' */        if (ch >= 256) {            *p++ = '\\';            *p++ = 'u';            *p++ = hexdigit[(ch >> 12) & 0x000F];            *p++ = hexdigit[(ch >> 8) & 0x000F];            *p++ = hexdigit[(ch >> 4) & 0x000F];            *p++ = hexdigit[ch & 0x000F];        }         /* Map special whitespace to '\t', \n', '\r' */        else if (ch == '\t') {            *p++ = '\\';            *p++ = 't';        }        else if (ch == '\n') {            *p++ = '\\';            *p++ = 'n';        }        else if (ch == '\r') {            *p++ = '\\';            *p++ = 'r';        }         /* Map non-printable US ASCII to '\xhh' */        else if (ch < ' ' || ch >= 0x7F) {            *p++ = '\\';            *p++ = 'x';            *p++ = hexdigit[(ch >> 4) & 0x000F];            *p++ = hexdigit[ch & 0x000F];        }         /* Copy everything else as-is */        else            *p++ = (char) ch;    }    if (quotes)        *p++ = PyString_AS_STRING(repr)[1];     *p = '\0';    _PyString_Resize(&repr, p - PyString_AS_STRING(repr));    return repr;} PyObject *PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s,					Py_ssize_t size){    return unicodeescape_string(s, size, 0);} PyObject *PyUnicode_AsUnicodeEscapeString(PyObject *unicode){    if (!PyUnicode_Check(unicode)) {        PyErr_BadArgument();        return NULL;    }    return PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(unicode),					 PyUnicode_GET_SIZE(unicode));} /* --- Raw Unicode Escape Codec ------------------------------------------- */ PyObject *PyUnicode_DecodeRawUnicodeEscape(const char *s,					   Py_ssize_t size,					   const char *errors){    const char *starts = s;    Py_ssize_t startinpos;    Py_ssize_t endinpos;    Py_ssize_t outpos;    PyUnicodeObject *v;    Py_UNICODE *p;    const char *end;    const char *bs;    PyObject *errorHandler = NULL;    PyObject *exc = NULL;     /* Escaped strings will always be longer than the resulting       Unicode string, so we start with size here and then reduce the       length after conversion to the true value. (But decoding error       handler might have to resize the string) */    v = _PyUnicode_New(size);    if (v == NULL)	goto onError;    if (size == 0)	return (PyObject *)v;    p = PyUnicode_AS_UNICODE(v);    end = s + size;    while (s < end) {	unsigned char c;	Py_UCS4 x;	int i;        int count; 	/* Non-escape characters are interpreted as Unicode ordinals */	if (*s != '\\') {	    *p++ = (unsigned char)*s++;	    continue;	}	startinpos = s-starts; 	/* \u-escapes are only interpreted iff the number of leading	   backslashes if odd */	bs = s;	for (;s < end;) {	    if (*s != '\\')		break;	    *p++ = (unsigned char)*s++;	}	if (((s - bs) & 1) == 0 ||	    s >= end ||	    (*s != 'u' && *s != 'U')) {	    continue;	}	p--;        count = *s=='u' ? 4 : 8;	s++; 	/* \uXXXX with 4 hex digits, \Uxxxxxxxx with 8 */	outpos = p-PyUnicode_AS_UNICODE(v);	for (x = 0, i = 0; i < count; ++i, ++s) {	    c = (unsigned char)*s;	    if (!isxdigit(c)) {		endinpos = s-starts;		if (unicode_decode_call_errorhandler(		    errors, &errorHandler,		    "rawunicodeescape", "truncated \\uXXXX",		    starts, size, &startinpos, &endinpos, &exc, &s,		    (PyObject **)&v, &outpos, &p))		    goto onError;		goto nextByte;	    }	    x = (x<<4) & ~0xF;	    if (c >= '0' && c <= '9')		x += c - '0';	    else if (c >= 'a' && c <= 'f')		x += 10 + c - 'a';	    else		x += 10 + c - 'A';	}#ifndef Py_UNICODE_WIDE        if (x > 0x10000) {            if (unicode_decode_call_errorhandler(                    errors, &errorHandler,                    "rawunicodeescape", "\\Uxxxxxxxx out of range",		    starts, size, &startinpos, &endinpos, &exc, &s,		    (PyObject **)&v, &outpos, &p))		    goto onError;        }#endif	*p++ = x;	nextByte:	;    }    if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)	goto onError;    Py_XDECREF(errorHandler);    Py_XDECREF(exc);    return (PyObject *)v;  onError:    Py_XDECREF(v);    Py_XDECREF(errorHandler);    Py_XDECREF(exc);    return NULL;} PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,					   Py_ssize_t size){    PyObject *repr;    char *p;    char *q;     static const char *hexdigit = "0123456789abcdef"; #ifdef Py_UNICODE_WIDE    repr = PyString_FromStringAndSize(NULL, 10 * size);#else    repr = PyString_FromStringAndSize(NULL, 6 * size);#endif    if (repr == NULL)        return NULL;    if (size == 0)	return repr;     p = q = PyString_AS_STRING(repr);    while (size-- > 0) {        Py_UNICODE ch = *s++;#ifdef Py_UNICODE_WIDE	/* Map 32-bit characters to '\Uxxxxxxxx' */	if (ch >= 0x10000) {            *p++ = '\\';            *p++ = 'U';            *p++ = hexdigit[(ch >> 28) & 0xf];            *p++ = hexdigit[(ch >> 24) & 0xf];            *p++ = hexdigit[(ch >> 20) & 0xf];            *p++ = hexdigit[(ch >> 16) & 0xf];            *p++ = hexdigit[(ch >> 12) & 0xf];            *p++ = hexdigit[(ch >> 8) & 0xf];            *p++ = hexdigit[(ch >> 4) & 0xf];            *p++ = hexdigit[ch & 15];        }        else#endif	/* Map 16-bit characters to '\uxxxx' */	if (ch >= 256) {            *p++ = '\\';            *p++ = 'u';            *p++ = hexdigit[(ch >> 12) & 0xf];            *p++ = hexdigit[(ch >> 8) & 0xf];            *p++ = hexdigit[(ch >> 4) & 0xf];            *p++ = hexdigit[ch & 15];        }	/* Copy everything else as-is */	else            *p++ = (char) ch;    }    *p = '\0';    _PyString_Resize(&repr, p - q);    return repr;} PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode){    if (!PyUnicode_Check(unicode)) {	PyErr_BadArgument();	return NULL;    }    return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode),					    PyUnicode_GET_SIZE(unicode));} /* --- Unicode Internal Codec ------------------------------------------- */ PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s,					   Py_ssize_t size,					   const char *errors){    const char *starts = s;    Py_ssize_t startinpos;    Py_ssize_t endinpos;    Py_ssize_t outpos;    PyUnicodeObject *v;    Py_UNICODE *p;    const char *end;    const char *reason;    PyObject *errorHandler = NULL;    PyObject *exc = NULL; #ifdef Py_UNICODE_WIDE    Py_UNICODE unimax = PyUnicode_GetMax();#endif     /* XXX overflow detection missing */    v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE);    if (v == NULL)	goto onError;    if (PyUnicode_GetSize((PyObject *)v) == 0)	return (PyObject *)v;    p = PyUnicode_AS_UNICODE(v);    end = s + size;     while (s < end) {        memcpy(p, s, sizeof(Py_UNICODE));        /* We have to sanity check the raw data, otherwise doom looms for           some malformed UCS-4 data. */        if (            #ifdef Py_UNICODE_WIDE            *p > unimax || *p < 0 ||            #endif            end-s < Py_UNICODE_SIZE            )            {            startinpos = s - starts;            if (end-s < Py_UNICODE_SIZE) {                endinpos = end-starts;                reason = "truncated input";            }            else {                endinpos = s - starts + Py_UNICODE_SIZE;                reason = "illegal code point (> 0x10FFFF)";            }            outpos = p - PyUnicode_AS_UNICODE(v);            if (unicode_decode_call_errorhandler(                    errors, &errorHandler,                    "unicode_internal", reason,                    starts, size, &startinpos, &endinpos, &exc, &s,                    (PyObject **)&v, &outpos, &p)) {                goto onError;            }        }        else {            p++;            s += Py_UNICODE_SIZE;        }    }     if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)        goto onError;    Py_XDECREF(errorHandler);    Py_XDECREF(exc);    return (PyObject *)v;  onError:    Py_XDECREF(v);    Py_XDECREF(errorHandler);    Py_XDECREF(exc);    return NULL;} /* --- Latin-1 Codec ------------------------------------------------------ */ PyObject *PyUnicode_DecodeLatin1(const char *s,				 Py_ssize_t size,				 const char *errors){    PyUnicodeObject *v;    Py_UNICODE *p;     /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */    if (size == 1) {	Py_UNICODE r = *(unsigned char*)s;	return PyUnicode_FromUnicode(&r, 1);    }     v = _PyUnicode_New(size);    if (v == NULL)	goto onError;    if (size == 0)	return (PyObject *)v;    p = PyUnicode_AS_UNICODE(v);    while (size-- > 0)	*p++ = (unsigned char)*s++;    return (PyObject *)v;  onError:    Py_XDECREF(v);    return NULL;} /* create or adjust a UnicodeEncodeError */static void make_encode_exception(PyObject **exceptionObject,    const char *encoding,    const Py_UNICODE *unicode, Py_ssize_t size,    Py_ssize_t startpos, Py_ssize_t endpos,    const char *reason){    if (*exceptionObject == NULL) {	*exceptionObject = PyUnicodeEncodeError_Create(	    encoding, unicode, size, startpos, endpos, reason);    }    else {	if (PyUnicodeEncodeError_SetStart(*exceptionObject, startpos))	    goto onError;	if (PyUnicodeEncodeError_SetEnd(*exceptionObject, endpos))	    goto onError;	if (PyUnicodeEncodeError_SetReason(*exceptionObject, reason))	    goto onError;	return;	onError:	Py_DECREF(*exceptionObject);	*exceptionObject = NULL;    }} /* raises a UnicodeEncodeError */static void raise_encode_exception(PyObject **exceptionObject,    const char *encoding,    const Py_UNICODE *unicode, Py_ssize_t size,    Py_ssize_t startpos, Py_ssize_t endpos,    const char *reason)

Lines 2001–2500 of 8067. Previous lines