Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad8d300

Browse files
committed
SF patch# 1766592 by Paul Colomiets.
Fix test_zipimport.
1 parent 6afaeb7 commit ad8d300

4 files changed

Lines changed: 31 additions & 25 deletions

File tree

Lib/doctest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def _load_testfile(filename, package, module_relative):
209209
filename = _module_relative_path(package, filename)
210210
if hasattr(package, '__loader__'):
211211
if hasattr(package.__loader__, 'get_data'):
212-
return package.__loader__.get_data(filename), filename
212+
return package.__loader__.get_data(filename).decode('utf-8'), filename
213213
return open(filename, encoding="utf-8").read(), filename
214214

215215
def _indent(s, indent=4):

Lib/test/test_zipimport.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,16 @@ def testEmptyPy(self):
153153

154154
def testBadMagic(self):
155155
# make pyc magic word invalid, forcing loading from .py
156-
m0 = ord(test_pyc[0])
157-
m0 ^= 0x04 # flip an arbitrary bit
158-
badmagic_pyc = chr(m0) + test_pyc[1:]
156+
badmagic_pyc = bytes(test_pyc)
157+
badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
159158
files = {TESTMOD + ".py": (NOW, test_src),
160159
TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
161160
self.doTest(".py", files, TESTMOD)
162161

163162
def testBadMagic2(self):
164163
# make pyc magic word invalid, causing an ImportError
165-
m0 = ord(test_pyc[0])
166-
m0 ^= 0x04 # flip an arbitrary bit
167-
badmagic_pyc = chr(m0) + test_pyc[1:]
164+
badmagic_pyc = bytes(test_pyc)
165+
badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
168166
files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
169167
try:
170168
self.doTest(".py", files, TESTMOD)
@@ -174,10 +172,9 @@ def testBadMagic2(self):
174172
self.fail("expected ImportError; import from bad pyc")
175173

176174
def testBadMTime(self):
177-
t3 = ord(test_pyc[7])
178-
t3 ^= 0x02 # flip the second bit -- not the first as that one
179-
# isn't stored in the .py's mtime in the zip archive.
180-
badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:]
175+
badtime_pyc = bytes(test_pyc)
176+
badtime_pyc[7] ^= 0x02 # flip the second bit -- not the first as that one
177+
# isn't stored in the .py's mtime in the zip archive.
181178
files = {TESTMOD + ".py": (NOW, test_src),
182179
TESTMOD + pyc_ext: (NOW, badtime_pyc)}
183180
self.doTest(".py", files, TESTMOD)
@@ -232,7 +229,7 @@ def testGetData(self):
232229
z.compression = self.compression
233230
try:
234231
name = "testdata.dat"
235-
data = "".join([chr(x) for x in range(256)]) * 500
232+
data = bytes(x for x in range(256))
236233
z.writestr(name, data)
237234
z.close()
238235
zi = zipimport.zipimporter(TEMP_ZIP)
@@ -246,7 +243,7 @@ def testImporterAttr(self):
246243
src = """if 1: # indent hack
247244
def get_file():
248245
return __file__
249-
if __loader__.get_data("some.data") != "some data":
246+
if __loader__.get_data("some.data") != b"some data":
250247
raise AssertionError, "bad data"\n"""
251248
pyc = make_pyc(compile(src, "<???>", "exec"), NOW)
252249
files = {TESTMOD + pyc_ext: (NOW, pyc),

Modules/zipimport.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,12 @@ zipimporter_get_source(PyObject *obj, PyObject *args)
475475
strcpy(path + len, ".py");
476476

477477
toc_entry = PyDict_GetItemString(self->files, path);
478-
if (toc_entry != NULL)
479-
return get_data(PyString_AsString(self->archive), toc_entry);
478+
if (toc_entry != NULL) {
479+
PyObject *bytes = get_data(PyString_AsString(self->archive), toc_entry);
480+
PyObject *res = PyUnicode_FromString(PyBytes_AsString(bytes));
481+
Py_XDECREF(bytes);
482+
return res;
483+
}
480484

481485
/* we have the module, but no source */
482486
Py_INCREF(Py_None);
@@ -857,8 +861,10 @@ get_data(char *archive, PyObject *toc_entry)
857861
}
858862
buf[data_size] = '\0';
859863

860-
if (compress == 0) /* data is not compressed */
864+
if (compress == 0) { /* data is not compressed */
865+
raw_data = PyBytes_FromStringAndSize(buf, data_size);
861866
return raw_data;
867+
}
862868

863869
/* Decompress with zlib */
864870
decompress = get_decompress_func();
@@ -896,8 +902,8 @@ static PyObject *
896902
unmarshal_code(char *pathname, PyObject *data, time_t mtime)
897903
{
898904
PyObject *code;
899-
char *buf = PyString_AsString(data);
900-
Py_ssize_t size = PyString_Size(data);
905+
char *buf = PyBytes_AsString(data);
906+
Py_ssize_t size = PyBytes_Size(data);
901907

902908
if (size <= 9) {
903909
PyErr_SetString(ZipImportError,
@@ -942,14 +948,16 @@ unmarshal_code(char *pathname, PyObject *data, time_t mtime)
942948
static PyObject *
943949
normalize_line_endings(PyObject *source)
944950
{
945-
char *buf, *q, *p = PyString_AsString(source);
951+
char *buf, *q, *p = PyBytes_AsString(source);
946952
PyObject *fixed_source;
953+
int len = 0;
947954

948-
if (!p)
949-
return NULL;
955+
if (!p) {
956+
return PyBytes_FromStringAndSize("\n\0", 2);
957+
}
950958

951959
/* one char extra for trailing \n and one for terminating \0 */
952-
buf = (char *)PyMem_Malloc(PyString_Size(source) + 2);
960+
buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2);
953961
if (buf == NULL) {
954962
PyErr_SetString(PyExc_MemoryError,
955963
"zipimport: no memory to allocate "
@@ -965,10 +973,11 @@ normalize_line_endings(PyObject *source)
965973
}
966974
else
967975
*q++ = *p;
976+
len++;
968977
}
969978
*q++ = '\n'; /* add trailing \n */
970979
*q = '\0';
971-
fixed_source = PyString_FromString(buf);
980+
fixed_source = PyBytes_FromStringAndSize(buf, len + 2);
972981
PyMem_Free(buf);
973982
return fixed_source;
974983
}
@@ -984,7 +993,7 @@ compile_source(char *pathname, PyObject *source)
984993
if (fixed_source == NULL)
985994
return NULL;
986995

987-
code = Py_CompileString(PyString_AsString(fixed_source), pathname,
996+
code = Py_CompileString(PyBytes_AsString(fixed_source), pathname,
988997
Py_file_input);
989998
Py_DECREF(fixed_source);
990999
return code;

Python/import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2620,7 +2620,7 @@ imp_get_magic(PyObject *self, PyObject *noargs)
26202620
buf[2] = (char) ((pyc_magic >> 16) & 0xff);
26212621
buf[3] = (char) ((pyc_magic >> 24) & 0xff);
26222622

2623-
return PyString_FromStringAndSize(buf, 4);
2623+
return PyBytes_FromStringAndSize(buf, 4);
26242624
}
26252625

26262626
static PyObject *

0 commit comments

Comments
 (0)