bpo-25711: Rewrite zipimport in pure Python. (GH-6809)

This commit is contained in:
Serhiy Storchaka 2018-09-18 22:22:29 +03:00 • committed by GitHub
parent 4ba3b50bfe
commit 79d1c2e6c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 2998 additions and 3408 deletions

View file

@ -27,28 +27,30 @@ static const struct _frozen _PyImport_FrozenModules[] = {
const struct _frozen *PyImport_FrozenModules;
#endif
const char header[] = "/* Auto-generated by Programs/_freeze_importlib.c */";
static const char header[] =
"/* Auto-generated by Programs/_freeze_importlib.c */";
int
main(int argc, char *argv[])
{
char *inpath, *outpath, *code_name;
const char *name, *inpath, *outpath;
char buf[100];
FILE *infile = NULL, *outfile = NULL;
struct _Py_stat_struct status;
size_t text_size, data_size, n;
size_t text_size, data_size, i, n;
char *text = NULL;
unsigned char *data;
PyObject *code = NULL, *marshalled = NULL;
int is_bootstrap = 1;
PyImport_FrozenModules = _PyImport_FrozenModules;
if (argc != 3) {
fprintf(stderr, "need to specify input and output paths\n");
if (argc != 4) {
fprintf(stderr, "need to specify the name, input and output paths\n");
return 2;
}
inpath = argv[1];
outpath = argv[2];
name = argv[1];
inpath = argv[2];
outpath = argv[3];
infile = fopen(inpath, "rb");
if (infile == NULL) {
fprintf(stderr, "cannot open '%s' for reading\n", inpath);
@ -90,14 +92,8 @@ main(int argc, char *argv[])
_Py_FatalInitError(err);
}
if (strstr(inpath, "_external") != NULL) {
is_bootstrap = 0;
}
code_name = is_bootstrap ?
"<frozen importlib._bootstrap>" :
"<frozen importlib._bootstrap_external>";
code = Py_CompileStringExFlags(text, code_name, Py_file_input, NULL, 0);
sprintf(buf, "<frozen %s>", name);
code = Py_CompileStringExFlags(text, buf, Py_file_input, NULL, 0);
if (code == NULL)
goto error;
free(text);
@ -120,11 +116,13 @@ main(int argc, char *argv[])
goto error;
}
fprintf(outfile, "%s\n", header);
if (is_bootstrap)
fprintf(outfile, "const unsigned char _Py_M__importlib[] = {\n");
else
fprintf(outfile,
"const unsigned char _Py_M__importlib_external[] = {\n");
for (i = n = 0; name[i] != '\0'; i++) {
if (name[i] != '.') {
buf[n++] = name[i];
}
}
buf[n] = '\0';
fprintf(outfile, "const unsigned char _Py_M__%s[] = {\n", buf);
for (n = 0; n < data_size; n += 16) {
size_t i, end = Py_MIN(n + 16, data_size);
fprintf(outfile, " ");