File tree 2 files changed +17
-11
lines changed
2 files changed +17
-11
lines changed Original file line number Diff line number Diff line change @@ -152,7 +152,14 @@ def search_function(encoding):
152
152
# Return the registry entry
153
153
return entry
154
154
155
+ # Register the search_function in the Python codec registry
156
+ codecs .register (search_function )
157
+
155
158
if sys .platform == 'win32' :
159
+ # bpo-671666, bpo-46668: If Python does not implement a codec for current
160
+ # Windows ANSI code page, use the "mbcs" codec instead:
161
+ # WideCharToMultiByte() and MultiByteToWideChar() functions with CP_ACP.
162
+ # Python does not support custom code pages.
156
163
def _alias_mbcs (encoding ):
157
164
try :
158
165
import _winapi
@@ -164,8 +171,4 @@ def _alias_mbcs(encoding):
164
171
# Imports may fail while we are shutting down
165
172
pass
166
173
167
- # It must be registered before search_function()
168
174
codecs .register (_alias_mbcs )
169
-
170
- # Register the search_function in the Python codec registry
171
- codecs .register (search_function )
Original file line number Diff line number Diff line change @@ -3191,13 +3191,16 @@ def test_incremental(self):
3191
3191
self .assertEqual (decoded , ('abc' , 3 ))
3192
3192
3193
3193
def test_mbcs_alias (self ):
3194
- # On Windows, the encoding name must be the ANSI code page
3195
- encoding = locale .getpreferredencoding (False )
3196
- self .assertTrue (encoding .startswith ('cp' ), encoding )
3197
-
3198
- # The encodings module create a "mbcs" alias to the ANSI code page
3199
- codec = codecs .lookup (encoding )
3200
- self .assertEqual (codec .name , "mbcs" )
3194
+ # Check that looking up our 'default' codepage will return
3195
+ # mbcs when we don't have a more specific one available
3196
+ code_page = 99_999
3197
+ name = f'cp{ code_page } '
3198
+ with mock .patch ('_winapi.GetACP' , return_value = code_page ):
3199
+ try :
3200
+ codec = codecs .lookup (name )
3201
+ self .assertEqual (codec .name , 'mbcs' )
3202
+ finally :
3203
+ codecs .unregister (name )
3201
3204
3202
3205
@support .bigmemtest (size = 2 ** 31 , memuse = 7 , dry_run = False )
3203
3206
def test_large_input (self , size ):
You can’t perform that action at this time.
0 commit comments