{
  "id": "python/unittest-teardown-not-called-on-skip",
  "signature": "ResourceWarning: unclosed file <_io.TextIOWrapper name='/tmp/test.log' mode='w' encoding='UTF-8'>",
  "signature_zh": "ResourceWarning: 未关闭的文件 <_io.TextIOWrapper name='/tmp/test.log' mode='w' encoding='UTF-8'>",
  "regex": "ResourceWarning:\\ unclosed\\ file\\ <_io\\.TextIOWrapper\\ name='/tmp/test\\.log'\\ mode='w'\\ encoding='UTF\\-8'>",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A test calls self.skipTest() inside setUp after opening a resource, so tearDown never runs and the resource leaks; or setUpClass raised and tearDownClass is skipped.",
  "root_cause_type": "generic",
  "root_cause_zh": "测试在 setUp 中打开资源后调用 self.skipTest()，导致 tearDown 从未运行、资源泄漏；或 setUpClass 抛出异常后 tearDownClass 被跳过。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Hides the leak; file descriptors accumulate across a large test suite and eventually cause EMFILE.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Skips before any setup runs, but other tests that do open resources still leak.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "class TestX(unittest.TestCase):\n    def setUp(self):\n        self.f = open('/tmp/test.log', 'w')\n        self.addCleanup(self.f.close)\n        if not condition:\n            self.skipTest('condition not met')",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.92,
      "how": "def test_x(self):\n    with open('/tmp/test.log', 'w') as f:\n        ...\n    # file closed even if test fails",
      "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.83,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-07-30",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}