diff options
author | Brett Cannon <brett@python.org> | 2012-02-29 23:31:31 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-02-29 23:31:31 (GMT) |
commit | efb00c0cc189c1fdee329e8b7fdd07b3fd4a54cf (patch) | |
tree | 22dc683979e0dd42d049416797b756a5c6a053cd /Modules | |
parent | 54c32032aa28bdfead50714bf7861c98a9843597 (diff) | |
download | cpython-efb00c0cc189c1fdee329e8b7fdd07b3fd4a54cf.zip cpython-efb00c0cc189c1fdee329e8b7fdd07b3fd4a54cf.tar.gz cpython-efb00c0cc189c1fdee329e8b7fdd07b3fd4a54cf.tar.bz2 |
Issue #14153 Create _Py_device_encoding() to prevent _io from having to import
the os module.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_io/_iomodule.c | 14 | ||||
-rw-r--r-- | Modules/_io/_iomodule.h | 5 | ||||
-rw-r--r-- | Modules/_io/textio.c | 11 | ||||
-rw-r--r-- | Modules/posixmodule.c | 30 |
4 files changed, 15 insertions, 45 deletions
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index 9aad479..31eea3c 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -1,9 +1,9 @@ /* An implementation of the new I/O lib as defined by PEP 3116 - "New I/O" - + Classes defined here: UnsupportedOperation, BlockingIOError. Functions defined here: open(). - + Mostly written by Amaury Forgeot d'Arc */ @@ -510,7 +510,7 @@ PyNumber_AsOff_t(PyObject *item, PyObject *err) /* Basically the "n" format code with the ability to turn None into -1. */ -int +int _PyIO_ConvertSsize_t(PyObject *obj, void *result) { Py_ssize_t limit; if (obj == Py_None) { @@ -537,7 +537,6 @@ iomodule_traverse(PyObject *mod, visitproc visit, void *arg) { _PyIO_State *state = IO_MOD_STATE(mod); if (!state->initialized) return 0; - Py_VISIT(state->os_module); if (state->locale_module != NULL) { Py_VISIT(state->locale_module); } @@ -551,7 +550,6 @@ iomodule_clear(PyObject *mod) { _PyIO_State *state = IO_MOD_STATE(mod); if (!state->initialized) return 0; - Py_CLEAR(state->os_module); if (state->locale_module != NULL) Py_CLEAR(state->locale_module); Py_CLEAR(state->unsupported_operation); @@ -595,11 +593,6 @@ PyInit__io(void) state = IO_MOD_STATE(m); state->initialized = 0; - /* put os in the module state */ - state->os_module = PyImport_ImportModule("os"); - if (state->os_module == NULL) - goto fail; - #define ADD_TYPE(type, name) \ if (PyType_Ready(type) < 0) \ goto fail; \ @@ -725,7 +718,6 @@ PyInit__io(void) return m; fail: - Py_XDECREF(state->os_module); Py_XDECREF(state->unsupported_operation); Py_DECREF(m); return NULL; diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h index b3a8471..987aac8 100644 --- a/Modules/_io/_iomodule.h +++ b/Modules/_io/_iomodule.h @@ -50,8 +50,8 @@ extern PyObject *_PyIncrementalNewlineDecoder_decode( `*consumed`. If not found, returns -1 and sets `*consumed` to the number of characters which can be safely put aside until another search. - - NOTE: for performance reasons, `end` must point to a NUL character ('\0'). + + NOTE: for performance reasons, `end` must point to a NUL character ('\0'). Otherwise, the function will scan further and return garbage. */ extern Py_ssize_t _PyIO_find_line_ending( int translated, int universal, PyObject *readnl, @@ -124,7 +124,6 @@ extern PyModuleDef _PyIO_Module; typedef struct { int initialized; - PyObject *os_module; PyObject *locale_module; PyObject *unsupported_operation; diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 79c64ba..833a527 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -14,7 +14,6 @@ _Py_IDENTIFIER(close); _Py_IDENTIFIER(_dealloc_warn); _Py_IDENTIFIER(decode); -_Py_IDENTIFIER(device_encoding); _Py_IDENTIFIER(fileno); _Py_IDENTIFIER(flush); _Py_IDENTIFIER(getpreferredencoding); @@ -875,9 +874,13 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds) } } else { - self->encoding = _PyObject_CallMethodId(state->os_module, - &PyId_device_encoding, - "N", fileno); + int fd = (int) PyLong_AsLong(fileno); + Py_DECREF(fileno); + if (fd == -1 && PyErr_Occurred()) { + goto error; + } + + self->encoding = _Py_device_encoding(fd); if (self->encoding == NULL) goto error; else if (!PyUnicode_Check(self->encoding)) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index dbace1a..628b0b9 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -9326,35 +9326,11 @@ static PyObject * device_encoding(PyObject *self, PyObject *args) { int fd; -#if defined(MS_WINDOWS) || defined(MS_WIN64) - UINT cp; -#endif + if (!PyArg_ParseTuple(args, "i:device_encoding", &fd)) return NULL; - if (!_PyVerify_fd(fd) || !isatty(fd)) { - Py_INCREF(Py_None); - return Py_None; - } -#if defined(MS_WINDOWS) || defined(MS_WIN64) - if (fd == 0) - cp = GetConsoleCP(); - else if (fd == 1 || fd == 2) - cp = GetConsoleOutputCP(); - else - cp = 0; - /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application - has no console */ - if (cp != 0) - return PyUnicode_FromFormat("cp%u", (unsigned int)cp); -#elif defined(CODESET) - { - char *codeset = nl_langinfo(CODESET); - if (codeset != NULL && codeset[0] != 0) - return PyUnicode_FromString(codeset); - } -#endif - Py_INCREF(Py_None); - return Py_None; + + return _Py_device_encoding(fd); } #ifdef __VMS |