Skip to content

Match CPython unsigned socket conversion errors - #8257

Merged
youknowone merged 1 commit into
RustPython:mainfrom
kyokuping:codex/socket-ntoh-errors
Jul 15, 2026
Merged

Match CPython unsigned socket conversion errors#8257
youknowone merged 1 commit into
RustPython:mainfrom
kyokuping:codex/socket-ntoh-errors

Conversation

@kyokuping

@kyokuping kyokuping commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Assisted-by: Codex:gpt-5.4

Summary

  • Fixed test_socket.GeneralModuleTests.testNtoHErrors by aligning RustPython’s socket.htons(), socket.htonl(), socket.ntohs(), and socket.ntohl() error handling with CPython.
  • This change makes the socket byte-order conversion helpers accept indexable Python objects, route conversion through the shared unsigned integer path, and raise ValueError for negative inputs instead of incorrectly surfacing OverflowError. It also updates the shared integer conversion helper so unsigned primitive conversions report the CPython-style negative-number error, which lets the expected-failure marker be removed from Lib/test/test_socket.py.

Summary by CodeRabbit

  • Bug Fixes
    • Updated socket ancillary-data helpers to accept Python integer-like inputs and correctly raise overflow/out-of-range errors.
    • Improved IPv6 address tuple parsing for the optional flowinfo value.
    • Prevented negative integers from converting to unsigned integer types, raising a clear ValueError.
    • Aligned integer array element conversion with the same unsigned/primitive conversion rules.
    • Improved Windows waitstatus_to_exitcode input handling to accept Python integer-like values consistently.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 973f9b84-cd1f-4a83-bfab-64e1ec4a783e

📥 Commits

Reviewing files that changed from the base of the PR and between 4f12c39 and e65442f.

⛔ Files ignored due to path filters (4)
  • Lib/test/test_lzma.py is excluded by !Lib/**
  • Lib/test/test_memoryio.py is excluded by !Lib/**
  • Lib/test/test_resource.py is excluded by !Lib/**
  • Lib/test/test_socket.py is excluded by !Lib/**
📒 Files selected for processing (4)
  • crates/stdlib/src/array.rs
  • crates/stdlib/src/socket.rs
  • crates/vm/src/builtins/int.rs
  • crates/vm/src/stdlib/os.rs
🚧 Files skipped from review as they are similar to previous changes (4)
  • crates/stdlib/src/socket.rs
  • crates/stdlib/src/array.rs
  • crates/vm/src/stdlib/os.rs
  • crates/vm/src/builtins/int.rs

📝 Walkthrough

Walkthrough

Integer conversion now rejects negative values for unsigned targets and exposes raw conversion for callers. Array elements, socket arguments, and the Windows wait-status helper use VM-based Python integer conversion.

Changes

Integer conversion behavior

Layer / File(s) Summary
Validate unsigned primitive conversion
crates/vm/src/builtins/int.rs
Unsigned conversion raises ValueError for negative PyInt values and delegates conversion through the new raw helper.
Update array element conversion
crates/stdlib/src/array.rs
Indexed array values use try_to_primitive_raw(vm) for integer element conversion.
Update socket argument conversion
crates/stdlib/src/socket.rs
IPv6 flowinfo, CMSG_LEN, and CMSG_SPACE use VM-based indexing and raw primitive conversion, with range errors preserved for ancillary-data helpers.
Update Windows status conversion
crates/vm/src/stdlib/os.rs
waitstatus_to_exitcode accepts a Python object and converts it through VM indexing and raw u64 conversion.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: shaharnaveh

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: aligning socket unsigned conversion error handling with CPython.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@github-actions

github-actions Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

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

[x] test: cpython/Lib/test/test_resource.py (TODO: 3)

dependencies:

dependent tests: (4 tests)

  • resource: test_os test_selectors test_subprocess test_threading

[x] lib: cpython/Lib/socket.py
[ ] test: cpython/Lib/test/test_socket.py (TODO: 15)

dependencies:

  • socket

dependent tests: (101 tests)

  • socket: test_asyncio test_epoll test_exception_hierarchy test_external_inspection test_ftplib test_httplib test_httpservers test_imaplib test_kqueue test_largefile test_logging test_mailbox test_mmap test_os test_pathlib test_poplib test_pty test_selectors test_signal test_smtplib test_smtpnet test_socket test_socketserver test_ssl test_stat test_subprocess test_support test_sys test_timeout test_urllib test_urllib2 test_urllib2net test_urllib_response test_urllibnet test_xmlrpc
    • asyncio: test_asyncio test_inspect test_pdb test_unittest
    • email.utils: test_email
      • http.server: test_robotparser test_urllib2_localnet
      • logging.handlers: test_concurrent_futures test_pkgutil
      • urllib.request: test_http_cookiejar test_pydoc test_sax test_site
    • http.client: test_docxmlrpc test_hashlib test_ucn test_unicodedata test_wsgiref
    • http.server:
      • pydoc: test_enum
    • mailbox: test_genericalias
    • multiprocessing: test_compileall test_concurrent_futures test_fcntl test_memoryview test_multiprocessing_main_handling test_re
      • concurrent.futures.process: test_concurrent_futures
    • platform: test__locale test__osx_support test_baseexception test_builtin test_cmath test_ctypes test_math test_mimetypes test_platform test_posix test_regrtest test_shutil test_strptime test_sysconfig test_time test_winreg
    • ssl: test_venv
    • urllib.request:
      • pathlib: test_ast test_dbm_sqlite3 test_ensurepip test_importlib test_json test_launcher test_pathlib test_peg_generator test_pyrepl test_runpy test_tarfile test_tempfile test_tomllib test_tools test_traceback test_unparse test_winapi test_zipapp test_zipfile test_zoneinfo test_zstd
    • uuid:
      • wave: test_wave

[x] lib: cpython/Lib/io.py
[x] lib: cpython/Lib/_pyio.py
[ ] test: cpython/Lib/test/test_io.py (TODO: 13)
[x] test: cpython/Lib/test/test_bufio.py
[x] test: cpython/Lib/test/test_fileio.py (TODO: 1)
[ ] test: cpython/Lib/test/test_memoryio.py (TODO: 25)

dependencies:

  • io

dependent tests: (108 tests)

  • io: test__colorize test_android test_argparse test_ast test_asyncio test_base64 test_buffer test_bufio test_builtin test_bz2 test_calendar test_cmd test_cmd_line_script test_codecs test_compile test_compileall test_compiler_assemble test_concurrent_futures test_configparser test_contextlib test_csv test_dbm_dumb test_descr test_dis test_email test_enum test_file test_fileinput test_fileio test_ftplib test_generated_cases test_getpass test_gzip test_hashlib test_http_cookiejar test_httplib test_httpservers test_importlib test_inspect test_io test_json test_largefile test_logging test_lzma test_mailbox test_marshal test_memoryio test_memoryview test_mimetypes test_minidom test_multibytecodec test_optparse test_pathlib test_pdb test_peg_generator test_pickle test_pickletools test_platform test_plistlib test_pprint test_print test_profile test_pstats test_pty test_pulldom test_pydoc test_pyexpat test_pyrepl test_quopri test_regrtest test_robotparser test_sax test_shlex test_shutil test_site test_smtplib test_socket test_socketserver test_subprocess test_support test_sys test_tarfile test_tempfile test_threadedtempfile test_timeit test_tokenize test_traceback test_types test_typing test_unittest test_univnewlines test_urllib test_urllib2 test_uuid test_wave test_webbrowser test_winconsoleio test_wsgiref test_xml_dom_xmlbuilder test_xml_etree test_xml_etree_c test_xmlrpc test_xpickle test_zipapp test_zipfile test_zipimport test_zoneinfo test_zstd

[x] lib: cpython/Lib/lzma.py
[x] test: cpython/Lib/test/test_lzma.py (TODO: 10)

dependencies:

  • lzma

dependent tests: (101 tests)

  • lzma: test_lzma test_tarfile
    • shutil: test_argparse test_bz2 test_compileall test_ctypes test_embed test_filecmp test_glob test_httpservers test_importlib test_inspect test_largefile test_launcher test_logging test_modulefinder test_os test_peg_generator test_pkgutil test_py_compile test_reprlib test_sax test_shutil test_site test_string_literals test_subprocess test_support test_sysconfig test_tempfile test_traceback test_unicode_file test_venv test_zoneinfo
      • ctypes.util: test_ctypes
      • ensurepip: test_ensurepip
      • http.server: test_robotparser test_urllib2_localnet test_xmlrpc
      • multiprocessing.util: test_asyncio test_concurrent_futures
      • pathlib: test_ast test_dbm_sqlite3 test_importlib test_json test_pathlib test_pyrepl test_runpy test_tomllib test_tools test_unparse test_winapi test_zipapp test_zipfile test_zstd
      • tempfile: test_asyncio test_bytes test_cmd_line test_compile test_concurrent_futures test_contextlib test_cprofile test_csv test_dis test_doctest test_faulthandler test_fileinput test_generated_cases test_genericalias test_hashlib test_importlib test_linecache test_mailbox test_ntpath test_pickle test_pkg test_posix test_pstats test_pydoc test_pyrepl test_regrtest test_selectors test_socket test_sys test_sys_settrace test_tabnanny test_termios test_threadedtempfile test_tokenize test_turtle test_urllib test_urllib2 test_urllib_response test_winconsoleio test_zipfile test_zipfile64
      • webbrowser: test_webbrowser
      • zipapp: test_pdb
      • zipfile: test_zipfile test_zipimport test_zipimport_support
    • zipfile:
      • importlib.metadata: test_importlib

Legend:

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

@ShaharNaveh ShaharNaveh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm:)

tysm, and welcome to the project:)

@youknowone youknowone added the z-ca-2026 Tag to track Contribution Academy 2026 label Jul 12, 2026
@kyokuping
kyokuping force-pushed the codex/socket-ntoh-errors branch from 9176b4a to c3fa208 Compare July 13, 2026 09:36
@youknowone

Copy link
Copy Markdown
Member

you fixed many tests:

UNEXPECTED SUCCESS: test_seek (test.test_memoryio.CBytesIOTest.test_seek)
UNEXPECTED SUCCESS: test_seek (test.test_memoryio.CStringIOTest.test_seek)

please remove expected failure marks from those tests

@kyokuping
kyokuping force-pushed the codex/socket-ntoh-errors branch from c3fa208 to 3a7b464 Compare July 13, 2026 15:11
@kyokuping
kyokuping force-pushed the codex/socket-ntoh-errors branch from 4f12c39 to e65442f Compare July 14, 2026 10:31

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you so much! and welcome to RustPython project

@youknowone
youknowone merged commit 415f3d3 into RustPython:main Jul 15, 2026
27 checks passed
@kyokuping
kyokuping deleted the codex/socket-ntoh-errors branch August 8, 2026 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2026 Tag to track Contribution Academy 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants