Skip to content

Commit 1a7df66

Browse files
authored
Merge pull request RustPython#4781 from MegasKomnenos/io
Add encoding="locale" support for TextIOWrapper
2 parents 7b0ab17 + 7e6c331 commit 1a7df66

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

Lib/test/test_fileinput.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,6 @@ def test__getitem__(self):
391391
retval2 = fi[1]
392392
self.assertEqual(retval2, "line2\n")
393393

394-
# TODO: RUSTPYTHON
395-
@unittest.expectedFailure
396394
def test__getitem___deprecation(self):
397395
t = self.writeTmp("line1\nline2\n")
398396
with self.assertWarnsRegex(DeprecationWarning,
@@ -914,8 +912,6 @@ def test_empty_string(self):
914912
def test_no_ext(self):
915913
self.do_test_use_builtin_open("abcd", 2)
916914

917-
# TODO: RUSTPYTHON
918-
@unittest.expectedFailure
919915
@unittest.skipUnless(gzip, "Requires gzip and zlib")
920916
def test_gz_ext_fake(self):
921917
original_open = gzip.open

vm/src/stdlib/io.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,10 +2193,14 @@ mod _io {
21932193
*data = None;
21942194

21952195
let encoding = match args.encoding {
2196-
Some(enc) => enc,
2197-
None => {
2198-
// TODO: try os.device_encoding(fileno) and then locale.getpreferredencoding()
2199-
PyStr::from(crate::codecs::DEFAULT_ENCODING).into_ref(&vm.ctx)
2196+
None if vm.state.settings.utf8_mode > 0 => PyStr::from("utf-8").into_ref(&vm.ctx),
2197+
Some(enc) if enc.as_str() != "locale" => enc,
2198+
_ => {
2199+
// None without utf8_mode or "locale" encoding
2200+
vm.import("locale", None, 0)?
2201+
.get_attr("getencoding", vm)?
2202+
.call((), vm)?
2203+
.try_into_value(vm)?
22002204
}
22012205
};
22022206

vm/src/stdlib/sys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ mod sys {
728728
hash_randomization: settings.hash_seed.is_none() as u8,
729729
isolated: settings.isolated as u8,
730730
dev_mode: settings.dev_mode,
731-
utf8_mode: 1,
731+
utf8_mode: settings.utf8_mode,
732732
int_max_str_digits: -1,
733733
safe_path: false,
734734
warn_default_encoding: settings.warn_default_encoding as u8,

vm/src/vm/setting.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ pub struct Settings {
7474
/// false for wasm. Not a command-line option
7575
pub allow_external_library: bool,
7676

77+
pub utf8_mode: u8,
78+
7779
#[cfg(feature = "flame-it")]
7880
pub profile_output: Option<OsString>,
7981
#[cfg(feature = "flame-it")]
@@ -107,6 +109,7 @@ impl Default for Settings {
107109
stdio_unbuffered: false,
108110
check_hash_based_pycs: "default".to_owned(),
109111
allow_external_library: cfg!(feature = "importlib"),
112+
utf8_mode: 1,
110113
#[cfg(feature = "flame-it")]
111114
profile_output: None,
112115
#[cfg(feature = "flame-it")]

0 commit comments

Comments
 (0)