static int join_append(WriterObj *self, PyObject *field, int quoted) { unsigned int field_kind = -1; void *field_data = NULL; Py_ssize_t field_len = 0; Py_ssize_t rec_len; if (field != NULL) { if (PyUnicode_READY(field) == -1) return 0; field_kind = PyUnicode_KIND(field); field_data = PyUnicode_DATA(field); field_len = PyUnicode_GET_LENGTH(field); } rec_len = join_append_data(self, field_kind, field_data, field_len, "ed, 0); if (rec_len < 0) return 0; /* grow record buffer if necessary */ if (!join_check_rec_size(self, rec_len)) return 0; self->rec_len = join_append_data(self, field_kind, field_data, field_len, "ed, 1); self->num_fields++; return 1; }
static PyObject * csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs) { PyObject *name_obj, *dialect_obj = NULL; PyObject *dialect; if (!PyArg_UnpackTuple(args, "", 1, 2, &name_obj, &dialect_obj)) return NULL; if (!PyUnicode_Check(name_obj)) { PyErr_SetString(PyExc_TypeError, "dialect name must be a string"); return NULL; } if (PyUnicode_READY(name_obj) == -1) return NULL; dialect = _call_dialect(dialect_obj, kwargs); if (dialect == NULL) return NULL; if (PyDict_SetItem(_csvstate_global->dialects, name_obj, dialect) < 0) { Py_DECREF(dialect); return NULL; } Py_DECREF(dialect); Py_RETURN_NONE; }
/* Given a path to a .pyc file in the archive, return the modification time of the matching .py file, or 0 if no source is available. */ static time_t get_mtime_of_source(ZipImporter *self, PyObject *path) { PyObject *toc_entry, *stripped; time_t mtime; /* strip 'c' or 'o' from *.py[co] */ if (PyUnicode_READY(path) == -1) return (time_t)-1; stripped = PyUnicode_FromKindAndData(PyUnicode_KIND(path), PyUnicode_DATA(path), PyUnicode_GET_LENGTH(path) - 1); if (stripped == NULL) return (time_t)-1; toc_entry = PyDict_GetItem(self->files, stripped); Py_DECREF(stripped); if (toc_entry != NULL && PyTuple_Check(toc_entry) && PyTuple_Size(toc_entry) == 8) { /* fetch the time stamp of the .py file for comparison with an embedded pyc time stamp */ int time, date; time = PyLong_AsLong(PyTuple_GetItem(toc_entry, 5)); date = PyLong_AsLong(PyTuple_GetItem(toc_entry, 6)); mtime = parse_dostime(time, date); } else mtime = 0; return mtime; }
/* return fullname.split(".")[-1] */ static PyObject * get_subname(PyObject *fullname) { Py_ssize_t len, dot; if (PyUnicode_READY(fullname) < 0) return NULL; len = PyUnicode_GET_LENGTH(fullname); dot = PyUnicode_FindChar(fullname, '.', 0, len, -1); if (dot == -1) { Py_INCREF(fullname); return fullname; } else return PyUnicode_Substring(fullname, dot+1, len); }
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; }
/* Intern selected string constants */ static int intern_string_constants(PyObject *tuple) { int modified = 0; Py_ssize_t i; for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { PyObject *v = PyTuple_GET_ITEM(tuple, i); if (PyUnicode_CheckExact(v)) { if (PyUnicode_READY(v) == -1) { PyErr_Clear(); continue; } if (all_name_chars(v)) { PyObject *w = v; PyUnicode_InternInPlace(&v); if (w != v) { PyTuple_SET_ITEM(tuple, i, v); modified = 1; } } } else if (PyTuple_CheckExact(v)) { intern_string_constants(v); } else if (PyFrozenSet_CheckExact(v)) { PyObject *w = v; PyObject *tmp = PySequence_Tuple(v); if (tmp == NULL) { PyErr_Clear(); continue; } if (intern_string_constants(tmp)) { v = PyFrozenSet_New(tmp); if (v == NULL) { PyErr_Clear(); } else { PyTuple_SET_ITEM(tuple, i, v); Py_DECREF(w); modified = 1; } } Py_DECREF(tmp); } } return modified; }
static int _set_str(const char *name, PyObject **target, PyObject *src, const char *dflt) { if (src == NULL) *target = PyUnicode_DecodeASCII(dflt, strlen(dflt), NULL); else { if (src == Py_None) *target = NULL; else if (!PyUnicode_Check(src)) { PyErr_Format(PyExc_TypeError, "\"%s\" must be a string", name); return -1; } else { if (PyUnicode_READY(src) == -1) return -1; Py_INCREF(src); Py_XSETREF(*target, src); } } return 0; }
/* Verify that the identifier follows PEP 3131. All identifier strings are guaranteed to be "ready" unicode objects. */ static int verify_identifier(struct tok_state *tok) { PyObject *s; int result; if (tok->decoding_erred) return 0; s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL); if (s == NULL || PyUnicode_READY(s) == -1) { if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { PyErr_Clear(); tok->done = E_IDENTIFIER; } else { tok->done = E_ERROR; } return 0; } result = PyUnicode_IsIdentifier(s); Py_DECREF(s); if (result == 0) tok->done = E_IDENTIFIER; return result; }
static int all_name_chars(PyObject *o) { static char ok_name_char[256]; static unsigned char *name_chars = (unsigned char *)NAME_CHARS; PyUnicodeObject *u = (PyUnicodeObject *)o; const unsigned char *s; if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 || PyUnicode_MAX_CHAR_VALUE(u) >= 128) return 0; if (ok_name_char[*name_chars] == 0) { unsigned char *p; for (p = name_chars; *p; p++) ok_name_char[*p] = 1; } s = PyUnicode_1BYTE_DATA(u); while (*s) { if (ok_name_char[*s++] == 0) return 0; } return 1; }
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; }
/* Fill in the digit parts of a numbers's string representation, as determined in calc_number_widths(). Return -1 on error, or 0 on success. */ static int fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec, PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end, PyObject *prefix, Py_ssize_t p_start, Py_UCS4 fill_char, LocaleInfo *locale, int toupper) { /* Used to keep track of digits, decimal, and remainder. */ Py_ssize_t d_pos = d_start; const unsigned int kind = writer->kind; const void *data = writer->data; Py_ssize_t r; if (spec->n_lpadding) { _PyUnicode_FastFill(writer->buffer, writer->pos, spec->n_lpadding, fill_char); writer->pos += spec->n_lpadding; } if (spec->n_sign == 1) { PyUnicode_WRITE(kind, data, writer->pos, spec->sign); writer->pos++; } if (spec->n_prefix) { _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, prefix, p_start, spec->n_prefix); if (toupper) { Py_ssize_t t; for (t = 0; t < spec->n_prefix; t++) { Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t); c = Py_TOUPPER(c); assert (c <= 127); PyUnicode_WRITE(kind, data, writer->pos + t, c); } } writer->pos += spec->n_prefix; } if (spec->n_spadding) { _PyUnicode_FastFill(writer->buffer, writer->pos, spec->n_spadding, fill_char); writer->pos += spec->n_spadding; } /* Only for type 'c' special case, it has no digits. */ if (spec->n_digits != 0) { /* Fill the digits with InsertThousandsGrouping. */ char *pdigits; if (PyUnicode_READY(digits)) return -1; pdigits = PyUnicode_DATA(digits); if (PyUnicode_KIND(digits) < kind) { pdigits = _PyUnicode_AsKind(digits, kind); if (pdigits == NULL) return -1; } r = _PyUnicode_InsertThousandsGrouping( writer->buffer, writer->pos, spec->n_grouped_digits, pdigits + kind * d_pos, spec->n_digits, spec->n_min_width, locale->grouping, locale->thousands_sep, NULL); if (r == -1) return -1; assert(r == spec->n_grouped_digits); if (PyUnicode_KIND(digits) < kind) PyMem_Free(pdigits); d_pos += spec->n_digits; } if (toupper) { Py_ssize_t t; for (t = 0; t < spec->n_grouped_digits; t++) { Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t); c = Py_TOUPPER(c); if (c > 127) { PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit"); return -1; } PyUnicode_WRITE(kind, data, writer->pos + t, c); } } writer->pos += spec->n_grouped_digits; if (spec->n_decimal) { _PyUnicode_FastCopyCharacters( writer->buffer, writer->pos, locale->decimal_point, 0, spec->n_decimal); writer->pos += spec->n_decimal; d_pos += 1; } if (spec->n_remainder) { _PyUnicode_FastCopyCharacters( writer->buffer, writer->pos, digits, d_pos, spec->n_remainder); writer->pos += spec->n_remainder; /* d_pos += spec->n_remainder; */ } if (spec->n_rpadding) { _PyUnicode_FastFill(writer->buffer, writer->pos, spec->n_rpadding, fill_char); writer->pos += spec->n_rpadding; } return 0; }
PyCodeObject * PyCode_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab) { PyCodeObject *co; unsigned char *cell2arg = NULL; Py_ssize_t i, n_cellvars; /* Check argument types */ if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 || code == NULL || consts == NULL || !PyTuple_Check(consts) || names == NULL || !PyTuple_Check(names) || varnames == NULL || !PyTuple_Check(varnames) || freevars == NULL || !PyTuple_Check(freevars) || cellvars == NULL || !PyTuple_Check(cellvars) || name == NULL || !PyUnicode_Check(name) || filename == NULL || !PyUnicode_Check(filename) || lnotab == NULL || !PyBytes_Check(lnotab) || !PyObject_CheckReadBuffer(code)) { PyErr_BadInternalCall(); return NULL; } /* Ensure that the filename is a ready Unicode string */ if (PyUnicode_READY(filename) < 0) return NULL; n_cellvars = PyTuple_GET_SIZE(cellvars); intern_strings(names); intern_strings(varnames); intern_strings(freevars); intern_strings(cellvars); /* Intern selected string constants */ for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) { PyObject *v = PyTuple_GetItem(consts, i); if (!all_name_chars(v)) continue; PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i)); } /* Create mapping between cells and arguments if needed. */ if (n_cellvars) { Py_ssize_t total_args = argcount + kwonlyargcount + ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars; int used_cell2arg = 0; cell2arg = PyMem_MALLOC(alloc_size); if (cell2arg == NULL) return NULL; memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size); /* Find cells which are also arguments. */ for (i = 0; i < n_cellvars; i++) { Py_ssize_t j; PyObject *cell = PyTuple_GET_ITEM(cellvars, i); for (j = 0; j < total_args; j++) { PyObject *arg = PyTuple_GET_ITEM(varnames, j); if (!PyUnicode_Compare(cell, arg)) { cell2arg[i] = j; used_cell2arg = 1; break; } } } if (!used_cell2arg) { PyMem_FREE(cell2arg); cell2arg = NULL; } } co = PyObject_NEW(PyCodeObject, &PyCode_Type); if (co == NULL) { if (cell2arg) PyMem_FREE(cell2arg); return NULL; } co->co_argcount = argcount; co->co_kwonlyargcount = kwonlyargcount; co->co_nlocals = nlocals; co->co_stacksize = stacksize; co->co_flags = flags; Py_INCREF(code); co->co_code = code; Py_INCREF(consts); co->co_consts = consts; Py_INCREF(names); co->co_names = names; Py_INCREF(varnames); co->co_varnames = varnames; Py_INCREF(freevars); co->co_freevars = freevars; Py_INCREF(cellvars); co->co_cellvars = cellvars; co->co_cell2arg = cell2arg; Py_INCREF(filename); co->co_filename = filename; Py_INCREF(name); co->co_name = name; co->co_firstlineno = firstlineno; Py_INCREF(lnotab); co->co_lnotab = lnotab; co->co_zombieframe = NULL; co->co_weakreflist = NULL; return co; }
static PyObject * zipimport_zipimporter_load_module_impl(ZipImporter *self, PyObject *fullname) /*[clinic end generated code: output=7303cebf88d47953 input=c236e2e8621f04ef]*/ { PyObject *code = NULL, *mod, *dict; PyObject *modpath = NULL; int ispackage; if (PyUnicode_READY(fullname) == -1) return NULL; code = get_module_code(self, fullname, &ispackage, &modpath); if (code == NULL) goto error; mod = PyImport_AddModuleObject(fullname); if (mod == NULL) goto error; dict = PyModule_GetDict(mod); /* mod.__loader__ = self */ if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) goto error; if (ispackage) { /* add __path__ to the module *before* the code gets executed */ PyObject *pkgpath, *fullpath, *subname; int err; subname = get_subname(fullname); if (subname == NULL) goto error; fullpath = PyUnicode_FromFormat("%U%c%U%U", self->archive, SEP, self->prefix, subname); Py_DECREF(subname); if (fullpath == NULL) goto error; pkgpath = Py_BuildValue("[N]", fullpath); if (pkgpath == NULL) goto error; err = PyDict_SetItemString(dict, "__path__", pkgpath); Py_DECREF(pkgpath); if (err != 0) goto error; } mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL); Py_CLEAR(code); if (mod == NULL) goto error; if (Py_VerboseFlag) PySys_FormatStderr("import %U # loaded from Zip %U\n", fullname, modpath); Py_DECREF(modpath); return mod; error: Py_XDECREF(code); Py_XDECREF(modpath); return NULL; }
static PyObject * Reader_iternext(ReaderObj *self) { PyObject *fields = NULL; Py_UCS4 c; Py_ssize_t pos, linelen; unsigned int kind; void *data; PyObject *lineobj; if (parse_reset(self) < 0) return NULL; do { lineobj = PyIter_Next(self->input_iter); if (lineobj == NULL) { /* End of input OR exception */ if (!PyErr_Occurred() && (self->field_len != 0 || self->state == IN_QUOTED_FIELD)) { if (self->dialect->strict) PyErr_SetString(_csvstate_global->error_obj, "unexpected end of data"); else if (parse_save_field(self) >= 0) break; } return NULL; } if (!PyUnicode_Check(lineobj)) { PyErr_Format(_csvstate_global->error_obj, "iterator should return strings, " "not %.200s " "(did you open the file in text mode?)", lineobj->ob_type->tp_name ); Py_DECREF(lineobj); return NULL; } if (PyUnicode_READY(lineobj) == -1) { Py_DECREF(lineobj); return NULL; } ++self->line_num; kind = PyUnicode_KIND(lineobj); data = PyUnicode_DATA(lineobj); pos = 0; linelen = PyUnicode_GET_LENGTH(lineobj); while (linelen--) { c = PyUnicode_READ(kind, data, pos); if (c == '\0') { Py_DECREF(lineobj); PyErr_Format(_csvstate_global->error_obj, "line contains NULL byte"); goto err; } if (parse_process_char(self, c) < 0) { Py_DECREF(lineobj); goto err; } pos++; } Py_DECREF(lineobj); if (parse_process_char(self, 0) < 0) goto err; } while (self->state != START_RECORD); fields = self->fields; self->fields = NULL; err: return fields; }
/* 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 PyObject * parse(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject *string; if (!PyArg_ParseTuple(args, "U", &string)) return NULL; if (PyUnicode_READY(string) == -1) return NULL; Py_ssize_t string_length = PyUnicode_GET_LENGTH(string); int string_kind = PyUnicode_KIND(string); void *string_data = PyUnicode_DATA(string); Py_UCS4 c; int i; Symbol token = {.kind = UNDECIDED, .data=NULL}; int escape = 0; for (i = 0; i < string_length; i++) { c = PyUnicode_READ(string_kind, string_data, i); switch (c) { case 0x22: // " if (escape) { } else { } break; case 0x27: // ' if (escape) { } else { } break; case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: switch (token.kind) { case UNDECIDED: token.kind = INTEGER; token.data = c - 0x30; break; case INTEGER: token.data += (c - 0x30); break; } case 0x5c: // backslash if (escape) { printf("todo literal backslash\n"); escape = 0; } else { escape = 1; } break; CASE__WHITE_SPACE: switch (token.kind) { case INTEGER: case IDENTIFIER: printf("whitespace new token\n"); break; } break; CASE__XID_START: switch (token.kind) { case UNDECIDED: token.kind = IDENTIFIER; break; case INTEGER: printf("todo\n"); break; } break; CASE__XID_CONTINUE__EXCLUDING__XID_START: switch (token.kind) { case UNDECIDED: printf("continue start?\n"); break; case INTEGER: printf("continue integer\n"); break; } break; default: printf("unclassified"); } } PyErr_SetString(PyExc_NotImplementedError, "parse"); return NULL; }
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; }
/* Load and return the module named by 'fullname'. */ static PyObject * zipimporter_load_module(PyObject *obj, PyObject *args) { ZipImporter *self = (ZipImporter *)obj; PyObject *code = NULL, *mod, *dict; PyObject *fullname; PyObject *modpath = NULL; int ispackage; if (!PyArg_ParseTuple(args, "U:zipimporter.load_module", &fullname)) return NULL; if (PyUnicode_READY(fullname) == -1) return NULL; code = get_module_code(self, fullname, &ispackage, &modpath); if (code == NULL) goto error; mod = PyImport_AddModuleObject(fullname); if (mod == NULL) goto error; dict = PyModule_GetDict(mod); /* mod.__loader__ = self */ if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) goto error; if (ispackage) { /* add __path__ to the module *before* the code gets executed */ PyObject *pkgpath, *fullpath, *subname; int err; subname = get_subname(fullname); if (subname == NULL) goto error; fullpath = PyUnicode_FromFormat("%U%c%U%U", self->archive, SEP, self->prefix, subname); Py_DECREF(subname); if (fullpath == NULL) goto error; pkgpath = Py_BuildValue("[N]", fullpath); if (pkgpath == NULL) goto error; err = PyDict_SetItemString(dict, "__path__", pkgpath); Py_DECREF(pkgpath); if (err != 0) goto error; } mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL); Py_CLEAR(code); if (mod == NULL) goto error; if (Py_VerboseFlag) PySys_FormatStderr("import %U # loaded from Zip %U\n", fullname, modpath); Py_DECREF(modpath); return mod; error: Py_XDECREF(code); Py_XDECREF(modpath); return NULL; }
PyCodeObject * PyCode_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab) { PyCodeObject *co; unsigned char *cell2arg = NULL; Py_ssize_t i, n_cellvars; // We originally had a Py_GUARD here, and all was well. It was never hit // by normal parallel contexts. Then, after misusing datrie, it was // suddenly being hit -- turns out Cython calls PyCode_New() when an // exception occurs in order to do stuff. I've since perused the body and // nothing immediately stands out as being unsafe for calling in a // parallel context (i.e. no random mallocs or static storage), so, let's // remove the guard and see what happens. //Py_GUARD(); /* Check argument types */ if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 || code == NULL || consts == NULL || !PyTuple_Check(consts) || names == NULL || !PyTuple_Check(names) || varnames == NULL || !PyTuple_Check(varnames) || freevars == NULL || !PyTuple_Check(freevars) || cellvars == NULL || !PyTuple_Check(cellvars) || name == NULL || !PyUnicode_Check(name) || filename == NULL || !PyUnicode_Check(filename) || lnotab == NULL || !PyBytes_Check(lnotab) || !PyObject_CheckReadBuffer(code)) { PyErr_BadInternalCall(); return NULL; } /* Ensure that the filename is a ready Unicode string */ if (PyUnicode_READY(filename) < 0) return NULL; n_cellvars = PyTuple_GET_SIZE(cellvars); intern_strings(names); intern_strings(varnames); intern_strings(freevars); intern_strings(cellvars); /* Intern selected string constants */ for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) { PyObject *v = PyTuple_GetItem(consts, i); if (!all_name_chars(v)) continue; PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i)); } /* Create mapping between cells and arguments if needed. */ if (n_cellvars) { Py_ssize_t total_args = argcount + kwonlyargcount + ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars; int used_cell2arg = 0; cell2arg = PyMem_MALLOC(alloc_size); if (cell2arg == NULL) return NULL; memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size); /* Find cells which are also arguments. */ for (i = 0; i < n_cellvars; i++) { Py_ssize_t j; PyObject *cell = PyTuple_GET_ITEM(cellvars, i); for (j = 0; j < total_args; j++) { PyObject *arg = PyTuple_GET_ITEM(varnames, j); if (!PyUnicode_Compare(cell, arg)) { cell2arg[i] = j; used_cell2arg = 1; break; } } } if (!used_cell2arg) { PyMem_FREE(cell2arg); cell2arg = NULL; } } co = PyObject_NEW(PyCodeObject, &PyCode_Type); if (co == NULL) { if (cell2arg) PyMem_FREE(cell2arg); return NULL; } co->co_argcount = argcount; co->co_kwonlyargcount = kwonlyargcount; co->co_nlocals = nlocals; co->co_stacksize = stacksize; co->co_flags = flags; Py_INCREF(code); co->co_code = code; Py_INCREF(consts); co->co_consts = consts; Py_INCREF(names); co->co_names = names; Py_INCREF(varnames); co->co_varnames = varnames; Py_INCREF(freevars); co->co_freevars = freevars; Py_INCREF(cellvars); co->co_cellvars = cellvars; co->co_cell2arg = cell2arg; Py_INCREF(filename); co->co_filename = filename; Py_INCREF(name); co->co_name = name; co->co_firstlineno = firstlineno; Py_INCREF(lnotab); co->co_lnotab = lnotab; co->co_zombieframe = NULL; co->co_weakreflist = NULL; return co; }
PyCodeObject * PyCode_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab) { PyCodeObject *co; Py_ssize_t *cell2arg = NULL; Py_ssize_t i, n_cellvars, n_varnames, total_args; /* Check argument types */ if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 || code == NULL || !PyBytes_Check(code) || consts == NULL || !PyTuple_Check(consts) || names == NULL || !PyTuple_Check(names) || varnames == NULL || !PyTuple_Check(varnames) || freevars == NULL || !PyTuple_Check(freevars) || cellvars == NULL || !PyTuple_Check(cellvars) || name == NULL || !PyUnicode_Check(name) || filename == NULL || !PyUnicode_Check(filename) || lnotab == NULL || !PyBytes_Check(lnotab)) { PyErr_BadInternalCall(); return NULL; } /* Ensure that the filename is a ready Unicode string */ if (PyUnicode_READY(filename) < 0) return NULL; intern_strings(names); intern_strings(varnames); intern_strings(freevars); intern_strings(cellvars); intern_string_constants(consts); /* Check for any inner or outer closure references */ n_cellvars = PyTuple_GET_SIZE(cellvars); if (!n_cellvars && !PyTuple_GET_SIZE(freevars)) { flags |= CO_NOFREE; } else { flags &= ~CO_NOFREE; } n_varnames = PyTuple_GET_SIZE(varnames); if (argcount <= n_varnames && kwonlyargcount <= n_varnames) { /* Never overflows. */ total_args = (Py_ssize_t)argcount + (Py_ssize_t)kwonlyargcount + ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); } else { total_args = n_varnames + 1; } if (total_args > n_varnames) { PyErr_SetString(PyExc_ValueError, "code: varnames is too small"); return NULL; } /* Create mapping between cells and arguments if needed. */ if (n_cellvars) { bool used_cell2arg = false; cell2arg = PyMem_NEW(Py_ssize_t, n_cellvars); if (cell2arg == NULL) { PyErr_NoMemory(); return NULL; } /* Find cells which are also arguments. */ for (i = 0; i < n_cellvars; i++) { Py_ssize_t j; PyObject *cell = PyTuple_GET_ITEM(cellvars, i); cell2arg[i] = CO_CELL_NOT_AN_ARG; for (j = 0; j < total_args; j++) { PyObject *arg = PyTuple_GET_ITEM(varnames, j); int cmp = PyUnicode_Compare(cell, arg); if (cmp == -1 && PyErr_Occurred()) { PyMem_FREE(cell2arg); return NULL; } if (cmp == 0) { cell2arg[i] = j; used_cell2arg = true; break; } } } if (!used_cell2arg) { PyMem_FREE(cell2arg); cell2arg = NULL; } } co = PyObject_NEW(PyCodeObject, &PyCode_Type); if (co == NULL) { if (cell2arg) PyMem_FREE(cell2arg); return NULL; } co->co_argcount = argcount; co->co_kwonlyargcount = kwonlyargcount; co->co_nlocals = nlocals; co->co_stacksize = stacksize; co->co_flags = flags; Py_INCREF(code); co->co_code = code; Py_INCREF(consts); co->co_consts = consts; Py_INCREF(names); co->co_names = names; Py_INCREF(varnames); co->co_varnames = varnames; Py_INCREF(freevars); co->co_freevars = freevars; Py_INCREF(cellvars); co->co_cellvars = cellvars; co->co_cell2arg = cell2arg; Py_INCREF(filename); co->co_filename = filename; Py_INCREF(name); co->co_name = name; co->co_firstlineno = firstlineno; Py_INCREF(lnotab); co->co_lnotab = lnotab; co->co_zombieframe = NULL; co->co_weakreflist = NULL; co->co_extra = NULL; return co; }
static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) { #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT if ( #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII __Pyx_sys_getdefaultencoding_not_ascii && #endif PyUnicode_Check(o)) { #if PY_VERSION_HEX < 0x03030000 char* defenc_c; // borrowed, cached reference PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL); if (!defenc) return NULL; defenc_c = PyBytes_AS_STRING(defenc); #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII { char* end = defenc_c + PyBytes_GET_SIZE(defenc); char* c; for (c = defenc_c; c < end; c++) { if ((unsigned char) (*c) >= 128) { // raise the error PyUnicode_AsASCIIString(o); return NULL; } } } #endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/ *length = PyBytes_GET_SIZE(defenc); return defenc_c; #else /* PY_VERSION_HEX < 0x03030000 */ if (PyUnicode_READY(o) == -1) return NULL; #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII if (PyUnicode_IS_ASCII(o)) { // cached for the lifetime of the object *length = PyUnicode_GET_DATA_SIZE(o); return PyUnicode_AsUTF8(o); } else { // raise the error PyUnicode_AsASCIIString(o); return NULL; } #else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */ return PyUnicode_AsUTF8AndSize(o, length); #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */ #endif /* PY_VERSION_HEX < 0x03030000 */ } else #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */ #if PY_VERSION_HEX >= 0x02060000 if (PyByteArray_Check(o)) { *length = PyByteArray_GET_SIZE(o); return PyByteArray_AS_STRING(o); } else #endif { char* result; int r = PyBytes_AsStringAndSize(o, &result, length); if (unlikely(r < 0)) { return NULL; } else { return result; } } }
static void show_warning(PyObject *filename, int lineno, PyObject *text, PyObject *category, PyObject *sourceline) { PyObject *f_stderr; PyObject *name; char lineno_str[128]; _Py_IDENTIFIER(__name__); PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); name = _PyObject_GetAttrId(category, &PyId___name__); if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */ goto error; f_stderr = _PySys_GetObjectId(&PyId_stderr); if (f_stderr == NULL) { fprintf(stderr, "lost sys.stderr\n"); goto error; } /* Print "filename:lineno: category: text\n" */ if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0) goto error; if (PyFile_WriteString(lineno_str, f_stderr) < 0) goto error; if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0) goto error; if (PyFile_WriteString(": ", f_stderr) < 0) goto error; if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0) goto error; if (PyFile_WriteString("\n", f_stderr) < 0) goto error; Py_CLEAR(name); /* Print " source_line\n" */ if (sourceline) { int kind; void *data; Py_ssize_t i, len; Py_UCS4 ch; PyObject *truncated; if (PyUnicode_READY(sourceline) < 1) goto error; kind = PyUnicode_KIND(sourceline); data = PyUnicode_DATA(sourceline); len = PyUnicode_GET_LENGTH(sourceline); for (i=0; i<len; i++) { ch = PyUnicode_READ(kind, data, i); if (ch != ' ' && ch != '\t' && ch != '\014') break; } truncated = PyUnicode_Substring(sourceline, i, len); if (truncated == NULL) goto error; PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW); Py_DECREF(truncated); PyFile_WriteString("\n", f_stderr); } else { _Py_DisplaySourceLine(f_stderr, filename, lineno, 2); } error: Py_XDECREF(name); PyErr_Clear(); }
/* read_directory(archive) -> files dict (new reference) Given a path to a Zip archive, build a dict, mapping file names (local to the archive, using SEP as a separator) to toc entries. A toc_entry is a tuple: (__file__, # value to use for __file__, available for all files, # encoded to the filesystem encoding compress, # compression kind; 0 for uncompressed data_size, # size of compressed data on disk file_size, # size of decompressed data file_offset, # offset of file header from start of archive time, # mod time of file (in dos format) date, # mod data of file (in dos format) crc, # crc checksum of the data ) Directories can be recognized by the trailing SEP in the name, data_size and file_offset are 0. */ static PyObject * read_directory(PyObject *archive) { PyObject *files = NULL; FILE *fp; unsigned short flags, compress, time, date, name_size; unsigned int crc, data_size, file_size, header_size, header_offset; unsigned long file_offset, header_position; unsigned long arc_offset; /* Absolute offset to start of the zip-archive. */ unsigned int count, i; unsigned char buffer[46]; char name[MAXPATHLEN + 5]; PyObject *nameobj = NULL; PyObject *path; const char *charset; int bootstrap; const char *errmsg = NULL; fp = _Py_fopen_obj(archive, "rb"); if (fp == NULL) { if (PyErr_ExceptionMatches(PyExc_OSError)) { _PyErr_FormatFromCause(ZipImportError, "can't open Zip file: %R", archive); } return NULL; } if (fseek(fp, -22, SEEK_END) == -1) { goto file_error; } header_position = (unsigned long)ftell(fp); if (header_position == (unsigned long)-1) { goto file_error; } assert(header_position <= (unsigned long)LONG_MAX); if (fread(buffer, 1, 22, fp) != 22) { goto file_error; } if (get_uint32(buffer) != 0x06054B50u) { /* Bad: End of Central Dir signature */ errmsg = "not a Zip file"; goto invalid_header; } header_size = get_uint32(buffer + 12); header_offset = get_uint32(buffer + 16); if (header_position < header_size) { errmsg = "bad central directory size"; goto invalid_header; } if (header_position < header_offset) { errmsg = "bad central directory offset"; goto invalid_header; } if (header_position - header_size < header_offset) { errmsg = "bad central directory size or offset"; goto invalid_header; } header_position -= header_size; arc_offset = header_position - header_offset; files = PyDict_New(); if (files == NULL) { goto error; } /* Start of Central Directory */ count = 0; if (fseek(fp, (long)header_position, 0) == -1) { goto file_error; } for (;;) { PyObject *t; size_t n; int err; n = fread(buffer, 1, 46, fp); if (n < 4) { goto eof_error; } /* Start of file header */ if (get_uint32(buffer) != 0x02014B50u) { break; /* Bad: Central Dir File Header */ } if (n != 46) { goto eof_error; } flags = get_uint16(buffer + 8); compress = get_uint16(buffer + 10); time = get_uint16(buffer + 12); date = get_uint16(buffer + 14); crc = get_uint32(buffer + 16); data_size = get_uint32(buffer + 20); file_size = get_uint32(buffer + 24); name_size = get_uint16(buffer + 28); header_size = (unsigned int)name_size + get_uint16(buffer + 30) /* extra field */ + get_uint16(buffer + 32) /* comment */; file_offset = get_uint32(buffer + 42); if (file_offset > header_offset) { errmsg = "bad local header offset"; goto invalid_header; } file_offset += arc_offset; if (name_size > MAXPATHLEN) { name_size = MAXPATHLEN; } if (fread(name, 1, name_size, fp) != name_size) { goto file_error; } name[name_size] = '\0'; /* Add terminating null byte */ #if SEP != '/' for (i = 0; i < name_size; i++) { if (name[i] == '/') { name[i] = SEP; } } #endif /* Skip the rest of the header. * On Windows, calling fseek to skip over the fields we don't use is * slower than reading the data because fseek flushes stdio's * internal buffers. See issue #8745. */ assert(header_size <= 3*0xFFFFu); for (i = name_size; i < header_size; i++) { if (getc(fp) == EOF) { goto file_error; } } bootstrap = 0; if (flags & 0x0800) { charset = "utf-8"; } else if (!PyThreadState_GET()->interp->codecs_initialized) { /* During bootstrap, we may need to load the encodings package from a ZIP file. But the cp437 encoding is implemented in Python in the encodings package. Break out of this dependency by assuming that the path to the encodings module is ASCII-only. */ charset = "ascii"; bootstrap = 1; } else { charset = "cp437"; } nameobj = PyUnicode_Decode(name, name_size, charset, NULL); if (nameobj == NULL) { if (bootstrap) { PyErr_Format(PyExc_NotImplementedError, "bootstrap issue: python%i%i.zip contains non-ASCII " "filenames without the unicode flag", PY_MAJOR_VERSION, PY_MINOR_VERSION); } goto error; } if (PyUnicode_READY(nameobj) == -1) { goto error; } path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj); if (path == NULL) { goto error; } t = Py_BuildValue("NHIIkHHI", path, compress, data_size, file_size, file_offset, time, date, crc); if (t == NULL) { goto error; } err = PyDict_SetItem(files, nameobj, t); Py_CLEAR(nameobj); Py_DECREF(t); if (err != 0) { goto error; } count++; } fclose(fp); if (Py_VerboseFlag) { PySys_FormatStderr("# zipimport: found %u names in %R\n", count, archive); } return files; eof_error: set_file_error(archive, !ferror(fp)); goto error; file_error: PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); goto error; invalid_header: assert(errmsg != NULL); PyErr_Format(ZipImportError, "%s: %R", errmsg, archive); goto error; error: fclose(fp); Py_XDECREF(files); Py_XDECREF(nameobj); return NULL; }
/* Returns 0 on error (no new refs), 1 on success */ static int setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, PyObject **module, PyObject **registry) { PyObject *globals; /* Setup globals and lineno. */ PyFrameObject *f = PyThreadState_GET()->frame; // Stack level comparisons to Python code is off by one as there is no // warnings-related stack level to avoid. if (stack_level <= 0 || is_internal_frame(f)) { while (--stack_level > 0 && f != NULL) { f = f->f_back; } } else { while (--stack_level > 0 && f != NULL) { f = next_external_frame(f); } } if (f == NULL) { globals = PyThreadState_Get()->interp->sysdict; *lineno = 1; } else { globals = f->f_globals; *lineno = PyFrame_GetLineNumber(f); } *module = NULL; /* Setup registry. */ assert(globals != NULL); assert(PyDict_Check(globals)); *registry = PyDict_GetItemString(globals, "__warningregistry__"); if (*registry == NULL) { int rc; *registry = PyDict_New(); if (*registry == NULL) return 0; rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); if (rc < 0) goto handle_error; } else Py_INCREF(*registry); /* Setup module. */ *module = PyDict_GetItemString(globals, "__name__"); if (*module == NULL) { *module = PyUnicode_FromString("<string>"); if (*module == NULL) goto handle_error; } else Py_INCREF(*module); /* Setup filename. */ *filename = PyDict_GetItemString(globals, "__file__"); if (*filename != NULL && PyUnicode_Check(*filename)) { Py_ssize_t len; int kind; void *data; if (PyUnicode_READY(*filename)) goto handle_error; len = PyUnicode_GetLength(*filename); kind = PyUnicode_KIND(*filename); data = PyUnicode_DATA(*filename); #define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0) /* if filename.lower().endswith(".pyc"): */ if (len >= 4 && PyUnicode_READ(kind, data, len-4) == '.' && ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' && ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' && ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c') { *filename = PyUnicode_Substring(*filename, 0, PyUnicode_GET_LENGTH(*filename)-1); if (*filename == NULL) goto handle_error; } else Py_INCREF(*filename); } else { *filename = NULL; if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) { PyObject *argv = _PySys_GetObjectId(&PyId_argv); /* PyList_Check() is needed because sys.argv is set to None during Python finalization */ if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) { int is_true; *filename = PyList_GetItem(argv, 0); Py_INCREF(*filename); /* If sys.argv[0] is false, then use '__main__'. */ is_true = PyObject_IsTrue(*filename); if (is_true < 0) { Py_DECREF(*filename); goto handle_error; } else if (!is_true) { Py_XSETREF(*filename, PyUnicode_FromString("__main__")); if (*filename == NULL) goto handle_error; } } else { /* embedded interpreters don't have sys.argv, see bug #839151 */ *filename = PyUnicode_FromString("__main__"); if (*filename == NULL) goto handle_error; } } if (*filename == NULL) { *filename = *module; Py_INCREF(*filename); } } return 1; handle_error: /* filename not XDECREF'ed here as there is no way to jump here with a dangling reference. */ Py_XDECREF(*registry); Py_XDECREF(*module); return 0; }
/* read_directory(archive) -> files dict (new reference) Given a path to a Zip archive, build a dict, mapping file names (local to the archive, using SEP as a separator) to toc entries. A toc_entry is a tuple: (__file__, # value to use for __file__, available for all files, # encoded to the filesystem encoding compress, # compression kind; 0 for uncompressed data_size, # size of compressed data on disk file_size, # size of decompressed data file_offset, # offset of file header from start of archive time, # mod time of file (in dos format) date, # mod data of file (in dos format) crc, # crc checksum of the data ) Directories can be recognized by the trailing SEP in the name, data_size and file_offset are 0. */ static PyObject * read_directory(PyObject *archive) { PyObject *files = NULL; FILE *fp; unsigned short flags; short compress, time, date, name_size; long crc, data_size, file_size, header_size; Py_ssize_t file_offset, header_position, header_offset; long l, count; Py_ssize_t i; char name[MAXPATHLEN + 5]; PyObject *nameobj = NULL; char *p, endof_central_dir[22]; Py_ssize_t arc_offset; /* Absolute offset to start of the zip-archive. */ PyObject *path; const char *charset; int bootstrap; fp = _Py_fopen(archive, "rb"); if (fp == NULL) { if (!PyErr_Occurred()) PyErr_Format(ZipImportError, "can't open Zip file: %R", archive); return NULL; } fseek(fp, -22, SEEK_END); header_position = ftell(fp); if (fread(endof_central_dir, 1, 22, fp) != 22) { fclose(fp); PyErr_Format(ZipImportError, "can't read Zip file: %R", archive); return NULL; } if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) { /* Bad: End of Central Dir signature */ fclose(fp); PyErr_Format(ZipImportError, "not a Zip file: %R", archive); return NULL; } header_size = get_long((unsigned char *)endof_central_dir + 12); header_offset = get_long((unsigned char *)endof_central_dir + 16); arc_offset = header_position - header_offset - header_size; header_offset += arc_offset; files = PyDict_New(); if (files == NULL) goto error; /* Start of Central Directory */ count = 0; for (;;) { PyObject *t; int err; fseek(fp, header_offset, 0); /* Start of file header */ l = PyMarshal_ReadLongFromFile(fp); if (l != 0x02014B50) break; /* Bad: Central Dir File Header */ fseek(fp, header_offset + 8, 0); flags = (unsigned short)PyMarshal_ReadShortFromFile(fp); compress = PyMarshal_ReadShortFromFile(fp); time = PyMarshal_ReadShortFromFile(fp); date = PyMarshal_ReadShortFromFile(fp); crc = PyMarshal_ReadLongFromFile(fp); data_size = PyMarshal_ReadLongFromFile(fp); file_size = PyMarshal_ReadLongFromFile(fp); name_size = PyMarshal_ReadShortFromFile(fp); header_size = 46 + name_size + PyMarshal_ReadShortFromFile(fp) + PyMarshal_ReadShortFromFile(fp); fseek(fp, header_offset + 42, 0); file_offset = PyMarshal_ReadLongFromFile(fp) + arc_offset; if (name_size > MAXPATHLEN) name_size = MAXPATHLEN; p = name; for (i = 0; i < (Py_ssize_t)name_size; i++) { *p = (char)getc(fp); if (*p == '/') *p = SEP; p++; } *p = 0; /* Add terminating null byte */ header_offset += header_size; bootstrap = 0; if (flags & 0x0800) charset = "utf-8"; else if (!PyThreadState_GET()->interp->codecs_initialized) { /* During bootstrap, we may need to load the encodings package from a ZIP file. But the cp437 encoding is implemented in Python in the encodings package. Break out of this dependency by assuming that the path to the encodings module is ASCII-only. */ charset = "ascii"; bootstrap = 1; } else charset = "cp437"; nameobj = PyUnicode_Decode(name, name_size, charset, NULL); if (PyUnicode_READY(nameobj) == -1) goto error; if (nameobj == NULL) { if (bootstrap) PyErr_Format(PyExc_NotImplementedError, "bootstrap issue: python%i%i.zip contains non-ASCII " "filenames without the unicode flag", PY_MAJOR_VERSION, PY_MINOR_VERSION); goto error; } path = PyUnicode_FromFormat("%U%c%U", archive, SEP, nameobj); if (path == NULL) goto error; t = Py_BuildValue("Nhllnhhl", path, compress, data_size, file_size, file_offset, time, date, crc); if (t == NULL) goto error; err = PyDict_SetItem(files, nameobj, t); Py_CLEAR(nameobj); Py_DECREF(t); if (err != 0) goto error; count++; } fclose(fp); if (Py_VerboseFlag) PySys_FormatStderr("# zipimport: found %ld names in %R\n", count, archive); return files; error: fclose(fp); Py_XDECREF(files); Py_XDECREF(nameobj); return NULL; }