@@ -60,51 +60,68 @@ char _get_console_type(HANDLE handle) {
6060}
6161
6262char _PyIO_get_console_type (PyObject * path_or_fd ) {
63- int fd ;
64-
65- fd = PyLong_AsLong (path_or_fd );
63+ int fd = PyLong_AsLong (path_or_fd );
6664 PyErr_Clear ();
6765 if (fd >= 0 ) {
6866 HANDLE handle ;
6967 _Py_BEGIN_SUPPRESS_IPH
7068 handle = (HANDLE )_get_osfhandle (fd );
7169 _Py_END_SUPPRESS_IPH
72- if (! handle )
70+ if (handle == INVALID_HANDLE_VALUE )
7371 return '\0' ;
7472 return _get_console_type (handle );
7573 }
7674
77- PyObject * decoded , * decoded_upper ;
75+ PyObject * decoded ;
76+ wchar_t * decoded_wstr ;
7877
79- int d = PyUnicode_FSDecoder (path_or_fd , & decoded );
80- if (!d ) {
78+ if (!PyUnicode_FSDecoder (path_or_fd , & decoded )) {
8179 PyErr_Clear ();
8280 return '\0' ;
8381 }
84- if (!PyUnicode_Check (decoded )) {
85- Py_CLEAR (decoded );
86- return '\0' ;
87- }
88- decoded_upper = PyObject_CallMethod (decoded , "upper" , NULL );
82+ decoded_wstr = PyUnicode_AsWideCharString (decoded , NULL );
8983 Py_CLEAR (decoded );
90- if (!decoded_upper ) {
84+ if (!decoded_wstr ) {
9185 PyErr_Clear ();
9286 return '\0' ;
9387 }
9488
89+ DWORD length ;
90+ wchar_t name_buf [MAX_PATH ], * pname_buf = name_buf ;
91+
92+ length = GetFullPathNameW (decoded_wstr , MAX_PATH , pname_buf , NULL );
93+ if (length > MAX_PATH ) {
94+ pname_buf = PyMem_New (wchar_t , length );
95+ if (pname_buf )
96+ length = GetFullPathNameW (decoded_wstr , length , pname_buf , NULL );
97+ else
98+ length = 0 ;
99+ }
100+ PyMem_Free (decoded_wstr );
101+
95102 char m = '\0' ;
96- if (_PyUnicode_EqualToASCIIString (decoded_upper , "CONIN$" )) {
97- m = 'r' ;
98- } else if (_PyUnicode_EqualToASCIIString (decoded_upper , "CONOUT$" )) {
99- m = 'w' ;
100- } else if (_PyUnicode_EqualToASCIIString (decoded_upper , "CON" )) {
101- m = 'x' ;
103+ if (length ) {
104+ wchar_t * name = pname_buf ;
105+ if (length >= 4 && name [3 ] == L'\\' &&
106+ (name [2 ] == L'.' || name [2 ] == L'?' ) &&
107+ name [1 ] == L'\\' && name [0 ] == L'\\' ) {
108+ name += 4 ;
109+ }
110+ if (!_wcsicmp (name , L"CONIN$" )) {
111+ m = 'r' ;
112+ } else if (!_wcsicmp (name , L"CONOUT$" )) {
113+ m = 'w' ;
114+ } else if (!_wcsicmp (name , L"CON" )) {
115+ m = 'x' ;
116+ }
102117 }
103118
104- Py_CLEAR (decoded_upper );
119+ if (pname_buf != name_buf )
120+ PyMem_Free (pname_buf );
105121 return m ;
106122}
107123
124+
108125/*[clinic input]
109126module _io
110127class _io._WindowsConsoleIO "winconsoleio *" "&PyWindowsConsoleIO_Type"
0 commit comments