| OLD | NEW |
| 1 | 1 |
| 2 /* Tokenizer implementation */ | 2 /* Tokenizer implementation */ |
| 3 | 3 |
| 4 #include "Python.h" | 4 #include "Python.h" |
| 5 #include "pgenheaders.h" | 5 #include "pgenheaders.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <assert.h> | 8 #include <assert.h> |
| 9 | 9 |
| 10 #include "tokenizer.h" | 10 #include "tokenizer.h" |
| (...skipping 1659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1670 PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end) | 1670 PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end) |
| 1671 { | 1671 { |
| 1672 int result = tok_get(tok, p_start, p_end); | 1672 int result = tok_get(tok, p_start, p_end); |
| 1673 if (tok->decoding_erred) { | 1673 if (tok->decoding_erred) { |
| 1674 result = ERRORTOKEN; | 1674 result = ERRORTOKEN; |
| 1675 tok->done = E_DECODE; | 1675 tok->done = E_DECODE; |
| 1676 } | 1676 } |
| 1677 return result; | 1677 return result; |
| 1678 } | 1678 } |
| 1679 | 1679 |
| 1680 /* Get -*- encoding -*- from a Python file. | |
| 1681 | |
| 1682 PyTokenizer_FindEncoding returns NULL when it can't find the encoding in | |
| 1683 the first or second line of the file (in which case the encoding | |
| 1684 should be assumed to be PyUnicode_GetDefaultEncoding()). | |
| 1685 | |
| 1686 The char * returned is malloc'ed via PyMem_MALLOC() and thus must be freed | |
| 1687 by the caller. | |
| 1688 */ | |
| 1689 char * | 1680 char * |
| 1690 PyTokenizer_FindEncoding(int fd) | 1681 PyTokenizer_FindEncodingFilename(int fd, const char *filename) |
| 1691 { | 1682 { |
| 1692 struct tok_state *tok; | 1683 struct tok_state *tok; |
| 1693 FILE *fp; | 1684 FILE *fp; |
| 1694 char *p_start =NULL , *p_end =NULL , *encoding = NULL; | 1685 char *p_start =NULL , *p_end =NULL , *encoding = NULL; |
| 1695 | 1686 |
| 1696 fd = dup(fd); | 1687 fd = dup(fd); |
| 1697 if (fd < 0) { | 1688 if (fd < 0) { |
| 1698 return NULL; | 1689 return NULL; |
| 1699 } | 1690 } |
| 1700 fp = fdopen(fd, "r"); | 1691 fp = fdopen(fd, "r"); |
| 1701 if (fp == NULL) { | 1692 if (fp == NULL) { |
| 1702 return NULL; | 1693 return NULL; |
| 1703 } | 1694 } |
| 1704 tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL); | 1695 tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL); |
| 1705 if (tok == NULL) { | 1696 if (tok == NULL) { |
| 1706 fclose(fp); | 1697 fclose(fp); |
| 1707 return NULL; | 1698 return NULL; |
| 1708 } | 1699 } |
| 1700 if (filename) |
| 1701 tok->filename = filename; |
| 1702 else |
| 1703 tok->filename = "<string>"; |
| 1709 while (tok->lineno < 2 && tok->done == E_OK) { | 1704 while (tok->lineno < 2 && tok->done == E_OK) { |
| 1710 PyTokenizer_Get(tok, &p_start, &p_end); | 1705 PyTokenizer_Get(tok, &p_start, &p_end); |
| 1711 } | 1706 } |
| 1712 fclose(fp); | 1707 fclose(fp); |
| 1713 if (tok->encoding) { | 1708 if (tok->encoding) { |
| 1714 encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1); | 1709 encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1); |
| 1715 if (encoding) | 1710 if (encoding) |
| 1716 strcpy(encoding, tok->encoding); | 1711 strcpy(encoding, tok->encoding); |
| 1717 } | 1712 } |
| 1718 PyTokenizer_Free(tok); | 1713 PyTokenizer_Free(tok); |
| 1719 return encoding; | 1714 return encoding; |
| 1720 } | 1715 } |
| 1721 | 1716 |
| 1717 char * |
| 1718 PyTokenizer_FindEncoding(int fd) |
| 1719 { |
| 1720 return PyTokenizer_FindEncodingFilename(fd, NULL); |
| 1721 } |
| 1722 |
| 1722 #ifdef Py_DEBUG | 1723 #ifdef Py_DEBUG |
| 1723 | 1724 |
| 1724 void | 1725 void |
| 1725 tok_dump(int type, char *start, char *end) | 1726 tok_dump(int type, char *start, char *end) |
| 1726 { | 1727 { |
| 1727 printf("%s", _PyParser_TokenNames[type]); | 1728 printf("%s", _PyParser_TokenNames[type]); |
| 1728 if (type == NAME || type == NUMBER || type == STRING || type == OP) | 1729 if (type == NAME || type == NUMBER || type == STRING || type == OP) |
| 1729 printf("(%.*s)", (int)(end - start), start); | 1730 printf("(%.*s)", (int)(end - start), start); |
| 1730 } | 1731 } |
| 1731 | 1732 |
| 1732 #endif | 1733 #endif |
| OLD | NEW |