{"record":{"id":"e299119551b731f7","repo":"RustPython/RustPython","slug":"binary-mode-doesn-t-take-a-newline-argument","errorCode":null,"errorMessage":"binary mode doesn't take a newline argument","messagePattern":"binary mode doesn't take a newline argument","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":227,"sourceCode":"    creating = \"x\" in modes\n    reading = \"r\" in modes\n    writing = \"w\" in modes\n    appending = \"a\" in modes\n    updating = \"+\" in modes\n    text = \"t\" in modes\n    binary = \"b\" in modes\n    if text and binary:\n        raise ValueError(\"can't have text and binary mode at once\")\n    if creating + reading + writing + appending > 1:\n        raise ValueError(\"can't have read/write/append mode at once\")\n    if not (creating or reading or writing or appending):\n        raise ValueError(\"must have exactly one of read/write/append mode\")\n    if binary and encoding is not None:\n        raise ValueError(\"binary mode doesn't take an encoding argument\")\n    if binary and errors is not None:\n        raise ValueError(\"binary mode doesn't take an errors argument\")\n    if binary and newline is not None:\n        raise ValueError(\"binary mode doesn't take a newline argument\")\n    if binary and buffering == 1:\n        import warnings\n        warnings.warn(\"line buffering (buffering=1) isn't supported in binary \"\n                      \"mode, the default buffer size will be used\",\n                      RuntimeWarning, 2)\n    raw = FileIO(file,\n                 (creating and \"x\" or \"\") +\n                 (reading and \"r\" or \"\") +\n                 (writing and \"w\" or \"\") +\n                 (appending and \"a\" or \"\") +\n                 (updating and \"+\" or \"\"),\n                 closefd, opener=opener)\n    result = raw\n    try:\n        line_buffering = False\n        if buffering == 1 or buffering < 0 and raw._isatty_open_only():\n            buffering = -1\n            line_buffering = True","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_pyio.py#L209-L245","documentation":"ValueError raised when newline is passed together with a binary mode. newline controls line-ending translation ('\\n' vs os.linesep), which is a text-layer concept; binary streams must see bytes exactly as they are. Notably the common csv recipe open(path, 'w', newline='') is text-mode and legal - the error only appears when 'b' is combined with a non-None newline.","triggerScenarios":"open(path, 'wb', newline='') copied from the csv.writer recipe after switching to binary; wrappers that always pass newline='' to avoid universal-newline surprises; a commit that flipped 'w' to 'wb' while leaving newline in place.","commonSituations":"CSV/text exporters migrated to binary formats; a shared open-kwargs dict reused across text and binary paths; documentation examples adapted incompletely.","solutions":["Remove newline from binary opens","Keep newline='' only with text modes: open(path, 'w', newline='', encoding='utf-8')","Strip text-only keys from shared kwargs when 'b' in mode"],"exampleFix":"# before\nwith open(path, 'wb', newline='') as f:\n    writer = csv.writer(f)      # binary + newline -> ValueError\n\n# after\nwith open(path, 'w', newline='', encoding='utf-8') as f:\n    writer = csv.writer(f)","handlingStrategy":"validation","validationCode":"def open_any(path, mode='r', newline=None):\n    kwargs = {} if 'b' in mode else ({'newline': newline} if newline is not None else {})\n    return open(path, mode, **kwargs)","typeGuard":"def takes_text_kwargs(mode) -> bool:\n    return 'b' not in mode","tryCatchPattern":null,"preventionTips":["newline='' belongs to text-mode csv usage only","When flipping 'w' to 'wb', remove newline from the same call","Keep per-call-site kwargs instead of one global open-kwargs dict"],"tags":["io","open","valueerror","newline","binary-io","csv","argument-conflict"],"backgroundTag":"incompatible-open-arguments","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}