{
  "id": "python/pytest-capture-output-binary",
  "signature": "TypeError: a bytes-like object is required, not 'str' in capsys.readouterr()",
  "signature_zh": "类型错误：需要类似字节的对象，而不是 'str'，在 capsys.readouterr() 中",
  "regex": "TypeError:\\ a\\ bytes\\-like\\ object\\ is\\ required,\\ not\\ 'str'\\ in\\ capsys\\.readouterr\\(\\)",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Using capsys (which captures text) to capture binary output from sys.stdout.buffer, causing a type mismatch when the code writes bytes.",
  "root_cause_type": "generic",
  "root_cause_zh": "使用 capsys（捕获文本）来捕获来自 sys.stdout.buffer 的二进制输出，当代码写入字节时导致类型不匹配。",
  "versions": [
    {
      "version": "3.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Using capfd instead of capsys may work for binary but fails if the test also needs to capture text from sys.stdout.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Decoding bytes to str before assertion may mask encoding issues in the output.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use capfd (capture file descriptors) instead of capsys to capture both text and binary output: out, err = capfd.readouterr()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "If the output is binary, use sys.stdout.buffer.write() and capture with capfd, then decode as needed.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.82,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2026-02-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}