| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7f2fee3 commit fe7c5b5
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,6 +58,12 @@ def test_find_module_encoding(self): | |||
| 58 | 58 | with imp.find_module('module_' + mod, self.test_path)[0] as fd: | |
| 59 | 59 | self.assertEqual(fd.encoding, encoding) | |
| 60 | 60 | ||
| 61 | + path = [os.path.dirname(__file__)] | ||
| 62 | + self.assertRaisesRegex(SyntaxError, | ||
| 63 | + r"Non-UTF-8 code starting with '\\xf6'" | ||
| 64 | + r" in file .*badsyntax_pep3120.py", | ||
| 65 | + imp.find_module, 'badsyntax_pep3120', path) | ||
| 66 | + | ||
| 61 | 67 | def test_issue1267(self): | |
| 62 | 68 | for mod, encoding, _ in self.test_strings: | |
| 63 | 69 | fp, filename, info = imp.find_module('module_' + mod, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,8 @@ What's New in Python 3.3 Alpha 1? | |||
| 10 | 10 | Core and Builtins | |
| 11 | 11 | ----------------- | |
| 12 | 12 | ||
| 13 | + - Issue #9319: Include the filename in "Non-UTF8 code ..." syntax error. | ||
| 14 | + | ||
| 13 | 15 | - Issue #10785: Store the filename as Unicode in the Python parser. | |
| 14 | 16 | ||
| 15 | 17 | - Issue #11619: _PyImport_LoadDynamicModule() doesn't encode the path to bytes | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1690,17 +1690,18 @@ PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end) | |||
| 1690 | 1690 | return result; | |
| 1691 | 1691 | } | |
| 1692 | 1692 | ||
| 1693 | - /* Get -*- encoding -*- from a Python file. | ||
| 1693 | + /* Get the encoding of a Python file. Check for the coding cookie and check if | ||
| 1694 | + the file starts with a BOM. | ||
| 1694 | 1695 | ||
| 1695 | - PyTokenizer_FindEncoding returns NULL when it can't find the encoding in | ||
| 1696 | - the first or second line of the file (in which case the encoding | ||
| 1697 | - should be assumed to be PyUnicode_GetDefaultEncoding()). | ||
| 1696 | + PyTokenizer_FindEncodingFilename() returns NULL when it can't find the | ||
| 1697 | + encoding in the first or second line of the file (in which case the encoding | ||
| 1698 | + should be assumed to be UTF-8). | ||
| 1699 | + | ||
| 1700 | + The char* returned is malloc'ed via PyMem_MALLOC() and thus must be freed | ||
| 1701 | + by the caller. */ | ||
| 1698 | 1702 | ||
| 1699 | - The char * returned is malloc'ed via PyMem_MALLOC() and thus must be freed | ||
| 1700 | - by the caller. | ||
| 1701 | - */ | ||
| 1702 | 1703 | char * | |
| 1703 | - PyTokenizer_FindEncoding(int fd) | ||
| 1704 | + PyTokenizer_FindEncodingFilename(int fd, PyObject *filename) | ||
| 1704 | 1705 | { | |
| 1705 | 1706 | struct tok_state *tok; | |
| 1706 | 1707 | FILE *fp; | |
@@ -1720,9 +1721,18 @@ PyTokenizer_FindEncoding(int fd) | |||
| 1720 | 1721 | return NULL; | |
| 1721 | 1722 | } | |
| 1722 | 1723 | #ifndef PGEN | |
| 1723 | - tok->filename = PyUnicode_FromString("<string>"); | ||
| 1724 | - if (tok->filename == NULL) | ||
| 1725 | - goto error; | ||
| 1724 | + if (filename != NULL) { | ||
| 1725 | + Py_INCREF(filename); | ||
| 1726 | + tok->filename = filename; | ||
| 1727 | + } | ||
| 1728 | + else { | ||
| 1729 | + tok->filename = PyUnicode_FromString("<string>"); | ||
| 1730 | + if (tok->filename == NULL) { | ||
| 1731 | + fclose(fp); | ||
| 1732 | + PyTokenizer_Free(tok); | ||
| 1733 | + return encoding; | ||
| 1734 | + } | ||
| 1735 | + } | ||
| 1726 | 1736 | #endif | |
| 1727 | 1737 | while (tok->lineno < 2 && tok->done == E_OK) { | |
| 1728 | 1738 | PyTokenizer_Get(tok, &p_start, &p_end); | |
@@ -1733,13 +1743,16 @@ PyTokenizer_FindEncoding(int fd) | |||
| 1733 | 1743 | if (encoding) | |
| 1734 | 1744 | strcpy(encoding, tok->encoding); | |
| 1735 | 1745 | } | |
| 1736 | - #ifndef PGEN | ||
| 1737 | - error: | ||
| 1738 | - #endif | ||
| 1739 | 1746 | PyTokenizer_Free(tok); | |
| 1740 | 1747 | return encoding; | |
| 1741 | 1748 | } | |
| 1742 | 1749 | ||
| 1750 | + char * | ||
| 1751 | + PyTokenizer_FindEncoding(int fd) | ||
| 1752 | + { | ||
| 1753 | + return PyTokenizer_FindEncodingFilename(fd, NULL); | ||
| 1754 | + } | ||
| 1755 | + | ||
| 1743 | 1756 | #ifdef Py_DEBUG | |
| 1744 | 1757 | ||
| 1745 | 1758 | void | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -75,7 +75,6 @@ extern void PyTokenizer_Free(struct tok_state *); | |||
| 75 | 75 | extern int PyTokenizer_Get(struct tok_state *, char **, char **); | |
| 76 | 76 | extern char * PyTokenizer_RestoreEncoding(struct tok_state* tok, | |
| 77 | 77 | int len, int *offset); | |
| 78 | - extern char * PyTokenizer_FindEncoding(int); | ||
| 79 | 78 | ||
| 80 | 79 | #ifdef __cplusplus | |
| 81 | 80 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -124,12 +124,12 @@ static const Py_UNICODE PYC_TAG_UNICODE[] = { | |||
| 124 | 124 | /* See _PyImport_FixupExtensionObject() below */ | |
| 125 | 125 | static PyObject *extensions = NULL; | |
| 126 | 126 | ||
| 127 | + /* Function from Parser/tokenizer.c */ | ||
| 128 | + extern char * PyTokenizer_FindEncodingFilename(int, PyObject *); | ||
| 129 | + | ||
| 127 | 130 | /* This table is defined in config.c: */ | |
| 128 | 131 | extern struct _inittab _PyImport_Inittab[]; | |
| 129 | 132 | ||
| 130 | - /* Method from Parser/tokenizer.c */ | ||
| 131 | - extern char * PyTokenizer_FindEncoding(int); | ||
| 132 | - | ||
| 133 | 133 | struct _inittab *PyImport_Inittab = _PyImport_Inittab; | |
| 134 | 134 | ||
| 135 | 135 | /* these tables define the module suffixes that Python recognizes */ | |
@@ -3540,9 +3540,9 @@ call_find_module(PyObject *name, PyObject *path_list) | |||
| 3540 | 3540 | } | |
| 3541 | 3541 | if (fd != -1) { | |
| 3542 | 3542 | if (strchr(fdp->mode, 'b') == NULL) { | |
| 3543 | - /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed | ||
| 3543 | + /* PyTokenizer_FindEncodingFilename() returns PyMem_MALLOC'ed | ||
| 3544 | 3544 | memory. */ | |
| 3545 | - found_encoding = PyTokenizer_FindEncoding(fd); | ||
| 3545 | + found_encoding = PyTokenizer_FindEncodingFilename(fd, pathobj); | ||
| 3546 | 3546 | lseek(fd, 0, 0); /* Reset position */ | |
| 3547 | 3547 | if (found_encoding == NULL && PyErr_Occurred()) { | |
| 3548 | 3548 | Py_XDECREF(pathobj); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,8 +18,8 @@ | |||
| 18 | 18 | #define MAX_FRAME_DEPTH 100 | |
| 19 | 19 | #define MAX_NTHREADS 100 | |
| 20 | 20 | ||
| 21 | - /* Method from Parser/tokenizer.c */ | ||
| 22 | - extern char * PyTokenizer_FindEncoding(int); | ||
| 21 | + /* Function from Parser/tokenizer.c */ | ||
| 22 | + extern char * PyTokenizer_FindEncodingFilename(int, PyObject *); | ||
| 23 | 23 | ||
| 24 | 24 | static PyObject * | |
| 25 | 25 | tb_dir(PyTracebackObject *self) | |
@@ -251,7 +251,7 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent) | |||
| 251 | 251 | ||
| 252 | 252 | /* use the right encoding to decode the file as unicode */ | |
| 253 | 253 | fd = PyObject_AsFileDescriptor(binary); | |
| 254 | - found_encoding = PyTokenizer_FindEncoding(fd); | ||
| 254 | + found_encoding = PyTokenizer_FindEncodingFilename(fd, filename); | ||
| 255 | 255 | encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; | |
| 256 | 256 | lseek(fd, 0, 0); /* Reset position */ | |
| 257 | 257 | fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments