Skip to content

_socket: socket.sethostname() reports a failed buffer export as an OS error, raising SystemError #161665

Open

Description

@theosotr

Bug report

Summary

socket.sethostname() accepts a bytes subclass, and if getting a buffer from that object fails, the error is not reported. Instead SystemError is returned to caller.

SystemError: <class 'OSError'> returned a result with an exception set

Reproducer

import os
import socket

class B(bytes):
    def __buffer__(self, flags):
        raise KeyError("boom")

try:
    os.stat("/nonexistent")
except OSError:
    pass

socket.sethostname(B(b"h"))
Traceback (most recent call last):
  File "min.py", line 23, in <module>
    socket.sethostname(B(b"h"))
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^
SystemError: <class 'OSError'> returned a result with an exception set

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    topic-socketRelated to the socket, socketserver and selectors modules
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtopic-socketRelated to the socket, socketserver and selectors modulestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions