@@ -263,7 +263,7 @@ decode_ascii_surrogateescape(const char *arg, size_t *size)
263263
264264#if !defined(__APPLE__ ) && !defined(__ANDROID__ )
265265static wchar_t *
266- decode_locale (const char * arg , size_t * size )
266+ decode_current_locale (const char * arg , size_t * size )
267267{
268268 wchar_t * res ;
269269 size_t argsize ;
@@ -380,32 +380,13 @@ decode_locale(const char* arg, size_t *size)
380380#endif
381381
382382
383- /* Decode a byte string from the locale encoding with the
384- surrogateescape error handler: undecodable bytes are decoded as characters
385- in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
386- character, escape the bytes using the surrogateescape error handler instead
387- of decoding them.
388-
389- Return a pointer to a newly allocated wide character string, use
390- PyMem_RawFree() to free the memory. If size is not NULL, write the number of
391- wide characters excluding the null character into *size
392-
393- Return NULL on decoding error or memory allocation error. If *size* is not
394- NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
395- decoding error.
396-
397- Decoding errors should never happen, unless there is a bug in the C
398- library.
399-
400- Use the Py_EncodeLocale() function to encode the character string back to a
401- byte string. */
402- wchar_t *
403- Py_DecodeLocale (const char * arg , size_t * size )
383+ static wchar_t *
384+ decode_locale (const char * arg , size_t * size , int ignore_utf8_mode )
404385{
405386#if defined(__APPLE__ ) || defined(__ANDROID__ )
406387 return _Py_DecodeUTF8_surrogateescape (arg , strlen (arg ), size );
407388#else
408- if (Py_UTF8Mode == 1 ) {
389+ if (! ignore_utf8_mode && Py_UTF8Mode == 1 ) {
409390 return _Py_DecodeUTF8_surrogateescape (arg , strlen (arg ), size );
410391 }
411392
@@ -426,11 +407,45 @@ Py_DecodeLocale(const char* arg, size_t *size)
426407 }
427408#endif
428409
429- return decode_locale (arg , size );
410+ return decode_current_locale (arg , size );
430411#endif /* __APPLE__ or __ANDROID__ */
431412}
432413
433414
415+ /* Decode a byte string from the locale encoding with the
416+ surrogateescape error handler: undecodable bytes are decoded as characters
417+ in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate
418+ character, escape the bytes using the surrogateescape error handler instead
419+ of decoding them.
420+
421+ Return a pointer to a newly allocated wide character string, use
422+ PyMem_RawFree() to free the memory. If size is not NULL, write the number of
423+ wide characters excluding the null character into *size
424+
425+ Return NULL on decoding error or memory allocation error. If *size* is not
426+ NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on
427+ decoding error.
428+
429+ Decoding errors should never happen, unless there is a bug in the C
430+ library.
431+
432+ Use the Py_EncodeLocale() function to encode the character string back to a
433+ byte string. */
434+ wchar_t *
435+ Py_DecodeLocale (const char * arg , size_t * size )
436+ {
437+ return decode_locale (arg , size , 0 );
438+ }
439+
440+
441+ /* Similar to Py_DecodeLocale() but ignore the UTF-8 mode */
442+ wchar_t *
443+ _Py_DecodeCurrentLocale (const char * arg , size_t * size )
444+ {
445+ return decode_locale (arg , size , 1 );
446+ }
447+
448+
434449#if !defined(__APPLE__ ) && !defined(__ANDROID__ )
435450static char *
436451encode_current_locale (const wchar_t * text , size_t * error_pos , int raw_malloc )
@@ -508,12 +523,13 @@ encode_current_locale(const wchar_t *text, size_t *error_pos, int raw_malloc)
508523#endif
509524
510525static char *
511- encode_locale (const wchar_t * text , size_t * error_pos , int raw_malloc )
526+ encode_locale (const wchar_t * text , size_t * error_pos ,
527+ int raw_malloc , int ignore_utf8_mode )
512528{
513529#if defined(__APPLE__ ) || defined(__ANDROID__ )
514530 return _Py_EncodeUTF8_surrogateescape (text , error_pos , raw_malloc );
515531#else /* __APPLE__ */
516- if (Py_UTF8Mode == 1 ) {
532+ if (! ignore_utf8_mode && Py_UTF8Mode == 1 ) {
517533 return _Py_EncodeUTF8_surrogateescape (text , error_pos , raw_malloc );
518534 }
519535
@@ -544,7 +560,7 @@ encode_locale(const wchar_t *text, size_t *error_pos, int raw_malloc)
544560char *
545561Py_EncodeLocale (const wchar_t * text , size_t * error_pos )
546562{
547- return encode_locale (text , error_pos , 0 );
563+ return encode_locale (text , error_pos , 0 , 0 );
548564}
549565
550566
@@ -553,7 +569,15 @@ Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
553569char *
554570_Py_EncodeLocaleRaw (const wchar_t * text , size_t * error_pos )
555571{
556- return encode_locale (text , error_pos , 1 );
572+ return encode_locale (text , error_pos , 1 , 0 );
573+ }
574+
575+
576+ /* Similar to _Py_EncodeLocaleRaw() but ignore the UTF-8 Mode */
577+ char *
578+ _Py_EncodeCurrentLocale (const wchar_t * text , size_t * error_pos )
579+ {
580+ return encode_locale (text , error_pos , 1 , 1 );
557581}
558582
559583
0 commit comments