{"record":{"id":"71cf235af2a6dc09","repo":"python/cpython","slug":"write-argument-must-be-str-not-type-s-name-71cf23","errorCode":null,"errorMessage":"write() argument must be str, not {type(s).__name__}","messagePattern":"write\\(\\) argument must be str, not (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"warning","filePath":"Lib/_apple_support.py","lineNumber":24,"sourceCode":"    # Redirect stdout and stderr to the Apple system log. This method is\n    # invoked by init_apple_streams() (initconfig.c) if config->use_system_logger\n    # is enabled.\n    sys.stdout = SystemLog(log_write, stdout_level, errors=sys.stderr.errors)\n    sys.stderr = SystemLog(log_write, stderr_level, errors=sys.stderr.errors)\n\n\nclass SystemLog(io.TextIOWrapper):\n    def __init__(self, log_write, level, **kwargs):\n        kwargs.setdefault(\"encoding\", \"UTF-8\")\n        kwargs.setdefault(\"line_buffering\", True)\n        super().__init__(LogStream(log_write, level), **kwargs)\n\n    def __repr__(self):\n        return f\"<SystemLog (level {self.buffer.level})>\"\n\n    def write(self, s):\n        if not isinstance(s, str):\n            raise TypeError(\n                f\"write() argument must be str, not {type(s).__name__}\")\n\n        # In case `s` is a str subclass that writes itself to stdout or stderr\n        # when we call its methods, convert it to an actual str.\n        s = str.__str__(s)\n\n        # We want to emit one log message per line, so split\n        # the string before sending it to the superclass.\n        for line in s.splitlines(keepends=True):\n            super().write(line)\n\n        return len(s)\n\n\nclass LogStream(io.RawIOBase):\n    def __init__(self, log_write, level):\n        self.log_write = log_write\n        self.level = level","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_apple_support.py#L6-L42","documentation":"Debug-mode guard in BaseSelectorEventLoop.sock_recvfrom_into(): before performing an asynchronous datagram receive into a provided buffer, the loop checks that the socket is non-blocking when debug mode is enabled. Blocking sockets would hang the loop inside recvfrom_into, so the guard converts that into an immediate ValueError during development.","triggerScenarios":"await loop.sock_recvfrom_into(sock, buf, nbytes) with debug on and sock.gettimeout() != 0, covering both fully blocking sockets (timeout None) and those with explicit timeouts left from sync usage.","commonSituations":"Converting sync UDP proxy code that used recvfrom_into with settimeout for reliability; benchmark tools reusing capture sockets; debug-mode-enabled test matrices surfacing previously silent blocking state.","solutions":["Set sock.setblocking(False) before the first await on sock_recvfrom_into","Migrate socket construction to an async-aware factory","Where timeouts are genuinely wanted, implement them with asyncio.wait_for around the await instead of socket timeouts"],"exampleFix":"# before\nsock.settimeout(2.0)\nn, addr = await loop.sock_recvfrom_into(sock, buf)  # debug: ValueError\n# after\nsock.setblocking(False)\nn, addr = await asyncio.wait_for(loop.sock_recvfrom_into(sock, buf), 2.0)","handlingStrategy":"validation","validationCode":"def nb(sock):\n    if sock.gettimeout() != 0:\n        sock.setblocking(False)\n    return sock\n\nn, addr = await asyncio.wait_for(loop.sock_recvfrom_into(nb(sock), buf), 2.0)","typeGuard":null,"tryCatchPattern":"try:\n    await loop.sock_recvfrom_into(sock, buf, n)\nexcept ValueError as e:\n    if 'non-blocking' in str(e):\n        sock.setblocking(False)\n        return await loop.sock_recvfrom_into(sock, buf, n)\n    raise","preventionTips":["Non-blocking first, await second — make it a rule for datagram buffers","Replace sync settimeout with asyncio.wait_for bounds","Keep one socket factory enforcing loop readiness","Guard shared pools with a gettimeout() == 0 assertion"],"tags":["asyncio","udp","socket","debug-mode","buffer"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}