cinap_lenrekadd hg and python4bb9e2215 years agoHistory
8067 lines · 210.4 KB · C
RawDownload
	    Py_UNICODE c = '\0';	    Py_UNICODE fill;	    PyObject *v = NULL;	    PyObject *temp = NULL;	    Py_UNICODE *pbuf;	    Py_UNICODE sign;	    Py_ssize_t len;	    Py_UNICODE formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ 	    fmt++;	    if (*fmt == '(') {		Py_UNICODE *keystart;		Py_ssize_t keylen;		PyObject *key;		int pcount = 1; 		if (dict == NULL) {		    PyErr_SetString(PyExc_TypeError,				    "format requires a mapping");		    goto onError;		}		++fmt;		--fmtcnt;		keystart = fmt;		/* Skip over balanced parentheses */		while (pcount > 0 && --fmtcnt >= 0) {		    if (*fmt == ')')			--pcount;		    else if (*fmt == '(')			++pcount;		    fmt++;		}		keylen = fmt - keystart - 1;		if (fmtcnt < 0 || pcount > 0) {		    PyErr_SetString(PyExc_ValueError,				    "incomplete format key");		    goto onError;		}#if 0		/* keys are converted to strings using UTF-8 and		   then looked up since Python uses strings to hold		   variables names etc. in its namespaces and we		   wouldn't want to break common idioms. */		key = PyUnicode_EncodeUTF8(keystart,					   keylen,					   NULL);#else		key = PyUnicode_FromUnicode(keystart, keylen);#endif		if (key == NULL)		    goto onError;		if (args_owned) {		    Py_DECREF(args);		    args_owned = 0;		}		args = PyObject_GetItem(dict, key);		Py_DECREF(key);		if (args == NULL) {		    goto onError;		}		args_owned = 1;		arglen = -1;		argidx = -2;	    }	    while (--fmtcnt >= 0) {		switch (c = *fmt++) {		case '-': flags |= F_LJUST; continue;		case '+': flags |= F_SIGN; continue;		case ' ': flags |= F_BLANK; continue;		case '#': flags |= F_ALT; continue;		case '0': flags |= F_ZERO; continue;		}		break;	    }	    if (c == '*') {		v = getnextarg(args, arglen, &argidx);		if (v == NULL)		    goto onError;		if (!PyInt_Check(v)) {		    PyErr_SetString(PyExc_TypeError,				    "* wants int");		    goto onError;		}		width = PyInt_AsLong(v);		if (width < 0) {		    flags |= F_LJUST;		    width = -width;		}		if (--fmtcnt >= 0)		    c = *fmt++;	    }	    else if (c >= '0' && c <= '9') {		width = c - '0';		while (--fmtcnt >= 0) {		    c = *fmt++;		    if (c < '0' || c > '9')			break;		    if ((width*10) / 10 != width) {			PyErr_SetString(PyExc_ValueError,					"width too big");			goto onError;		    }		    width = width*10 + (c - '0');		}	    }	    if (c == '.') {		prec = 0;		if (--fmtcnt >= 0)		    c = *fmt++;		if (c == '*') {		    v = getnextarg(args, arglen, &argidx);		    if (v == NULL)			goto onError;		    if (!PyInt_Check(v)) {			PyErr_SetString(PyExc_TypeError,					"* wants int");			goto onError;		    }		    prec = PyInt_AsLong(v);		    if (prec < 0)			prec = 0;		    if (--fmtcnt >= 0)			c = *fmt++;		}		else if (c >= '0' && c <= '9') {		    prec = c - '0';		    while (--fmtcnt >= 0) {			c = Py_CHARMASK(*fmt++);			if (c < '0' || c > '9')			    break;			if ((prec*10) / 10 != prec) {			    PyErr_SetString(PyExc_ValueError,					    "prec too big");			    goto onError;			}			prec = prec*10 + (c - '0');		    }		}	    } /* prec */	    if (fmtcnt >= 0) {		if (c == 'h' || c == 'l' || c == 'L') {		    if (--fmtcnt >= 0)			c = *fmt++;		}	    }	    if (fmtcnt < 0) {		PyErr_SetString(PyExc_ValueError,				"incomplete format");		goto onError;	    }	    if (c != '%') {		v = getnextarg(args, arglen, &argidx);		if (v == NULL)		    goto onError;	    }	    sign = 0;	    fill = ' ';	    switch (c) { 	    case '%':		pbuf = formatbuf;		/* presume that buffer length is at least 1 */		pbuf[0] = '%';		len = 1;		break; 	    case 's':	    case 'r':		if (PyUnicode_Check(v) && c == 's') {		    temp = v;		    Py_INCREF(temp);		}		else {		    PyObject *unicode;		    if (c == 's')			temp = PyObject_Unicode(v);		    else			temp = PyObject_Repr(v);		    if (temp == NULL)			goto onError;                    if (PyUnicode_Check(temp))                        /* nothing to do */;                    else if (PyString_Check(temp)) {                        /* convert to string to Unicode */		        unicode = PyUnicode_Decode(PyString_AS_STRING(temp),						   PyString_GET_SIZE(temp),						   NULL,						   "strict");		        Py_DECREF(temp);		        temp = unicode;		        if (temp == NULL)			    goto onError;		    }		    else {			Py_DECREF(temp);			PyErr_SetString(PyExc_TypeError,					"%s argument has non-string str()");			goto onError;		    }		}		pbuf = PyUnicode_AS_UNICODE(temp);		len = PyUnicode_GET_SIZE(temp);		if (prec >= 0 && len > prec)		    len = prec;		break; 	    case 'i':	    case 'd':	    case 'u':	    case 'o':	    case 'x':	    case 'X':		if (c == 'i')		    c = 'd';		if (PyLong_Check(v)) {		    temp = formatlong(v, flags, prec, c);		    if (!temp)			goto onError;		    pbuf = PyUnicode_AS_UNICODE(temp);		    len = PyUnicode_GET_SIZE(temp);		    sign = 1;		}		else {		    pbuf = formatbuf;		    len = formatint(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE),				    flags, prec, c, v);		    if (len < 0)			goto onError;		    sign = 1;		}		if (flags & F_ZERO)		    fill = '0';		break; 	    case 'e':	    case 'E':	    case 'f':	    case 'F':	    case 'g':	    case 'G':		if (c == 'F')			c = 'f';		pbuf = formatbuf;		len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE),			flags, prec, c, v);		if (len < 0)		    goto onError;		sign = 1;		if (flags & F_ZERO)		    fill = '0';		break; 	    case 'c':		pbuf = formatbuf;		len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v);		if (len < 0)		    goto onError;		break; 	    default:		PyErr_Format(PyExc_ValueError,			     "unsupported format character '%c' (0x%x) "			     "at index %zd",			     (31<=c && c<=126) ? (char)c : '?',                             (int)c,			     (Py_ssize_t)(fmt - 1 -					  PyUnicode_AS_UNICODE(uformat)));		goto onError;	    }	    if (sign) {		if (*pbuf == '-' || *pbuf == '+') {		    sign = *pbuf++;		    len--;		}		else if (flags & F_SIGN)		    sign = '+';		else if (flags & F_BLANK)		    sign = ' ';		else		    sign = 0;	    }	    if (width < len)		width = len;	    if (rescnt - (sign != 0) < width) {		reslen -= rescnt;		rescnt = width + fmtcnt + 100;		reslen += rescnt;		if (reslen < 0) {		    Py_XDECREF(temp);		    PyErr_NoMemory();		    goto onError;		}		if (_PyUnicode_Resize(&result, reslen) < 0) {		    Py_XDECREF(temp);		    goto onError;		}		res = PyUnicode_AS_UNICODE(result)		    + reslen - rescnt;	    }	    if (sign) {		if (fill != ' ')		    *res++ = sign;		rescnt--;		if (width > len)		    width--;	    }	    if ((flags & F_ALT) && (c == 'x' || c == 'X')) {		assert(pbuf[0] == '0');		assert(pbuf[1] == c);		if (fill != ' ') {		    *res++ = *pbuf++;		    *res++ = *pbuf++;		}		rescnt -= 2;		width -= 2;		if (width < 0)		    width = 0;		len -= 2;	    }	    if (width > len && !(flags & F_LJUST)) {		do {		    --rescnt;		    *res++ = fill;		} while (--width > len);	    }	    if (fill == ' ') {		if (sign)		    *res++ = sign;		if ((flags & F_ALT) && (c == 'x' || c == 'X')) {		    assert(pbuf[0] == '0');		    assert(pbuf[1] == c);		    *res++ = *pbuf++;		    *res++ = *pbuf++;		}	    }	    Py_UNICODE_COPY(res, pbuf, len);	    res += len;	    rescnt -= len;	    while (--width >= len) {		--rescnt;		*res++ = ' ';	    }	    if (dict && (argidx < arglen) && c != '%') {		PyErr_SetString(PyExc_TypeError,				"not all arguments converted during string formatting");                Py_XDECREF(temp);		goto onError;	    }	    Py_XDECREF(temp);	} /* '%' */    } /* until end */    if (argidx < arglen && !dict) {	PyErr_SetString(PyExc_TypeError,			"not all arguments converted during string formatting");	goto onError;    }     if (_PyUnicode_Resize(&result, reslen - rescnt) < 0)	goto onError;    if (args_owned) {	Py_DECREF(args);    }    Py_DECREF(uformat);    return (PyObject *)result;  onError:    Py_XDECREF(result);    Py_DECREF(uformat);    if (args_owned) {	Py_DECREF(args);    }    return NULL;} static PyBufferProcs unicode_as_buffer = {    (readbufferproc) unicode_buffer_getreadbuf,    (writebufferproc) unicode_buffer_getwritebuf,    (segcountproc) unicode_buffer_getsegcount,    (charbufferproc) unicode_buffer_getcharbuf,}; static PyObject *unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); static PyObject *unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds){        PyObject *x = NULL;	static char *kwlist[] = {"string", "encoding", "errors", 0};	char *encoding = NULL;	char *errors = NULL; 	if (type != &PyUnicode_Type)		return unicode_subtype_new(type, args, kwds);	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode",					  kwlist, &x, &encoding, &errors))	    return NULL;	if (x == NULL)		return (PyObject *)_PyUnicode_New(0);	if (encoding == NULL && errors == NULL)	    return PyObject_Unicode(x);	else	return PyUnicode_FromEncodedObject(x, encoding, errors);} static PyObject *unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds){	PyUnicodeObject *tmp, *pnew;	Py_ssize_t n; 	assert(PyType_IsSubtype(type, &PyUnicode_Type));	tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds);	if (tmp == NULL)		return NULL;	assert(PyUnicode_Check(tmp));	pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);	if (pnew == NULL) {		Py_DECREF(tmp);		return NULL;	}	pnew->str = PyMem_NEW(Py_UNICODE, n+1);	if (pnew->str == NULL) {		_Py_ForgetReference((PyObject *)pnew);		PyObject_Del(pnew);		Py_DECREF(tmp);		return PyErr_NoMemory();	}	Py_UNICODE_COPY(pnew->str, tmp->str, n+1);	pnew->length = n;	pnew->hash = tmp->hash;	Py_DECREF(tmp);	return (PyObject *)pnew;} PyDoc_STRVAR(unicode_doc,"unicode(string [, encoding[, errors]]) -> object\n\\n\Create a new Unicode object from the given encoded string.\n\encoding defaults to the current default string encoding.\n\errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'."); PyTypeObject PyUnicode_Type = {    PyObject_HEAD_INIT(&PyType_Type)    0, 					/* ob_size */    "unicode", 				/* tp_name */    sizeof(PyUnicodeObject), 		/* tp_size */    0, 					/* tp_itemsize */    /* Slots */    (destructor)unicode_dealloc, 	/* tp_dealloc */    0, 					/* tp_print */    0,				 	/* tp_getattr */    0, 					/* tp_setattr */    0, 					/* tp_compare */    unicode_repr, 			/* tp_repr */    &unicode_as_number, 		/* tp_as_number */    &unicode_as_sequence, 		/* tp_as_sequence */    &unicode_as_mapping, 		/* tp_as_mapping */    (hashfunc) unicode_hash, 		/* tp_hash*/    0, 					/* tp_call*/    (reprfunc) unicode_str,	 	/* tp_str */    PyObject_GenericGetAttr, 		/* tp_getattro */    0,			 		/* tp_setattro */    &unicode_as_buffer,			/* tp_as_buffer */    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |	    Py_TPFLAGS_BASETYPE,	/* tp_flags */    unicode_doc,			/* tp_doc */    0,					/* tp_traverse */    0,					/* tp_clear */    PyUnicode_RichCompare,		/* tp_richcompare */    0,					/* tp_weaklistoffset */    0,					/* tp_iter */    0,					/* tp_iternext */    unicode_methods,			/* tp_methods */    0,					/* tp_members */    0,					/* tp_getset */    &PyBaseString_Type,			/* tp_base */    0,					/* tp_dict */    0,					/* tp_descr_get */    0,					/* tp_descr_set */    0,					/* tp_dictoffset */    0,					/* tp_init */    0,					/* tp_alloc */    unicode_new,			/* tp_new */    PyObject_Del,      		/* tp_free */}; /* Initialize the Unicode implementation */ void _PyUnicode_Init(void){    int i;     /* XXX - move this array to unicodectype.c ? */    Py_UNICODE linebreak[] = {        0x000A, /* LINE FEED */        0x000D, /* CARRIAGE RETURN */        0x001C, /* FILE SEPARATOR */        0x001D, /* GROUP SEPARATOR */        0x001E, /* RECORD SEPARATOR */

Lines 7501–8000 of 8067. Previous lines