mirror of
https://github.com/python/cpython.git
synced 2025-10-21 22:22:48 +00:00
Rip out the file object's implementation.
Fixed test_import.py while I was at it. However, there's still a problem in import.c -- get_file() can leak a FILE struct (not a file descriptor though). I'm not sure how to fix this; closing the FILE* closes the file descriptor, and that's the wrong thing to do when there's still a Python file object keeping the file descriptor open. I also would rather not mess with dup(), as it won't port to Windows.
This commit is contained in:
parent
2d5c219fe0
commit
da5b8f2d28
14 changed files with 106 additions and 2537 deletions
|
@ -669,66 +669,6 @@ PyTokenizer_Free(struct tok_state *tok)
|
|||
PyMem_FREE(tok);
|
||||
}
|
||||
|
||||
#if !defined(PGEN)
|
||||
static int
|
||||
tok_stdin_decode(struct tok_state *tok, char **inp)
|
||||
{
|
||||
PyObject *enc, *sysstdin, *decoded, *utf8;
|
||||
const char *encoding;
|
||||
char *converted;
|
||||
|
||||
if (PySys_GetFile((char *)"stdin", NULL) != stdin)
|
||||
return 0;
|
||||
sysstdin = PySys_GetObject("stdin");
|
||||
if (sysstdin == NULL || !PyFile_Check(sysstdin))
|
||||
return 0;
|
||||
|
||||
enc = ((PyFileObject *)sysstdin)->f_encoding;
|
||||
if (enc == NULL || !PyString_Check(enc))
|
||||
return 0;
|
||||
Py_INCREF(enc);
|
||||
|
||||
encoding = PyString_AsString(enc);
|
||||
decoded = PyUnicode_Decode(*inp, strlen(*inp), encoding, NULL);
|
||||
if (decoded == NULL)
|
||||
goto error_clear;
|
||||
|
||||
utf8 = PyUnicode_AsEncodedString(decoded, "utf-8", NULL);
|
||||
Py_DECREF(decoded);
|
||||
if (utf8 == NULL)
|
||||
goto error_clear;
|
||||
|
||||
assert(PyBytes_Check(utf8));
|
||||
converted = new_string(PyBytes_AS_STRING(utf8),
|
||||
PyBytes_GET_SIZE(utf8));
|
||||
Py_DECREF(utf8);
|
||||
if (converted == NULL)
|
||||
goto error_nomem;
|
||||
|
||||
PyMem_FREE(*inp);
|
||||
*inp = converted;
|
||||
if (tok->encoding != NULL)
|
||||
PyMem_FREE(tok->encoding);
|
||||
tok->encoding = new_string(encoding, strlen(encoding));
|
||||
if (tok->encoding == NULL)
|
||||
goto error_nomem;
|
||||
|
||||
Py_DECREF(enc);
|
||||
return 0;
|
||||
|
||||
error_nomem:
|
||||
Py_DECREF(enc);
|
||||
tok->done = E_NOMEM;
|
||||
return -1;
|
||||
|
||||
error_clear:
|
||||
/* Fallback to iso-8859-1: for backward compatibility */
|
||||
Py_DECREF(enc);
|
||||
PyErr_Clear();
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Get next char, updating state; error code goes into tok->done */
|
||||
|
||||
static int
|
||||
|
@ -768,10 +708,6 @@ tok_nextc(register struct tok_state *tok)
|
|||
PyMem_FREE(newtok);
|
||||
tok->done = E_EOF;
|
||||
}
|
||||
#if !defined(PGEN)
|
||||
else if (tok_stdin_decode(tok, &newtok) != 0)
|
||||
PyMem_FREE(newtok);
|
||||
#endif
|
||||
else if (tok->start != NULL) {
|
||||
size_t start = tok->start - tok->buf;
|
||||
size_t oldlen = tok->cur - tok->buf;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue