Title: Context Managers in Python: Python __exit__ Returning True
Open Graph Title: Context Managers in Python: Python __exit__ Returning True
Description: A single `return True` in __exit__ caused 5% data loss by swallowing IntegrityError silently. How to audit exception suppression and test with injection.
Open Graph Description: A single `return True` in __exit__ caused 5% data loss by swallowing IntegrityError silently. How to audit exception suppression and test with injection.
Opengraph URL: https://thecodeforge.io/python/context-managers-python/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/python/context-managers-python/"
},
"headline": "Python __exit__ Returning True — The Silent Bug Pattern",
"description": "A single `return True` in __exit__ caused 5% data loss by swallowing IntegrityError silently. How to audit exception suppression and test with injection.",
"keywords": "Python __exit__ Returning True — The Silent Bug Pattern, Python, Advanced, Exception Handling, A single `return True` in __exit__ caused 5% data loss by swallowing IntegrityError silently. How to audit exception suppression and test with injection.",
"proficiencyLevel": "Expert",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-05T00:00:00+00:00",
"dateModified": "2026-09-23T00:00:00+00:00",
"url": "https://thecodeforge.io/python/context-managers-python/",
"inLanguage": "en",
"educationalLevel": "Advanced",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Python", "item": "https://thecodeforge.io/python/"},
{"@type": "ListItem", "position": 3, "name": "Python __exit__ Returning True — The Silent Bug Pattern", "item": "https://thecodeforge.io/python/context-managers-python/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is a context manager in Python in simple terms?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Think of a context manager as a wrapper around a resource that ensures setup happens before you use it and cleanup happens after, no matter what. The `with` statement is how you invoke it."
}
},
{
"@type": "Question",
"name": "Can I use a context manager without a class?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, use the @contextmanager decorator from the contextlib module. It turns a generator function into a context manager, reducing boilerplate."
}
},
{
"@type": "Question",
"name": "What happens if I return True from __exit__ without handling the exception?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The exception is suppressed — the program continues as if nothing happened. This is dangerous; you should only suppress exceptions you've explicitly handled and logged."
}
},
{
"@type": "Question",
"name": "What is the difference between contextlib.suppress and a try/except?",
"acceptedAnswer": {
"@type": "Answer",
"text": "contextlib.suppress is a context manager that suppresses specific exceptions within its block. It's syntactic sugar for a try/except with pass, but it only suppresses the listed exceptions, not all."
}
},
{
"@type": "Question",
"name": "How do I use multiple context managers in one with statement?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use comma separation: `with open('a') as a, open('b') as b:`. Python 3.1+ allows this. For an unknown number, use ExitStack."
}
},
{
"@type": "Question",
"name": "What should I do if my context manager fails to clean up when an exception occurs?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Check that __exit__ is called correctly and doesn't suppress the exception. Use a finally block or ensure __exit__ always performs cleanup before returning. Add logging and test with exception injection."
}
},
{
"@type": "Question",
"name": "How do async context managers differ from sync ones?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Async context managers use __aenter__ and __aexit__ coroutines instead of regular methods. They are used with `async with`. The same exception handling rules apply, but you must remember to make __aexit__ a coroutine and await any async cleanup."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large