Skip to content

Match CPython unsigned socket conversion errors#8257

Open
kyokuping wants to merge 1 commit into
RustPython:mainfrom
kyokuping:codex/socket-ntoh-errors
Open

Match CPython unsigned socket conversion errors#8257
kyokuping wants to merge 1 commit into
RustPython:mainfrom
kyokuping:codex/socket-ntoh-errors

Conversation

@kyokuping

@kyokuping kyokuping commented Jul 11, 2026

Copy link
Copy Markdown

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
    • Improved socket byte-order functions to accept Python integer-compatible values and provide standard errors for invalid inputs.
    • Prevented negative numbers from being converted to unsigned integer types, returning a clear ValueError instead.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

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: 8fa0c8a3-b484-41d3-92ae-95b15504d891

📥 Commits

Reviewing files that changed from the base of the PR and between 9c064c1 and 9176b4a.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_socket.py is excluded by !Lib/**
📒 Files selected for processing (2)
  • crates/stdlib/src/socket.rs
  • crates/vm/src/builtins/int.rs

📝 Walkthrough

Walkthrough

The socket byte-order helpers now accept Python objects and perform VM-based integer conversion. Unsigned conversions explicitly reject negative integers with a ValueError.

Changes

Socket conversion behavior

Layer / File(s) Summary
Reject negative unsigned conversions
crates/vm/src/builtins/int.rs
Unsigned primitive conversion now raises ValueError for negative PyInt values.
Update socket byte-order helpers
crates/stdlib/src/socket.rs
htonl, htons, ntohl, and ntohs use Python-exposed VM conversion and return PyResult values.

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

🚥 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 accurately summarizes the main change: aligning socket byte-order conversion error behavior with CPython.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

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

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

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

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
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