Skip to content

Commit efb00c0

Browse files
committed
Issue python#14153 Create _Py_device_encoding() to prevent _io from having to import
the os module.
1 parent 54c3203 commit efb00c0

7 files changed

Lines changed: 70 additions & 45 deletions

File tree

‎Include/fileutils.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
extern "C" {
66
#endif
77

8+
PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
9+
810
PyAPI_FUNC(wchar_t *) _Py_char2wchar(
911
const char *arg,
1012
size_t *size);

‎Lib/test/test_os.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import socket
2323
import itertools
2424
import stat
25+
import locale
26+
import codecs
2527
try:
2628
import threading
2729
except ImportError:
@@ -1424,6 +1426,22 @@ def test_identity(self):
14241426
self.assertEqual(os.fsdecode(bytesfn), fn)
14251427

14261428

1429+
1430+
class DeviceEncodingTests(unittest.TestCase):
1431+
1432+
def test_bad_fd(self):
1433+
# Return None when an fd doesn't actually exist.
1434+
self.assertIsNone(os.device_encoding(123456))
1435+
1436+
@unittest.skipUnless(sys.platform.startswith('win') or
1437+
(hasattr(locale, 'nl_langinfo') and hasattr(locale, 'CODESET')),
1438+
'test requires either Windows or nl_langinfo(CODESET)')
1439+
def test_device_encoding(self):
1440+
encoding = os.device_encoding(0)
1441+
self.assertIsNotNone(encoding)
1442+
self.assertTrue(codecs.lookup(encoding))
1443+
1444+
14271445
class PidTests(unittest.TestCase):
14281446
@unittest.skipUnless(hasattr(os, 'getppid'), "test needs os.getppid")
14291447
def test_getppid(self):
@@ -1923,6 +1941,7 @@ def test_main():
19231941
Win32KillTests,
19241942
Win32SymlinkTests,
19251943
FSEncodingTests,
1944+
DeviceEncodingTests,
19261945
PidTests,
19271946
LoginTests,
19281947
LinkTests,

‎Modules/_io/_iomodule.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
An implementation of the new I/O lib as defined by PEP 3116 - "New I/O"
3-
3+
44
Classes defined here: UnsupportedOperation, BlockingIOError.
55
Functions defined here: open().
6-
6+
77
Mostly written by Amaury Forgeot d'Arc
88
*/
99

@@ -510,7 +510,7 @@ PyNumber_AsOff_t(PyObject *item, PyObject *err)
510510

511511

512512
/* Basically the "n" format code with the ability to turn None into -1. */
513-
int
513+
int
514514
_PyIO_ConvertSsize_t(PyObject *obj, void *result) {
515515
Py_ssize_t limit;
516516
if (obj == Py_None) {
@@ -537,7 +537,6 @@ iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
537537
_PyIO_State *state = IO_MOD_STATE(mod);
538538
if (!state->initialized)
539539
return 0;
540-
Py_VISIT(state->os_module);
541540
if (state->locale_module != NULL) {
542541
Py_VISIT(state->locale_module);
543542
}
@@ -551,7 +550,6 @@ iomodule_clear(PyObject *mod) {
551550
_PyIO_State *state = IO_MOD_STATE(mod);
552551
if (!state->initialized)
553552
return 0;
554-
Py_CLEAR(state->os_module);
555553
if (state->locale_module != NULL)
556554
Py_CLEAR(state->locale_module);
557555
Py_CLEAR(state->unsupported_operation);
@@ -595,11 +593,6 @@ PyInit__io(void)
595593
state = IO_MOD_STATE(m);
596594
state->initialized = 0;
597595

598-
/* put os in the module state */
599-
state->os_module = PyImport_ImportModule("os");
600-
if (state->os_module == NULL)
601-
goto fail;
602-
603596
#define ADD_TYPE(type, name) \
604597
if (PyType_Ready(type) < 0) \
605598
goto fail; \
@@ -725,7 +718,6 @@ PyInit__io(void)
725718
return m;
726719

727720
fail:
728-
Py_XDECREF(state->os_module);
729721
Py_XDECREF(state->unsupported_operation);
730722
Py_DECREF(m);
731723
return NULL;

‎Modules/_io/_iomodule.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ extern PyObject *_PyIncrementalNewlineDecoder_decode(
5050
`*consumed`.
5151
If not found, returns -1 and sets `*consumed` to the number of characters
5252
which can be safely put aside until another search.
53-
54-
NOTE: for performance reasons, `end` must point to a NUL character ('\0').
53+
54+
NOTE: for performance reasons, `end` must point to a NUL character ('\0').
5555
Otherwise, the function will scan further and return garbage. */
5656
extern Py_ssize_t _PyIO_find_line_ending(
5757
int translated, int universal, PyObject *readnl,
@@ -124,7 +124,6 @@ extern PyModuleDef _PyIO_Module;
124124

125125
typedef struct {
126126
int initialized;
127-
PyObject *os_module;
128127
PyObject *locale_module;
129128

130129
PyObject *unsupported_operation;

‎Modules/_io/textio.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
_Py_IDENTIFIER(close);
1515
_Py_IDENTIFIER(_dealloc_warn);
1616
_Py_IDENTIFIER(decode);
17-
_Py_IDENTIFIER(device_encoding);
1817
_Py_IDENTIFIER(fileno);
1918
_Py_IDENTIFIER(flush);
2019
_Py_IDENTIFIER(getpreferredencoding);
@@ -875,9 +874,13 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
875874
}
876875
}
877876
else {
878-
self->encoding = _PyObject_CallMethodId(state->os_module,
879-
&PyId_device_encoding,
880-
"N", fileno);
877+
int fd = (int) PyLong_AsLong(fileno);
878+
Py_DECREF(fileno);
879+
if (fd == -1 && PyErr_Occurred()) {
880+
goto error;
881+
}
882+
883+
self->encoding = _Py_device_encoding(fd);
881884
if (self->encoding == NULL)
882885
goto error;
883886
else if (!PyUnicode_Check(self->encoding))

‎Modules/posixmodule.c‎

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9326,35 +9326,11 @@ static PyObject *
93269326
device_encoding(PyObject *self, PyObject *args)
93279327
{
93289328
int fd;
9329-
#if defined(MS_WINDOWS) || defined(MS_WIN64)
9330-
UINT cp;
9331-
#endif
9329+
93329330
if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
93339331
return NULL;
9334-
if (!_PyVerify_fd(fd) || !isatty(fd)) {
9335-
Py_INCREF(Py_None);
9336-
return Py_None;
9337-
}
9338-
#if defined(MS_WINDOWS) || defined(MS_WIN64)
9339-
if (fd == 0)
9340-
cp = GetConsoleCP();
9341-
else if (fd == 1 || fd == 2)
9342-
cp = GetConsoleOutputCP();
9343-
else
9344-
cp = 0;
9345-
/* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9346-
has no console */
9347-
if (cp != 0)
9348-
return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
9349-
#elif defined(CODESET)
9350-
{
9351-
char *codeset = nl_langinfo(CODESET);
9352-
if (codeset != NULL && codeset[0] != 0)
9353-
return PyUnicode_FromString(codeset);
9354-
}
9355-
#endif
9356-
Py_INCREF(Py_None);
9357-
return Py_None;
9332+
9333+
return _Py_device_encoding(fd);
93589334
}
93599335

93609336
#ifdef __VMS

‎Python/fileutils.c‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,40 @@
33
# include <windows.h>
44
#endif
55

6+
#ifdef HAVE_LANGINFO_H
7+
#include <langinfo.h>
8+
#endif
9+
10+
PyObject *
11+
_Py_device_encoding(int fd)
12+
{
13+
#if defined(MS_WINDOWS) || defined(MS_WIN64)
14+
UINT cp;
15+
#endif
16+
if (!_PyVerify_fd(fd) || !isatty(fd)) {
17+
Py_RETURN_NONE;
18+
}
19+
#if defined(MS_WINDOWS) || defined(MS_WIN64)
20+
if (fd == 0)
21+
cp = GetConsoleCP();
22+
else if (fd == 1 || fd == 2)
23+
cp = GetConsoleOutputCP();
24+
else
25+
cp = 0;
26+
/* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
27+
has no console */
28+
if (cp != 0)
29+
return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
30+
#elif defined(CODESET)
31+
{
32+
char *codeset = nl_langinfo(CODESET);
33+
if (codeset != NULL && codeset[0] != 0)
34+
return PyUnicode_FromString(codeset);
35+
}
36+
#endif
37+
Py_RETURN_NONE;
38+
}
39+
640
#ifdef HAVE_STAT
741

842
/* Decode a byte string from the locale encoding with the

0 commit comments

Comments
 (0)