mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	SF patch# 1766592 by Paul Colomiets.
Fix test_zipimport.
This commit is contained in:
		
							parent
							
								
									6afaeb757a
								
							
						
					
					
						commit
						ad8d30092c
					
				
					 4 changed files with 31 additions and 25 deletions
				
			
		|  | @ -209,7 +209,7 @@ def _load_testfile(filename, package, module_relative): | ||||||
|         filename = _module_relative_path(package, filename) |         filename = _module_relative_path(package, filename) | ||||||
|         if hasattr(package, '__loader__'): |         if hasattr(package, '__loader__'): | ||||||
|             if hasattr(package.__loader__, 'get_data'): |             if hasattr(package.__loader__, 'get_data'): | ||||||
|                 return package.__loader__.get_data(filename), filename |                 return package.__loader__.get_data(filename).decode('utf-8'), filename | ||||||
|     return open(filename, encoding="utf-8").read(), filename |     return open(filename, encoding="utf-8").read(), filename | ||||||
| 
 | 
 | ||||||
| def _indent(s, indent=4): | def _indent(s, indent=4): | ||||||
|  |  | ||||||
|  | @ -153,18 +153,16 @@ def testEmptyPy(self): | ||||||
| 
 | 
 | ||||||
|     def testBadMagic(self): |     def testBadMagic(self): | ||||||
|         # make pyc magic word invalid, forcing loading from .py |         # make pyc magic word invalid, forcing loading from .py | ||||||
|         m0 = ord(test_pyc[0]) |         badmagic_pyc = bytes(test_pyc) | ||||||
|         m0 ^= 0x04  # flip an arbitrary bit |         badmagic_pyc[0] ^= 0x04  # flip an arbitrary bit | ||||||
|         badmagic_pyc = chr(m0) + test_pyc[1:] |  | ||||||
|         files = {TESTMOD + ".py": (NOW, test_src), |         files = {TESTMOD + ".py": (NOW, test_src), | ||||||
|                  TESTMOD + pyc_ext: (NOW, badmagic_pyc)} |                  TESTMOD + pyc_ext: (NOW, badmagic_pyc)} | ||||||
|         self.doTest(".py", files, TESTMOD) |         self.doTest(".py", files, TESTMOD) | ||||||
| 
 | 
 | ||||||
|     def testBadMagic2(self): |     def testBadMagic2(self): | ||||||
|         # make pyc magic word invalid, causing an ImportError |         # make pyc magic word invalid, causing an ImportError | ||||||
|         m0 = ord(test_pyc[0]) |         badmagic_pyc = bytes(test_pyc) | ||||||
|         m0 ^= 0x04  # flip an arbitrary bit |         badmagic_pyc[0] ^= 0x04  # flip an arbitrary bit | ||||||
|         badmagic_pyc = chr(m0) + test_pyc[1:] |  | ||||||
|         files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)} |         files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)} | ||||||
|         try: |         try: | ||||||
|             self.doTest(".py", files, TESTMOD) |             self.doTest(".py", files, TESTMOD) | ||||||
|  | @ -174,10 +172,9 @@ def testBadMagic2(self): | ||||||
|             self.fail("expected ImportError; import from bad pyc") |             self.fail("expected ImportError; import from bad pyc") | ||||||
| 
 | 
 | ||||||
|     def testBadMTime(self): |     def testBadMTime(self): | ||||||
|         t3 = ord(test_pyc[7]) |         badtime_pyc = bytes(test_pyc) | ||||||
|         t3 ^= 0x02  # flip the second bit -- not the first as that one |         badtime_pyc[7] ^= 0x02  # flip the second bit -- not the first as that one | ||||||
|                     # isn't stored in the .py's mtime in the zip archive. |                                 # isn't stored in the .py's mtime in the zip archive. | ||||||
|         badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:] |  | ||||||
|         files = {TESTMOD + ".py": (NOW, test_src), |         files = {TESTMOD + ".py": (NOW, test_src), | ||||||
|                  TESTMOD + pyc_ext: (NOW, badtime_pyc)} |                  TESTMOD + pyc_ext: (NOW, badtime_pyc)} | ||||||
|         self.doTest(".py", files, TESTMOD) |         self.doTest(".py", files, TESTMOD) | ||||||
|  | @ -232,7 +229,7 @@ def testGetData(self): | ||||||
|         z.compression = self.compression |         z.compression = self.compression | ||||||
|         try: |         try: | ||||||
|             name = "testdata.dat" |             name = "testdata.dat" | ||||||
|             data = "".join([chr(x) for x in range(256)]) * 500 |             data = bytes(x for x in range(256)) | ||||||
|             z.writestr(name, data) |             z.writestr(name, data) | ||||||
|             z.close() |             z.close() | ||||||
|             zi = zipimport.zipimporter(TEMP_ZIP) |             zi = zipimport.zipimporter(TEMP_ZIP) | ||||||
|  | @ -246,7 +243,7 @@ def testImporterAttr(self): | ||||||
|         src = """if 1:  # indent hack |         src = """if 1:  # indent hack | ||||||
|         def get_file(): |         def get_file(): | ||||||
|             return __file__ |             return __file__ | ||||||
|         if __loader__.get_data("some.data") != "some data": |         if __loader__.get_data("some.data") != b"some data": | ||||||
|             raise AssertionError, "bad data"\n""" |             raise AssertionError, "bad data"\n""" | ||||||
|         pyc = make_pyc(compile(src, "<???>", "exec"), NOW) |         pyc = make_pyc(compile(src, "<???>", "exec"), NOW) | ||||||
|         files = {TESTMOD + pyc_ext: (NOW, pyc), |         files = {TESTMOD + pyc_ext: (NOW, pyc), | ||||||
|  |  | ||||||
|  | @ -475,8 +475,12 @@ zipimporter_get_source(PyObject *obj, PyObject *args) | ||||||
| 		strcpy(path + len, ".py"); | 		strcpy(path + len, ".py"); | ||||||
| 
 | 
 | ||||||
| 	toc_entry = PyDict_GetItemString(self->files, path); | 	toc_entry = PyDict_GetItemString(self->files, path); | ||||||
| 	if (toc_entry != NULL) | 	if (toc_entry != NULL) { | ||||||
| 		return get_data(PyString_AsString(self->archive), toc_entry); | 		PyObject *bytes = get_data(PyString_AsString(self->archive), toc_entry); | ||||||
|  | 		PyObject *res = PyUnicode_FromString(PyBytes_AsString(bytes)); | ||||||
|  | 		Py_XDECREF(bytes); | ||||||
|  | 		return res; | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	/* we have the module, but no source */ | 	/* we have the module, but no source */ | ||||||
| 	Py_INCREF(Py_None); | 	Py_INCREF(Py_None); | ||||||
|  | @ -857,8 +861,10 @@ get_data(char *archive, PyObject *toc_entry) | ||||||
| 	} | 	} | ||||||
| 	buf[data_size] = '\0'; | 	buf[data_size] = '\0'; | ||||||
| 
 | 
 | ||||||
| 	if (compress == 0)  /* data is not compressed */ | 	if (compress == 0) {  /* data is not compressed */ | ||||||
|  | 		raw_data = PyBytes_FromStringAndSize(buf, data_size); | ||||||
| 		return raw_data; | 		return raw_data; | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	/* Decompress with zlib */ | 	/* Decompress with zlib */ | ||||||
| 	decompress = get_decompress_func(); | 	decompress = get_decompress_func(); | ||||||
|  | @ -896,8 +902,8 @@ static PyObject * | ||||||
| unmarshal_code(char *pathname, PyObject *data, time_t mtime) | unmarshal_code(char *pathname, PyObject *data, time_t mtime) | ||||||
| { | { | ||||||
| 	PyObject *code; | 	PyObject *code; | ||||||
| 	char *buf = PyString_AsString(data); | 	char *buf = PyBytes_AsString(data); | ||||||
| 	Py_ssize_t size = PyString_Size(data); | 	Py_ssize_t size = PyBytes_Size(data); | ||||||
| 
 | 
 | ||||||
| 	if (size <= 9) { | 	if (size <= 9) { | ||||||
| 		PyErr_SetString(ZipImportError, | 		PyErr_SetString(ZipImportError, | ||||||
|  | @ -942,14 +948,16 @@ unmarshal_code(char *pathname, PyObject *data, time_t mtime) | ||||||
| static PyObject * | static PyObject * | ||||||
| normalize_line_endings(PyObject *source) | normalize_line_endings(PyObject *source) | ||||||
| { | { | ||||||
| 	char *buf, *q, *p = PyString_AsString(source); | 	char *buf, *q, *p = PyBytes_AsString(source); | ||||||
| 	PyObject *fixed_source; | 	PyObject *fixed_source; | ||||||
|  | 	int len = 0; | ||||||
| 
 | 
 | ||||||
| 	if (!p) | 	if (!p) { | ||||||
| 		return NULL; | 		return PyBytes_FromStringAndSize("\n\0", 2); | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	/* one char extra for trailing \n and one for terminating \0 */ | 	/* one char extra for trailing \n and one for terminating \0 */ | ||||||
| 	buf = (char *)PyMem_Malloc(PyString_Size(source) + 2); | 	buf = (char *)PyMem_Malloc(PyBytes_Size(source) + 2); | ||||||
| 	if (buf == NULL) { | 	if (buf == NULL) { | ||||||
| 		PyErr_SetString(PyExc_MemoryError, | 		PyErr_SetString(PyExc_MemoryError, | ||||||
| 				"zipimport: no memory to allocate " | 				"zipimport: no memory to allocate " | ||||||
|  | @ -965,10 +973,11 @@ normalize_line_endings(PyObject *source) | ||||||
| 		} | 		} | ||||||
| 		else | 		else | ||||||
| 			*q++ = *p; | 			*q++ = *p; | ||||||
|  | 		len++; | ||||||
| 	} | 	} | ||||||
| 	*q++ = '\n';  /* add trailing \n */ | 	*q++ = '\n';  /* add trailing \n */ | ||||||
| 	*q = '\0'; | 	*q = '\0'; | ||||||
| 	fixed_source = PyString_FromString(buf); | 	fixed_source = PyBytes_FromStringAndSize(buf, len + 2); | ||||||
| 	PyMem_Free(buf); | 	PyMem_Free(buf); | ||||||
| 	return fixed_source; | 	return fixed_source; | ||||||
| } | } | ||||||
|  | @ -984,7 +993,7 @@ compile_source(char *pathname, PyObject *source) | ||||||
| 	if (fixed_source == NULL) | 	if (fixed_source == NULL) | ||||||
| 		return NULL; | 		return NULL; | ||||||
| 
 | 
 | ||||||
| 	code = Py_CompileString(PyString_AsString(fixed_source), pathname, | 	code = Py_CompileString(PyBytes_AsString(fixed_source), pathname, | ||||||
| 				Py_file_input); | 				Py_file_input); | ||||||
| 	Py_DECREF(fixed_source); | 	Py_DECREF(fixed_source); | ||||||
| 	return code; | 	return code; | ||||||
|  |  | ||||||
|  | @ -2620,7 +2620,7 @@ imp_get_magic(PyObject *self, PyObject *noargs) | ||||||
| 	buf[2] = (char) ((pyc_magic >> 16) & 0xff); | 	buf[2] = (char) ((pyc_magic >> 16) & 0xff); | ||||||
| 	buf[3] = (char) ((pyc_magic >> 24) & 0xff); | 	buf[3] = (char) ((pyc_magic >> 24) & 0xff); | ||||||
| 
 | 
 | ||||||
| 	return PyString_FromStringAndSize(buf, 4); | 	return PyBytes_FromStringAndSize(buf, 4); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static PyObject * | static PyObject * | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Guido van Rossum
						Guido van Rossum