void _PyModule_Clear(PyObject *m) { /* To make the execution order of destructors for global objects a bit more predictable, we first zap all objects whose name starts with a single underscore, before we clear the entire dictionary. We zap them by replacing them with None, rather than deleting them from the dictionary, to avoid rehashing the dictionary (to some extent). */ Py_ssize_t pos; PyObject *key, *value; PyObject *d; d = ((PyModuleObject *)m)->md_dict; if (d == NULL) return; /* First, clear only names starting with a single underscore */ pos = 0; while (PyDict_Next(d, &pos, &key, &value)) { if (value != Py_None && PyUnicode_Check(key)) { if (PyUnicode_READ_CHAR(key, 0) == '_' && PyUnicode_READ_CHAR(key, 1) != '_') { if (Py_VerboseFlag > 1) { const char *s = _PyUnicode_AsString(key); if (s != NULL) PySys_WriteStderr("# clear[1] %s\n", s); else PyErr_Clear(); } PyDict_SetItem(d, key, Py_None); } } } /* Next, clear all names except for __builtins__ */ pos = 0; while (PyDict_Next(d, &pos, &key, &value)) { if (value != Py_None && PyUnicode_Check(key)) { if (PyUnicode_READ_CHAR(key, 0) != '_' || PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0) { if (Py_VerboseFlag > 1) { const char *s = _PyUnicode_AsString(key); if (s != NULL) PySys_WriteStderr("# clear[2] %s\n", s); else PyErr_Clear(); } PyDict_SetItem(d, key, Py_None); } } } /* Note: we leave __builtins__ in place, so that destructors of non-global objects defined in this module can still use builtins, in particularly 'None'. */ }
PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) { if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { PyObject *restuple; PyObject *object; Py_ssize_t i; Py_ssize_t start; Py_ssize_t end; PyObject *res; unsigned char *outp; int ressize; Py_UCS4 c; if (PyUnicodeEncodeError_GetStart(exc, &start)) return NULL; if (PyUnicodeEncodeError_GetEnd(exc, &end)) return NULL; if (!(object = PyUnicodeEncodeError_GetObject(exc))) return NULL; for (i = start, ressize = 0; i < end; ++i) { /* object is guaranteed to be "ready" */ c = PyUnicode_READ_CHAR(object, i); if (c >= 0x10000) { ressize += 1+1+8; } else if (c >= 0x100) { ressize += 1+1+4; } else ressize += 1+1+2; } res = PyUnicode_New(ressize, 127); if (res==NULL) return NULL; for (i = start, outp = PyUnicode_1BYTE_DATA(res); i < end; ++i) { c = PyUnicode_READ_CHAR(object, i); *outp++ = '\\'; if (c >= 0x00010000) { *outp++ = 'U'; *outp++ = Py_hexdigits[(c>>28)&0xf]; *outp++ = Py_hexdigits[(c>>24)&0xf]; *outp++ = Py_hexdigits[(c>>20)&0xf]; *outp++ = Py_hexdigits[(c>>16)&0xf]; *outp++ = Py_hexdigits[(c>>12)&0xf]; *outp++ = Py_hexdigits[(c>>8)&0xf]; } else if (c >= 0x100) { *outp++ = 'u'; *outp++ = Py_hexdigits[(c>>12)&0xf]; *outp++ = Py_hexdigits[(c>>8)&0xf]; }
static int _set_char(const char *name, Py_UCS4 *target, PyObject *src, Py_UCS4 dflt) { if (src == NULL) *target = dflt; else { *target = '\0'; if (src != Py_None) { Py_ssize_t len; if (!PyUnicode_Check(src)) { PyErr_Format(PyExc_TypeError, "\"%s\" must be string, not %.200s", name, src->ob_type->tp_name); return -1; } len = PyUnicode_GetLength(src); if (len > 1) { PyErr_Format(PyExc_TypeError, "\"%s\" must be a 1-character string", name); return -1; } /* PyUnicode_READY() is called in PyUnicode_GetLength() */ if (len > 0) *target = PyUnicode_READ_CHAR(src, 0); } } return 0; }
/* get_integer consumes 0 or more decimal digit characters from an input string, updates *result with the corresponding positive integer, and returns the number of digits consumed. returns -1 on error. */ static int get_integer(PyObject *str, Py_ssize_t *pos, Py_ssize_t end, Py_ssize_t *result) { Py_ssize_t accumulator, digitval; int numdigits; accumulator = numdigits = 0; for (;;(*pos)++, numdigits++) { if (*pos >= end) break; digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ_CHAR(str, *pos)); if (digitval < 0) break; /* Detect possible overflow before it happens: accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if accumulator > (PY_SSIZE_T_MAX - digitval) / 10. */ if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) { PyErr_Format(PyExc_ValueError, "Too many decimal digits in format string"); return -1; } accumulator = accumulator * 10 + digitval; } *result = accumulator; return numdigits; }
/* Given a number of the form: digits[remainder] where ptr points to the start and end points to the end, find where the integer part ends. This could be a decimal, an exponent, both, or neither. If a decimal point is present, set *has_decimal and increment remainder beyond it. Results are undefined (but shouldn't crash) for improperly formatted strings. */ static void parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end, Py_ssize_t *n_remainder, int *has_decimal) { Py_ssize_t remainder; while (pos<end && Py_ISDIGIT(PyUnicode_READ_CHAR(s, pos))) ++pos; remainder = pos; /* Does remainder start with a decimal point? */ *has_decimal = pos<end && PyUnicode_READ_CHAR(s, remainder) == '.'; /* Skip the decimal point. */ if (*has_decimal) remainder++; *n_remainder = end - remainder; }
static PyObject * zipimport_zipimporter_get_data_impl(ZipImporter *self, PyObject *path) /*[clinic end generated code: output=65dc506aaa268436 input=fa6428b74843c4ae]*/ { PyObject *key; PyObject *toc_entry; Py_ssize_t path_start, path_len, len; if (self->archive == NULL) { PyErr_SetString(PyExc_ValueError, "zipimporter.__init__() wasn't called"); return NULL; } #ifdef ALTSEP path = _PyObject_CallMethodId((PyObject *)&PyUnicode_Type, &PyId_replace, "OCC", path, ALTSEP, SEP); if (!path) return NULL; #else Py_INCREF(path); #endif if (PyUnicode_READY(path) == -1) goto error; path_len = PyUnicode_GET_LENGTH(path); len = PyUnicode_GET_LENGTH(self->archive); path_start = 0; if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1) && PyUnicode_READ_CHAR(path, len) == SEP) { path_start = len + 1; } key = PyUnicode_Substring(path, path_start, path_len); if (key == NULL) goto error; toc_entry = PyDict_GetItem(self->files, key); if (toc_entry == NULL) { PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, key); Py_DECREF(key); goto error; } Py_DECREF(key); Py_DECREF(path); return get_data(self->archive, toc_entry); error: Py_DECREF(path); return NULL; }
static PyObject * zipimporter_get_data(PyObject *obj, PyObject *args) { ZipImporter *self = (ZipImporter *)obj; PyObject *path, *key; #ifdef ALTSEP _Py_IDENTIFIER(replace); #endif PyObject *toc_entry; Py_ssize_t path_start, path_len, len; if (!PyArg_ParseTuple(args, "U:zipimporter.get_data", &path)) return NULL; #ifdef ALTSEP path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP); if (!path) return NULL; #else Py_INCREF(path); #endif if (PyUnicode_READY(path) == -1) goto error; path_len = PyUnicode_GET_LENGTH(path); len = PyUnicode_GET_LENGTH(self->archive); path_start = 0; if (PyUnicode_Tailmatch(path, self->archive, 0, len, -1) && PyUnicode_READ_CHAR(path, len) == SEP) { path_start = len + 1; } key = PyUnicode_Substring(path, path_start, path_len); if (key == NULL) goto error; toc_entry = PyDict_GetItem(self->files, key); if (toc_entry == NULL) { PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, key); Py_DECREF(key); goto error; } Py_DECREF(key); Py_DECREF(path); return get_data(self->archive, toc_entry); error: Py_DECREF(path); return NULL; }
static NUMBA_INLINE Py_UCS4 __Numba_PyObject_AsPy_UCS4(PyObject* x) { long ival; if (PyUnicode_Check(x)) { Py_ssize_t length; #if CYTHON_PEP393_ENABLED length = PyUnicode_GET_LENGTH(x); if (likely(length == 1)) { return PyUnicode_READ_CHAR(x, 0); } #else length = PyUnicode_GET_SIZE(x); if (likely(length == 1)) { return PyUnicode_AS_UNICODE(x)[0]; } #if Py_UNICODE_SIZE == 2 else if (PyUnicode_GET_SIZE(x) == 2) { Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0]; if (high_val >= 0xD800 && high_val <= 0xDBFF) { Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1]; if (low_val >= 0xDC00 && low_val <= 0xDFFF) { return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1))); } } } #endif #endif PyErr_Format(PyExc_ValueError, "only single character unicode strings can be converted to Py_UCS4, " "got length %" CYTHON_FORMAT_SSIZE_T "d", length); return (Py_UCS4)-1; } ival = __Numba_PyInt_AsLong(x); if (unlikely(ival < 0)) { if (!PyErr_Occurred()) PyErr_SetString(PyExc_OverflowError, "cannot convert negative value to Py_UCS4"); return (Py_UCS4)-1; } else if (unlikely(ival > 1114111)) { PyErr_SetString(PyExc_OverflowError, "value too large to convert to Py_UCS4"); return (Py_UCS4)-1; } return (Py_UCS4)ival; }
static NUMBA_INLINE Py_UNICODE __Numba_PyObject_AsPy_UNICODE(PyObject* x) { long ival; #if CYTHON_PEP393_ENABLED #if Py_UNICODE_SIZE > 2 const long maxval = 1114111; #else const long maxval = 65535; #endif #else static long maxval = 0; #endif if (PyUnicode_Check(x)) { if (unlikely(__Numba_PyUnicode_GET_LENGTH(x) != 1)) { PyErr_Format(PyExc_ValueError, "only single character unicode strings can be converted to Py_UNICODE, " "got length %" CYTHON_FORMAT_SSIZE_T "d", __Numba_PyUnicode_GET_LENGTH(x)); return (Py_UNICODE)-1; } #if CYTHON_PEP393_ENABLED ival = PyUnicode_READ_CHAR(x, 0); #else return PyUnicode_AS_UNICODE(x)[0]; #endif } else { #if !CYTHON_PEP393_ENABLED if (unlikely(!maxval)) maxval = (long)PyUnicode_GetMax(); #endif ival = __Numba_PyInt_AsLong(x); } if (unlikely(ival < 0)) { if (!PyErr_Occurred()) PyErr_SetString(PyExc_OverflowError, "cannot convert negative value to Py_UNICODE"); return (Py_UNICODE)-1; } else if (unlikely(ival > maxval)) { PyErr_SetString(PyExc_OverflowError, "value too large to convert to Py_UNICODE"); return (Py_UNICODE)-1; } return (Py_UNICODE)ival; }
static int _set_char(const char *name, Py_UCS4 *target, PyObject *src, Py_UCS4 dflt) { if (src == NULL) *target = dflt; else { *target = '\0'; if (src != Py_None) { Py_ssize_t len; len = PyUnicode_GetLength(src); if (len > 1) { PyErr_Format(PyExc_TypeError, "\"%s\" must be an 1-character string", name); return -1; } if (len > 0) *target = PyUnicode_READ_CHAR(src, 0); } } return 0; }
/* much of this is taken from unicodeobject.c */ static int format_float_internal(PyObject *value, const InternalFormatSpec *format, _PyUnicodeWriter *writer) { char *buf = NULL; /* buffer returned from PyOS_double_to_string */ Py_ssize_t n_digits; Py_ssize_t n_remainder; Py_ssize_t n_total; int has_decimal; double val; int precision, default_precision = 6; Py_UCS4 type = format->type; int add_pct = 0; Py_ssize_t index; NumberFieldWidths spec; int flags = 0; int result = -1; Py_UCS4 maxchar = 127; Py_UCS4 sign_char = '\0'; int float_type; /* Used to see if we have a nan, inf, or regular float. */ PyObject *unicode_tmp = NULL; /* Locale settings, either from the actual locale or from a hard-code pseudo-locale */ LocaleInfo locale = STATIC_LOCALE_INFO_INIT; if (format->precision > INT_MAX) { PyErr_SetString(PyExc_ValueError, "precision too big"); goto done; } precision = (int)format->precision; if (format->alternate) flags |= Py_DTSF_ALT; if (type == '\0') { /* Omitted type specifier. Behaves in the same way as repr(x) and str(x) if no precision is given, else like 'g', but with at least one digit after the decimal point. */ flags |= Py_DTSF_ADD_DOT_0; type = 'r'; default_precision = 0; } if (type == 'n') /* 'n' is the same as 'g', except for the locale used to format the result. We take care of that later. */ type = 'g'; val = PyFloat_AsDouble(value); if (val == -1.0 && PyErr_Occurred()) goto done; if (type == '%') { type = 'f'; val *= 100; add_pct = 1; } if (precision < 0) precision = default_precision; else if (type == 'r') type = 'g'; /* Cast "type", because if we're in unicode we need to pass a 8-bit char. This is safe, because we've restricted what "type" can be. */ buf = PyOS_double_to_string(val, (char)type, precision, flags, &float_type); if (buf == NULL) goto done; n_digits = strlen(buf); if (add_pct) { /* We know that buf has a trailing zero (since we just called strlen() on it), and we don't use that fact any more. So we can just write over the trailing zero. */ buf[n_digits] = '%'; n_digits += 1; } if (format->sign != '+' && format->sign != ' ' && format->width == -1 && format->type != 'n' && !format->thousands_separators) { /* Fast path */ result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits); PyMem_Free(buf); return result; } /* Since there is no unicode version of PyOS_double_to_string, just use the 8 bit version and then convert to unicode. */ unicode_tmp = _PyUnicode_FromASCII(buf, n_digits); PyMem_Free(buf); if (unicode_tmp == NULL) goto done; /* Is a sign character present in the output? If so, remember it and skip it */ index = 0; if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') { sign_char = '-'; ++index; --n_digits; } /* Determine if we have any "remainder" (after the digits, might include decimal or exponent or both (or neither)) */ parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal); /* Determine the grouping, separator, and decimal point, if any. */ if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : (format->thousands_separators ? LT_DEFAULT_LOCALE : LT_NO_LOCALE), &locale) == -1) goto done; /* Calculate how much memory we'll need. */ n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index, index + n_digits, n_remainder, has_decimal, &locale, format, &maxchar); /* Allocate the memory. */ if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1) goto done; /* Populate the memory. */ result = fill_number(writer, &spec, unicode_tmp, index, index + n_digits, NULL, 0, format->fill_char, &locale, 0); done: Py_XDECREF(unicode_tmp); free_locale_info(&locale); return result; }
static int format_long_internal(PyObject *value, const InternalFormatSpec *format, _PyUnicodeWriter *writer) { int result = -1; Py_UCS4 maxchar = 127; PyObject *tmp = NULL; Py_ssize_t inumeric_chars; Py_UCS4 sign_char = '\0'; Py_ssize_t n_digits; /* count of digits need from the computed string */ Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which produces non-digits */ Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */ Py_ssize_t n_total; Py_ssize_t prefix = 0; NumberFieldWidths spec; long x; /* Locale settings, either from the actual locale or from a hard-code pseudo-locale */ LocaleInfo locale = STATIC_LOCALE_INFO_INIT; /* no precision allowed on integers */ if (format->precision != -1) { PyErr_SetString(PyExc_ValueError, "Precision not allowed in integer format specifier"); goto done; } /* special case for character formatting */ if (format->type == 'c') { /* error to specify a sign */ if (format->sign != '\0') { PyErr_SetString(PyExc_ValueError, "Sign not allowed with integer" " format specifier 'c'"); goto done; } /* taken from unicodeobject.c formatchar() */ /* Integer input truncated to a character */ x = PyLong_AsLong(value); if (x == -1 && PyErr_Occurred()) goto done; if (x < 0 || x > 0x10ffff) { PyErr_SetString(PyExc_OverflowError, "%c arg not in range(0x110000)"); goto done; } tmp = PyUnicode_FromOrdinal(x); inumeric_chars = 0; n_digits = 1; maxchar = Py_MAX(maxchar, (Py_UCS4)x); /* As a sort-of hack, we tell calc_number_widths that we only have "remainder" characters. calc_number_widths thinks these are characters that don't get formatted, only copied into the output string. We do this for 'c' formatting, because the characters are likely to be non-digits. */ n_remainder = 1; } else { int base; int leading_chars_to_skip = 0; /* Number of characters added by PyNumber_ToBase that we want to skip over. */ /* Compute the base and how many characters will be added by PyNumber_ToBase */ switch (format->type) { case 'b': base = 2; leading_chars_to_skip = 2; /* 0b */ break; case 'o': base = 8; leading_chars_to_skip = 2; /* 0o */ break; case 'x': case 'X': base = 16; leading_chars_to_skip = 2; /* 0x */ break; default: /* shouldn't be needed, but stops a compiler warning */ case 'd': case 'n': base = 10; break; } if (format->sign != '+' && format->sign != ' ' && format->width == -1 && format->type != 'X' && format->type != 'n' && !format->thousands_separators && PyLong_CheckExact(value)) { /* Fast path */ return _PyLong_FormatWriter(writer, value, base, format->alternate); } /* The number of prefix chars is the same as the leading chars to skip */ if (format->alternate) n_prefix = leading_chars_to_skip; /* Do the hard part, converting to a string in a given base */ tmp = _PyLong_Format(value, base); if (tmp == NULL || PyUnicode_READY(tmp) == -1) goto done; inumeric_chars = 0; n_digits = PyUnicode_GET_LENGTH(tmp); prefix = inumeric_chars; /* Is a sign character present in the output? If so, remember it and skip it */ if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') { sign_char = '-'; ++prefix; ++leading_chars_to_skip; } /* Skip over the leading chars (0x, 0b, etc.) */ n_digits -= leading_chars_to_skip; inumeric_chars += leading_chars_to_skip; } /* Determine the grouping, separator, and decimal point, if any. */ if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : (format->thousands_separators ? LT_DEFAULT_LOCALE : LT_NO_LOCALE), &locale) == -1) goto done; /* Calculate how much memory we'll need. */ n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars, inumeric_chars + n_digits, n_remainder, 0, &locale, format, &maxchar); /* Allocate the memory. */ if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1) goto done; /* Populate the memory. */ result = fill_number(writer, &spec, tmp, inumeric_chars, inumeric_chars + n_digits, tmp, prefix, format->fill_char, &locale, format->type == 'X'); done: Py_XDECREF(tmp); free_locale_info(&locale); return result; }
static int format_complex_internal(PyObject *value, const InternalFormatSpec *format, _PyUnicodeWriter *writer) { double re; double im; char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */ char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */ InternalFormatSpec tmp_format = *format; Py_ssize_t n_re_digits; Py_ssize_t n_im_digits; Py_ssize_t n_re_remainder; Py_ssize_t n_im_remainder; Py_ssize_t n_re_total; Py_ssize_t n_im_total; int re_has_decimal; int im_has_decimal; int precision, default_precision = 6; Py_UCS4 type = format->type; Py_ssize_t i_re; Py_ssize_t i_im; NumberFieldWidths re_spec; NumberFieldWidths im_spec; int flags = 0; int result = -1; Py_UCS4 maxchar = 127; enum PyUnicode_Kind rkind; void *rdata; Py_UCS4 re_sign_char = '\0'; Py_UCS4 im_sign_char = '\0'; int re_float_type; /* Used to see if we have a nan, inf, or regular float. */ int im_float_type; int add_parens = 0; int skip_re = 0; Py_ssize_t lpad; Py_ssize_t rpad; Py_ssize_t total; PyObject *re_unicode_tmp = NULL; PyObject *im_unicode_tmp = NULL; /* Locale settings, either from the actual locale or from a hard-code pseudo-locale */ LocaleInfo locale = STATIC_LOCALE_INFO_INIT; if (format->precision > INT_MAX) { PyErr_SetString(PyExc_ValueError, "precision too big"); goto done; } precision = (int)format->precision; /* Zero padding is not allowed. */ if (format->fill_char == '0') { PyErr_SetString(PyExc_ValueError, "Zero padding is not allowed in complex format " "specifier"); goto done; } /* Neither is '=' alignment . */ if (format->align == '=') { PyErr_SetString(PyExc_ValueError, "'=' alignment flag is not allowed in complex format " "specifier"); goto done; } re = PyComplex_RealAsDouble(value); if (re == -1.0 && PyErr_Occurred()) goto done; im = PyComplex_ImagAsDouble(value); if (im == -1.0 && PyErr_Occurred()) goto done; if (format->alternate) flags |= Py_DTSF_ALT; if (type == '\0') { /* Omitted type specifier. Should be like str(self). */ type = 'r'; default_precision = 0; if (re == 0.0 && copysign(1.0, re) == 1.0) skip_re = 1; else add_parens = 1; } if (type == 'n') /* 'n' is the same as 'g', except for the locale used to format the result. We take care of that later. */ type = 'g'; if (precision < 0) precision = default_precision; else if (type == 'r') type = 'g'; /* Cast "type", because if we're in unicode we need to pass a 8-bit char. This is safe, because we've restricted what "type" can be. */ re_buf = PyOS_double_to_string(re, (char)type, precision, flags, &re_float_type); if (re_buf == NULL) goto done; im_buf = PyOS_double_to_string(im, (char)type, precision, flags, &im_float_type); if (im_buf == NULL) goto done; n_re_digits = strlen(re_buf); n_im_digits = strlen(im_buf); /* Since there is no unicode version of PyOS_double_to_string, just use the 8 bit version and then convert to unicode. */ re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits); if (re_unicode_tmp == NULL) goto done; i_re = 0; im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits); if (im_unicode_tmp == NULL) goto done; i_im = 0; /* Is a sign character present in the output? If so, remember it and skip it */ if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') { re_sign_char = '-'; ++i_re; --n_re_digits; } if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') { im_sign_char = '-'; ++i_im; --n_im_digits; } /* Determine if we have any "remainder" (after the digits, might include decimal or exponent or both (or neither)) */ parse_number(re_unicode_tmp, i_re, i_re + n_re_digits, &n_re_remainder, &re_has_decimal); parse_number(im_unicode_tmp, i_im, i_im + n_im_digits, &n_im_remainder, &im_has_decimal); /* Determine the grouping, separator, and decimal point, if any. */ if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : (format->thousands_separators ? LT_DEFAULT_LOCALE : LT_NO_LOCALE), &locale) == -1) goto done; /* Turn off any padding. We'll do it later after we've composed the numbers without padding. */ tmp_format.fill_char = '\0'; tmp_format.align = '<'; tmp_format.width = -1; /* Calculate how much memory we'll need. */ n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp, i_re, i_re + n_re_digits, n_re_remainder, re_has_decimal, &locale, &tmp_format, &maxchar); /* Same formatting, but always include a sign, unless the real part is * going to be omitted, in which case we use whatever sign convention was * requested by the original format. */ if (!skip_re) tmp_format.sign = '+'; n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp, i_im, i_im + n_im_digits, n_im_remainder, im_has_decimal, &locale, &tmp_format, &maxchar); if (skip_re) n_re_total = 0; /* Add 1 for the 'j', and optionally 2 for parens. */ calc_padding(n_re_total + n_im_total + 1 + add_parens * 2, format->width, format->align, &lpad, &rpad, &total); if (lpad || rpad) maxchar = Py_MAX(maxchar, format->fill_char); if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1) goto done; rkind = writer->kind; rdata = writer->data; /* Populate the memory. First, the padding. */ result = fill_padding(writer, n_re_total + n_im_total + 1 + add_parens * 2, format->fill_char, lpad, rpad); if (result == -1) goto done; if (add_parens) { PyUnicode_WRITE(rkind, rdata, writer->pos, '('); writer->pos++; } if (!skip_re) { result = fill_number(writer, &re_spec, re_unicode_tmp, i_re, i_re + n_re_digits, NULL, 0, 0, &locale, 0); if (result == -1) goto done; } result = fill_number(writer, &im_spec, im_unicode_tmp, i_im, i_im + n_im_digits, NULL, 0, 0, &locale, 0); if (result == -1) goto done; PyUnicode_WRITE(rkind, rdata, writer->pos, 'j'); writer->pos++; if (add_parens) { PyUnicode_WRITE(rkind, rdata, writer->pos, ')'); writer->pos++; } writer->pos += rpad; done: PyMem_Free(re_buf); PyMem_Free(im_buf); Py_XDECREF(re_unicode_tmp); Py_XDECREF(im_unicode_tmp); free_locale_info(&locale); return result; }
PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) { PyObject *object; Py_ssize_t i; Py_ssize_t start; Py_ssize_t end; PyObject *res; unsigned char *outp; int ressize; Py_UCS4 c; if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { const unsigned char *p; if (PyUnicodeDecodeError_GetStart(exc, &start)) return NULL; if (PyUnicodeDecodeError_GetEnd(exc, &end)) return NULL; if (!(object = PyUnicodeDecodeError_GetObject(exc))) return NULL; p = (const unsigned char*)PyBytes_AS_STRING(object); res = PyUnicode_New(4 * (end - start), 127); if (res == NULL) { Py_DECREF(object); return NULL; } outp = PyUnicode_1BYTE_DATA(res); for (i = start; i < end; i++, outp += 4) { unsigned char c = p[i]; outp[0] = '\\'; outp[1] = 'x'; outp[2] = Py_hexdigits[(c>>4)&0xf]; outp[3] = Py_hexdigits[c&0xf]; } assert(_PyUnicode_CheckConsistency(res, 1)); Py_DECREF(object); return Py_BuildValue("(Nn)", res, end); } if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { if (PyUnicodeEncodeError_GetStart(exc, &start)) return NULL; if (PyUnicodeEncodeError_GetEnd(exc, &end)) return NULL; if (!(object = PyUnicodeEncodeError_GetObject(exc))) return NULL; } else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { if (PyUnicodeTranslateError_GetStart(exc, &start)) return NULL; if (PyUnicodeTranslateError_GetEnd(exc, &end)) return NULL; if (!(object = PyUnicodeTranslateError_GetObject(exc))) return NULL; } else { wrong_exception_type(exc); return NULL; } if (end - start > PY_SSIZE_T_MAX / (1+1+8)) end = start + PY_SSIZE_T_MAX / (1+1+8); for (i = start, ressize = 0; i < end; ++i) { /* object is guaranteed to be "ready" */ c = PyUnicode_READ_CHAR(object, i); if (c >= 0x10000) { ressize += 1+1+8; } else if (c >= 0x100) { ressize += 1+1+4; } else ressize += 1+1+2; } res = PyUnicode_New(ressize, 127); if (res == NULL) { Py_DECREF(object); return NULL; } outp = PyUnicode_1BYTE_DATA(res); for (i = start; i < end; ++i) { c = PyUnicode_READ_CHAR(object, i); *outp++ = '\\'; if (c >= 0x00010000) { *outp++ = 'U'; *outp++ = Py_hexdigits[(c>>28)&0xf]; *outp++ = Py_hexdigits[(c>>24)&0xf]; *outp++ = Py_hexdigits[(c>>20)&0xf]; *outp++ = Py_hexdigits[(c>>16)&0xf]; *outp++ = Py_hexdigits[(c>>12)&0xf]; *outp++ = Py_hexdigits[(c>>8)&0xf]; } else if (c >= 0x100) {
PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc) { if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { PyObject *restuple; PyObject *object; Py_ssize_t i; Py_ssize_t start; Py_ssize_t end; PyObject *res; unsigned char *outp; Py_ssize_t ressize; Py_UCS4 ch; if (PyUnicodeEncodeError_GetStart(exc, &start)) return NULL; if (PyUnicodeEncodeError_GetEnd(exc, &end)) return NULL; if (!(object = PyUnicodeEncodeError_GetObject(exc))) return NULL; if (end - start > PY_SSIZE_T_MAX / (2+7+1)) end = start + PY_SSIZE_T_MAX / (2+7+1); for (i = start, ressize = 0; i < end; ++i) { /* object is guaranteed to be "ready" */ ch = PyUnicode_READ_CHAR(object, i); if (ch<10) ressize += 2+1+1; else if (ch<100) ressize += 2+2+1; else if (ch<1000) ressize += 2+3+1; else if (ch<10000) ressize += 2+4+1; else if (ch<100000) ressize += 2+5+1; else if (ch<1000000) ressize += 2+6+1; else ressize += 2+7+1; } /* allocate replacement */ res = PyUnicode_New(ressize, 127); if (res == NULL) { Py_DECREF(object); return NULL; } outp = PyUnicode_1BYTE_DATA(res); /* generate replacement */ for (i = start; i < end; ++i) { int digits; int base; ch = PyUnicode_READ_CHAR(object, i); *outp++ = '&'; *outp++ = '#'; if (ch<10) { digits = 1; base = 1; } else if (ch<100) { digits = 2; base = 10; } else if (ch<1000) { digits = 3; base = 100; } else if (ch<10000) { digits = 4; base = 1000; } else if (ch<100000) { digits = 5; base = 10000; } else if (ch<1000000) { digits = 6; base = 100000; } else { digits = 7; base = 1000000; } while (digits-->0) { *outp++ = '0' + ch/base; ch %= base; base /= 10; } *outp++ = ';'; } assert(_PyUnicode_CheckConsistency(res, 1)); restuple = Py_BuildValue("(Nn)", res, end); Py_DECREF(object); return restuple; } else { wrong_exception_type(exc); return NULL; } }
PyObject * PyFile_GetLine(PyObject *f, int n) { PyObject *result; if (f == NULL) { PyErr_BadInternalCall(); return NULL; } { PyObject *reader; PyObject *args; _Py_IDENTIFIER(readline); reader = _PyObject_GetAttrId(f, &PyId_readline); if (reader == NULL) return NULL; if (n <= 0) args = PyTuple_New(0); else args = Py_BuildValue("(i)", n); if (args == NULL) { Py_DECREF(reader); return NULL; } result = PyEval_CallObject(reader, args); Py_DECREF(reader); Py_DECREF(args); if (result != NULL && !PyBytes_Check(result) && !PyUnicode_Check(result)) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_TypeError, "object.readline() returned non-string"); } } if (n < 0 && result != NULL && PyBytes_Check(result)) { char *s = PyBytes_AS_STRING(result); Py_ssize_t len = PyBytes_GET_SIZE(result); if (len == 0) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } else if (s[len-1] == '\n') { if (result->ob_refcnt == 1) _PyBytes_Resize(&result, len-1); else { PyObject *v; v = PyBytes_FromStringAndSize(s, len-1); Py_DECREF(result); result = v; } } } if (n < 0 && result != NULL && PyUnicode_Check(result)) { Py_ssize_t len = PyUnicode_GET_LENGTH(result); if (len == 0) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } else if (PyUnicode_READ_CHAR(result, len-1) == '\n') { PyObject *v; v = PyUnicode_Substring(result, 0, len-1); Py_DECREF(result); result = v; } } return result; }
/* zipimporter.__init__ Split the "subdirectory" from the Zip archive path, lookup a matching entry in sys.path_importer_cache, fetch the file directory from there if found, or else read it from the archive. */ static int zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds) { PyObject *path, *files, *tmp; PyObject *filename = NULL; Py_ssize_t len, flen; if (!_PyArg_NoKeywords("zipimporter()", kwds)) return -1; if (!PyArg_ParseTuple(args, "O&:zipimporter", PyUnicode_FSDecoder, &path)) return -1; if (PyUnicode_READY(path) == -1) return -1; len = PyUnicode_GET_LENGTH(path); if (len == 0) { PyErr_SetString(ZipImportError, "archive path is empty"); goto error; } #ifdef ALTSEP tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP); if (!tmp) goto error; Py_DECREF(path); path = tmp; #endif filename = path; Py_INCREF(filename); flen = len; for (;;) { struct stat statbuf; int rv; rv = _Py_stat(filename, &statbuf); if (rv == -2) goto error; if (rv == 0) { /* it exists */ if (!S_ISREG(statbuf.st_mode)) /* it's a not file */ Py_CLEAR(filename); break; } Py_CLEAR(filename); /* back up one path element */ flen = PyUnicode_FindChar(path, SEP, 0, flen, -1); if (flen == -1) break; filename = PyUnicode_Substring(path, 0, flen); if (filename == NULL) goto error; } if (filename == NULL) { PyErr_SetString(ZipImportError, "not a Zip file"); goto error; } if (PyUnicode_READY(filename) < 0) goto error; files = PyDict_GetItem(zip_directory_cache, filename); if (files == NULL) { files = read_directory(filename); if (files == NULL) goto error; if (PyDict_SetItem(zip_directory_cache, filename, files) != 0) goto error; } else Py_INCREF(files); self->files = files; /* Transfer reference */ self->archive = filename; filename = NULL; /* Check if there is a prefix directory following the filename. */ if (flen != len) { tmp = PyUnicode_Substring(path, flen+1, PyUnicode_GET_LENGTH(path)); if (tmp == NULL) goto error; self->prefix = tmp; if (PyUnicode_READ_CHAR(path, len-1) != SEP) { /* add trailing SEP */ tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP); if (tmp == NULL) goto error; Py_SETREF(self->prefix, tmp); } } else self->prefix = PyUnicode_New(0, 0); Py_DECREF(path); return 0; error: Py_DECREF(path); Py_XDECREF(filename); return -1; }
static int zipimport_zipimporter___init___impl(ZipImporter *self, PyObject *path) /*[clinic end generated code: output=141558fefdb46dc8 input=92b9ebeed1f6a704]*/ { PyObject *files, *tmp; PyObject *filename = NULL; Py_ssize_t len, flen; if (PyUnicode_READY(path) == -1) return -1; len = PyUnicode_GET_LENGTH(path); if (len == 0) { PyErr_SetString(ZipImportError, "archive path is empty"); goto error; } #ifdef ALTSEP tmp = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP); if (!tmp) goto error; Py_DECREF(path); path = tmp; #endif filename = path; Py_INCREF(filename); flen = len; for (;;) { struct stat statbuf; int rv; rv = _Py_stat(filename, &statbuf); if (rv == -2) goto error; if (rv == 0) { /* it exists */ if (!S_ISREG(statbuf.st_mode)) /* it's a not file */ Py_CLEAR(filename); break; } Py_CLEAR(filename); /* back up one path element */ flen = PyUnicode_FindChar(path, SEP, 0, flen, -1); if (flen == -1) break; filename = PyUnicode_Substring(path, 0, flen); if (filename == NULL) goto error; } if (filename == NULL) { PyErr_SetString(ZipImportError, "not a Zip file"); goto error; } if (PyUnicode_READY(filename) < 0) goto error; files = PyDict_GetItem(zip_directory_cache, filename); if (files == NULL) { files = read_directory(filename); if (files == NULL) goto error; if (PyDict_SetItem(zip_directory_cache, filename, files) != 0) goto error; } else Py_INCREF(files); Py_XSETREF(self->files, files); /* Transfer reference */ Py_XSETREF(self->archive, filename); filename = NULL; /* Check if there is a prefix directory following the filename. */ if (flen != len) { tmp = PyUnicode_Substring(path, flen+1, PyUnicode_GET_LENGTH(path)); if (tmp == NULL) goto error; Py_XSETREF(self->prefix, tmp); if (PyUnicode_READ_CHAR(path, len-1) != SEP) { /* add trailing SEP */ tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP); if (tmp == NULL) goto error; Py_SETREF(self->prefix, tmp); } } else { Py_XSETREF(self->prefix, PyUnicode_New(0, 0)); } Py_DECREF(path); return 0; error: Py_DECREF(path); Py_XDECREF(filename); return -1; }