This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Ismael.Garrido
Recipients Ismael.Garrido
Date 2011-07-06.03:58:02
SpamBayes Score 2.0117352e-11
Marked as misclassified No
Message-id <1309924683.37.0.310972531328.issue12503@psf.upfronthosting.co.za>
In-reply-to
Content
Using the "with" statement wrongly results in a confusing error message.

Code (originally written by Alex Gaynor):

class Timer(object):
    def __enter__(self):
        self.start = time.time()
    def __exit__(self, exc_type, exc_val, tb):
        print "Section time: ", time.time() - self.start

#Note the error here, I call the class, not an instance
with Timer:
    pass


------------------------


Compare the Python 2.6 error:

ismael@chaos:~/Escritorio$ python bad.py 
Traceback (most recent call last):
  File "bad.py", line 8, in <module>
    with Timer:
TypeError: unbound method __enter__() must be called with Timer instance as first argument (got nothing instead)

Against Python 2.7:

ismael@chaos:~/Escritorio$ python2.7 bad.py 
Traceback (most recent call last):
  File "bad.py", line 8, in <module>
    with Timer:
AttributeError: __exit__
History
Date User Action Args
2011-07-06 03:58:03Ismael.Garridosetrecipients: + Ismael.Garrido
2011-07-06 03:58:03Ismael.Garridosetmessageid: <1309924683.37.0.310972531328.issue12503@psf.upfronthosting.co.za>
2011-07-06 03:58:02Ismael.Garridolinkissue12503 messages
2011-07-06 03:58:02Ismael.Garridocreate