[issue12503] "with" statement error message is more confusing in Py2.7

Ismael Garrido report at bugs.python.org
Wed Jul 6 05:58:03 CEST 2011


New submission from Ismael Garrido <ismaelgfk at gmail.com>:

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 at 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 at chaos:~/Escritorio$ python2.7 bad.py 
Traceback (most recent call last):
  File "bad.py", line 8, in <module>
    with Timer:
AttributeError: __exit__

----------
components: Interpreter Core
messages: 139918
nosy: Ismael.Garrido
priority: normal
severity: normal
status: open
title: "with" statement error message is more confusing in Py2.7
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12503>
_______________________________________


More information about the Python-bugs-list mailing list