@@ -109,6 +109,12 @@ def is_unicode_string(val: Any) -> bool:
109
109
return hasattr (val , "encode" )
110
110
111
111
112
+ def get_locale_encoding ():
113
+ if sys .version_info >= (3 , 11 ):
114
+ return locale .getencoding ()
115
+ return locale .getpreferredencoding (False )
116
+
117
+
112
118
@overload
113
119
def make_string_path (dir_name : AnyStr ) -> AnyStr :
114
120
...
@@ -127,15 +133,15 @@ def to_string(path: Union[AnyStr, Union[str, bytes]]) -> str:
127
133
"""Return the string representation of a byte string using the preferred
128
134
encoding, or the string itself if path is a str."""
129
135
if isinstance (path , bytes ):
130
- return path .decode (locale . getpreferredencoding ( False ))
136
+ return path .decode (get_locale_encoding ( ))
131
137
return path
132
138
133
139
134
140
def to_bytes (path : Union [AnyStr , Union [str , bytes ]]) -> bytes :
135
141
"""Return the bytes representation of a string using the preferred
136
142
encoding, or the byte string itself if path is a byte string."""
137
143
if isinstance (path , str ):
138
- return bytes (path , locale . getpreferredencoding ( False ))
144
+ return bytes (path , get_locale_encoding ( ))
139
145
return path
140
146
141
147
@@ -181,7 +187,7 @@ def matching_string( # type: ignore[misc]
181
187
if string is None :
182
188
return string
183
189
if isinstance (matched , bytes ) and isinstance (string , str ):
184
- return string .encode (locale . getpreferredencoding ( False ))
190
+ return string .encode (get_locale_encoding ( ))
185
191
return string # pytype: disable=bad-return-type
186
192
187
193
0 commit comments