Skip to content

Comments

StatelessIncrementalEncoder, sys attributes#7195

Merged
youknowone merged 3 commits intoRustPython:mainfrom
youknowone:sys
Feb 21, 2026
Merged

StatelessIncrementalEncoder, sys attributes#7195
youknowone merged 3 commits intoRustPython:mainfrom
youknowone:sys

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Feb 21, 2026

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved codec compatibility with enhanced fallback support for encoder/decoder operations
    • Refined I/O error handling during initialization for better stability
  • New Features

    • Automatic registration of latin-1 encoding aliases for broader compatibility

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 21, 2026

📝 Walkthrough

Walkthrough

This pull request introduces stateless codec encoder/decoder wrapper classes as fallback mechanisms in the _io module, refactors the sys module by removing module parameters from pyclass definitions and dynamically computing flag properties, and updates VM initialization logic for codec registration and stdio setup with refined feature gating and error handling.

Changes

Cohort / File(s) Summary
Codec Wrapper Fallback Implementation
crates/vm/src/stdlib/io.rs
Introduces StatelessIncrementalEncoder and StatelessIncrementalDecoder pyclasses that wrap non-incremental codec functions, providing fallback paths when incremental interfaces are unavailable. Each class implements encode/decode methods that delegate to underlying codec functions and state management methods.
Sys Module Refactoring
crates/vm/src/stdlib/sys.rs
Removes module = "sys" parameters from _BootstrapStderr and _SandboxStdio pyclass definitions. Refactors FlagsData by removing thread_inherit_context and context_aware_warnings fields and converting them to dynamic pygetset properties on PyFlags. Marks Windows version struct fields with #[pystruct_sequence(skip)] to exclude them from Python-facing struct sequence.
VM Initialization & Codec Registration
crates/vm/src/vm/mod.rs
Adds early registration of latin_1 codec aliases when freeze-stdlib feature is enabled. Refines stdio initialization by adding feature-gating requiring both host_env and stdio features, adjusting error handler derivation logic to check stdio_errors setting and fallback to appropriate defaults.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • #6617: Modifies vm/mod.rs for stdlib encoding registration and stdio initialization/error handling updates.
  • #7034: Adjusts VM initialization and encoding registration logic in vm/mod.rs for stdlib bootstrap.
  • #7035: Modifies stdio initialization closure with feature-gating and return type adjustments in vm/mod.rs.

Suggested reviewers

  • fanninpm
  • coolreader18
  • ShaharNaveh

Poem

🐰 A stateless hopper skips through io,
Flags flutter free with property glow,
Latin codecs register their call,
While stdio bootstraps through it all!
Refactor, fallback, feature gates shine,
The VM's foundation now redesigned.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title directly references two of the three main changes: StatelessIncrementalEncoder (new classes added to io.rs) and sys attributes (modifications to sys.rs attributes and getters). However, it omits the third significant change in vm/mod.rs regarding stdlib encodings registration and stdio initialization.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[ ] test: cpython/Lib/test/test_compile.py (TODO: 24)
[x] test: cpython/Lib/test/test_compiler_assemble.py
[x] test: cpython/Lib/test/test_compiler_codegen.py
[x] test: cpython/Lib/test/test_peepholer.py (TODO: 32)

dependencies:

dependent tests: (no tests depend on compile)

[x] test: cpython/Lib/test/test_sys.py (TODO: 8)
[ ] test: cpython/Lib/test/test_syslog.py (TODO: 2)
[ ] test: cpython/Lib/test/test_sys_setprofile.py (TODO: 14)
[ ] test: cpython/Lib/test/test_sys_settrace.py (TODO: 85)

dependencies:

dependent tests: (213 tests)

  • sys: regrtestdata test___all__ test__colorize test__locale test__osx_support test_android test_annotationlib test_argparse test_array test_ast test_asyncio test_audit test_bdb test_bigaddrspace test_bigmem test_bisect test_buffer test_builtin test_bytes test_bz2 test_c_locale_coercion test_calendar test_call test_cmath test_cmd test_cmd_line test_cmd_line_script test_code test_code_module test_codeccallbacks test_codecs test_collections test_compile test_compileall test_complex test_concurrent_futures test_context test_contextlib test_coroutines test_csv test_ctypes test_datetime test_dbm test_dbm_sqlite3 test_descr test_dict test_difflib test_dis test_doctest test_doctest2 test_docxmlrpc test_dtrace test_dynamic test_dynamicclassattribute test_email test_ensurepip test_enum test_enumerate test_eof test_except_star test_exceptions test_faulthandler test_fcntl test_file test_file_eintr test_fileinput test_fileio test_float test_fork1 test_format test_fractions test_frozen test_functools test_future_stmt test_generators test_genericpath test_getopt test_glob test_grammar test_gzip test_hash test_hashlib test_http_cookiejar test_httpservers test_importlib test_inspect test_int test_io test_iter test_itertools test_json test_largefile test_launcher test_list test_locale test_logging test_long test_lzma test_mailbox test_marshal test_math test_memoryio test_memoryview test_metaclass test_mimetypes test_mmap test_msvcrt test_multiprocessing_fork test_multiprocessing_main_handling test_ntpath test_numeric_tower test_operator test_optparse test_ordered_dict test_os test_osx_env test_pathlib test_patma test_peepholer test_pickle test_pkg test_pkgutil test_platform test_plistlib test_posix test_posixpath test_print test_property test_pty test_pwd test_py_compile test_pyclbr test_pydoc test_pyexpat test_quopri test_raise test_range test_re test_regrtest test_repl test_reprlib test_resource test_runpy test_script_helper test_select test_selectors test_shutil test_signal test_site test_slice test_smtplib test_socket test_sqlite3 test_ssl test_stable_abi_ctypes test_stat test_statistics test_str test_strftime test_string_literals test_strtod test_struct test_subprocess test_support test_symtable test_sys test_sys_setprofile test_sys_settrace test_sysconfig test_syslog test_tarfile test_tempfile test_termios test_threadedtempfile test_threading test_threading_local test_threadsignals test_time test_timeit test_tomllib test_tools test_trace test_traceback test_type_comments test_types test_typing test_unicode_file test_unicode_file_functions test_unicodedata test_unittest test_univnewlines test_urllib test_urllib2 test_urllib2net test_urlparse test_utf8_mode test_uuid test_venv test_wait3 test_wait4 test_wave test_weakref test_webbrowser test_winconsoleio test_with test_wsgiref test_xml_etree test_xmlrpc test_zipapp test_zipfile test_zipfile64 test_zipimport test_zlib

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@youknowone youknowone marked this pull request as ready for review February 21, 2026 14:46
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/vm/src/stdlib/io.rs`:
- Around line 2610-2653: The encode wrapper in StatelessIncrementalEncoder
currently ignores the second tuple item from the codec result; update the encode
method to validate that res.try_into_value(vm)? yields a 2-tuple where the
second element is an integer (non-negative if appropriate to your codec
contract) and raise a TypeError via vm.new_type_error when it is missing or not
an int (reusing the existing error message "encoder must return a tuple (object,
integer)"). Locate the encode method and the tuple variable, convert/validate
tuple[1] as an integer (e.g., via try_into_value or checking .isinstance), and
only then return tuple[0].clone(), ensuring malformed codec outputs surface as
errors.
- Around line 2656-2694: In StatelessIncrementalDecoder::decode, validate that
the second tuple element is an integer and raise a TypeError if not; after
converting res to PyTupleRef and checking tuple.len(), try to convert tuple[1]
to an integer (using the VM's integer/index conversion helpers) and return
Err(vm.new_type_error("decoder must return a tuple (object, integer)")) when
conversion fails, otherwise proceed to return tuple[0].clone() as before. Ensure
this check is applied in the decode method to mirror the encoder wrapper's
guard.

Comment on lines +2610 to +2653
#[pyclass(module = "_io", name, no_attr)]
#[derive(Debug, PyPayload)]
struct StatelessIncrementalEncoder {
encode: PyObjectRef,
errors: Option<PyStrRef>,
name: Option<PyStrRef>,
}

#[pyclass]
impl StatelessIncrementalEncoder {
#[pymethod]
fn encode(
&self,
input: PyObjectRef,
_final: OptionalArg<bool>,
vm: &VirtualMachine,
) -> PyResult {
let mut args: Vec<PyObjectRef> = vec![input];
if let Some(errors) = &self.errors {
args.push(errors.to_owned().into());
}
let res = self.encode.call(args, vm)?;
let tuple: PyTupleRef = res.try_into_value(vm)?;
if tuple.len() != 2 {
return Err(vm.new_type_error("encoder must return a tuple (object, integer)"));
}
Ok(tuple[0].clone())
}

#[pymethod]
fn reset(&self) {}

#[pymethod]
fn setstate(&self, _state: PyObjectRef) {}

#[pymethod]
fn getstate(&self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.new_int(0).into()
}

#[pygetset]
fn name(&self) -> Option<PyStrRef> {
self.name.clone()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Validate the consumed-length element from codec output.

The wrapper ignores the second tuple item entirely; if it’s not an integer, malformed codecs won’t surface errors. Consider validating it to match the codec contract.

🛠️ Suggested guard for the consumed-length slot
             let tuple: PyTupleRef = res.try_into_value(vm)?;
             if tuple.len() != 2 {
                 return Err(vm.new_type_error("encoder must return a tuple (object, integer)"));
             }
+            let _consumed: isize = isize::try_from_object(vm, tuple[1].clone()).map_err(|_| {
+                vm.new_type_error("encoder must return a tuple (object, integer)")
+            })?;
             Ok(tuple[0].clone())
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[pyclass(module = "_io", name, no_attr)]
#[derive(Debug, PyPayload)]
struct StatelessIncrementalEncoder {
encode: PyObjectRef,
errors: Option<PyStrRef>,
name: Option<PyStrRef>,
}
#[pyclass]
impl StatelessIncrementalEncoder {
#[pymethod]
fn encode(
&self,
input: PyObjectRef,
_final: OptionalArg<bool>,
vm: &VirtualMachine,
) -> PyResult {
let mut args: Vec<PyObjectRef> = vec![input];
if let Some(errors) = &self.errors {
args.push(errors.to_owned().into());
}
let res = self.encode.call(args, vm)?;
let tuple: PyTupleRef = res.try_into_value(vm)?;
if tuple.len() != 2 {
return Err(vm.new_type_error("encoder must return a tuple (object, integer)"));
}
Ok(tuple[0].clone())
}
#[pymethod]
fn reset(&self) {}
#[pymethod]
fn setstate(&self, _state: PyObjectRef) {}
#[pymethod]
fn getstate(&self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.new_int(0).into()
}
#[pygetset]
fn name(&self) -> Option<PyStrRef> {
self.name.clone()
}
#[pyclass(module = "_io", name, no_attr)]
#[derive(Debug, PyPayload)]
struct StatelessIncrementalEncoder {
encode: PyObjectRef,
errors: Option<PyStrRef>,
name: Option<PyStrRef>,
}
#[pyclass]
impl StatelessIncrementalEncoder {
#[pymethod]
fn encode(
&self,
input: PyObjectRef,
_final: OptionalArg<bool>,
vm: &VirtualMachine,
) -> PyResult {
let mut args: Vec<PyObjectRef> = vec![input];
if let Some(errors) = &self.errors {
args.push(errors.to_owned().into());
}
let res = self.encode.call(args, vm)?;
let tuple: PyTupleRef = res.try_into_value(vm)?;
if tuple.len() != 2 {
return Err(vm.new_type_error("encoder must return a tuple (object, integer)"));
}
let _consumed: isize = isize::try_from_object(vm, tuple[1].clone()).map_err(|_| {
vm.new_type_error("encoder must return a tuple (object, integer)")
})?;
Ok(tuple[0].clone())
}
#[pymethod]
fn reset(&self) {}
#[pymethod]
fn setstate(&self, _state: PyObjectRef) {}
#[pymethod]
fn getstate(&self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.new_int(0).into()
}
#[pygetset]
fn name(&self) -> Option<PyStrRef> {
self.name.clone()
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm/src/stdlib/io.rs` around lines 2610 - 2653, The encode wrapper in
StatelessIncrementalEncoder currently ignores the second tuple item from the
codec result; update the encode method to validate that res.try_into_value(vm)?
yields a 2-tuple where the second element is an integer (non-negative if
appropriate to your codec contract) and raise a TypeError via vm.new_type_error
when it is missing or not an int (reusing the existing error message "encoder
must return a tuple (object, integer)"). Locate the encode method and the tuple
variable, convert/validate tuple[1] as an integer (e.g., via try_into_value or
checking .isinstance), and only then return tuple[0].clone(), ensuring malformed
codec outputs surface as errors.

Comment on lines +2656 to +2694
#[pyclass(module = "_io", name, no_attr)]
#[derive(Debug, PyPayload)]
struct StatelessIncrementalDecoder {
decode: PyObjectRef,
errors: Option<PyStrRef>,
}

#[pyclass]
impl StatelessIncrementalDecoder {
#[pymethod]
fn decode(
&self,
input: PyObjectRef,
_final: OptionalArg<bool>,
vm: &VirtualMachine,
) -> PyResult {
let mut args: Vec<PyObjectRef> = vec![input];
if let Some(errors) = &self.errors {
args.push(errors.to_owned().into());
}
let res = self.decode.call(args, vm)?;
let tuple: PyTupleRef = res.try_into_value(vm)?;
if tuple.len() != 2 {
return Err(vm.new_type_error("decoder must return a tuple (object, integer)"));
}
Ok(tuple[0].clone())
}

#[pymethod]
fn getstate(&self, vm: &VirtualMachine) -> (PyBytesRef, u64) {
(vm.ctx.empty_bytes.to_owned(), 0)
}

#[pymethod]
fn setstate(&self, _state: PyTupleRef, _vm: &VirtualMachine) {}

#[pymethod]
fn reset(&self) {}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Validate the consumed-length element from codec output.

Same concern as the encoder wrapper: the second tuple element should be an integer; otherwise invalid codec implementations pass silently.

🛠️ Suggested guard for the consumed-length slot
             let tuple: PyTupleRef = res.try_into_value(vm)?;
             if tuple.len() != 2 {
                 return Err(vm.new_type_error("decoder must return a tuple (object, integer)"));
             }
+            let _consumed: isize = isize::try_from_object(vm, tuple[1].clone()).map_err(|_| {
+                vm.new_type_error("decoder must return a tuple (object, integer)")
+            })?;
             Ok(tuple[0].clone())
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm/src/stdlib/io.rs` around lines 2656 - 2694, In
StatelessIncrementalDecoder::decode, validate that the second tuple element is
an integer and raise a TypeError if not; after converting res to PyTupleRef and
checking tuple.len(), try to convert tuple[1] to an integer (using the VM's
integer/index conversion helpers) and return Err(vm.new_type_error("decoder must
return a tuple (object, integer)")) when conversion fails, otherwise proceed to
return tuple[0].clone() as before. Ensure this check is applied in the decode
method to mirror the encoder wrapper's guard.

@youknowone youknowone merged commit 2981010 into RustPython:main Feb 21, 2026
13 of 14 checks passed
@youknowone youknowone deleted the sys branch February 21, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant