Confusing context manager behavior-Collection of common programming errors

I’ve been playing around with making my own context managers in Python. I’m seeing some strange behavior most likely due to my implementation.

I see the __exit__ code called before a statement in the ‘with’ context. For example, here is the code snippet and it’s exception:

with ProgressBar(10) as p:
  p.update(1)

Traceback (most recent call last): File “”, line 3, in AttributeError: ‘NoneType’ object has no attribute ‘update’

I put debug in all the __enter__, __exit__, and update methods of my context manager. It looks like __exit__ is called before update(). This makes no sense so I must be missing something simple.

Here is my simple context manager class:

class ProgressBar(object):
    """Progress bar that normalizes progress to [0 - 100] scale"""

    def __init__(self, max_value):
        """Create progress bar with max_value"""

        self._current_value = 0.0
        self._completed_value = 100.0
        self._max_value = float(max_value)
        print 'init', max_value

    def __enter__(self):
        """Start of context manager, 'with' statement"""

        print 'enter'
        self._current_value = 0.0

    def __exit__(self, exc_type, exc_value, traceback):
        """Start of context manager, 'with' statement"""

        print 'exit'
        self._current_value = self._completed_value

        # Not handling any exceptions, so they'll be raised automatically
        # To ignore exceptions return True or inspect arguments to handle

        return False

    def update(self, value):
        """Update progress value"""

        print 'update'
        if value >= self._max_value:
            self._current_value = 100
        else:
            self._current_value = (value / self._max_value) * self._completed_value

        print '\r%s' % (self._current_value),