{"id":5855,"date":"2014-04-07T09:39:49","date_gmt":"2014-04-07T09:39:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/confusing-context-manager-behavior-collection-of-common-programming-errors\/"},"modified":"2014-04-07T09:39:49","modified_gmt":"2014-04-07T09:39:49","slug":"confusing-context-manager-behavior-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/confusing-context-manager-behavior-collection-of-common-programming-errors\/","title":{"rendered":"Confusing context manager behavior-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve been playing around with making my own context managers in Python. I&#8217;m seeing some strange behavior most likely due to my implementation.<\/p>\n<p>I see the <code>__exit__<\/code> code called before a statement in the &#8216;with&#8217; context. For example, here is the code snippet and it&#8217;s exception:<\/p>\n<pre><code>with ProgressBar(10) as p:\n  p.update(1)\n<\/code><\/pre>\n<p>Traceback (most recent call last): File &#8220;&#8221;, line 3, in AttributeError: &#8216;NoneType&#8217; object has no attribute &#8216;update&#8217;<\/p>\n<p>I put debug in all the <code>__enter__<\/code>, <code>__exit__<\/code>, and update methods of my context manager. It looks like <code>__exit__<\/code> is called before update(). This makes no sense so I must be missing something simple.<\/p>\n<p>Here is my simple context manager class:<\/p>\n<pre><code>class ProgressBar(object):\n    \"\"\"Progress bar that normalizes progress to [0 - 100] scale\"\"\"\n\n    def __init__(self, max_value):\n        \"\"\"Create progress bar with max_value\"\"\"\n\n        self._current_value = 0.0\n        self._completed_value = 100.0\n        self._max_value = float(max_value)\n        print 'init', max_value\n\n    def __enter__(self):\n        \"\"\"Start of context manager, 'with' statement\"\"\"\n\n        print 'enter'\n        self._current_value = 0.0\n\n    def __exit__(self, exc_type, exc_value, traceback):\n        \"\"\"Start of context manager, 'with' statement\"\"\"\n\n        print 'exit'\n        self._current_value = self._completed_value\n\n        # Not handling any exceptions, so they'll be raised automatically\n        # To ignore exceptions return True or inspect arguments to handle\n\n        return False\n\n    def update(self, value):\n        \"\"\"Update progress value\"\"\"\n\n        print 'update'\n        if value &gt;= self._max_value:\n            self._current_value = 100\n        else:\n            self._current_value = (value \/ self._max_value) * self._completed_value\n\n        print '\\r%s' % (self._current_value),\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been playing around with making my own context managers in Python. I&#8217;m seeing some strange behavior most likely due to my implementation. I see the __exit__ code called before a statement in the &#8216;with&#8217; context. For example, here is the code snippet and it&#8217;s exception: with ProgressBar(10) as p: p.update(1) Traceback (most recent call [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5855","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5855"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5855\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}