New submission from Bruno P. Kinoshita <brunodepau...@yahoo.com.br>:

Hi,

Tested on Python 3.7 on Ubuntu LTS. But I believe this affects previous 
versions too, as found it from an issue posted in Jinja2 in 2013: 
https://github.com/pallets/jinja/issues/276

This code simulates the issue where the traceback has the wrong line number 
(without using any Jinja2 code):

```
class Z(object):
    def __str__(self):
        raise ValueError('Z error')


def raise_generator():
    yield 'three values are: %s %s %s' % (
        'primeiro',
        Z(),  # traceback must point to this lineno 9
        'terceiro'  # but points to this lineno 10 (__str__ only, __eq__ is OK)
    )


print(list(raise_generator()))
```

The output:

```
Traceback (most recent call last):
  File 
"/home/kinow/Development/python/workspace/cylc-flow/cylc/flow/tests/Z.py", line 
14, in <module>
    print(list(raise_generator()))
  File 
"/home/kinow/Development/python/workspace/cylc-flow/cylc/flow/tests/Z.py", line 
10, in raise_generator
    'terceiro'  # but points to this lineno 10 (__str__ only, __eq__ is OK)
  File 
"/home/kinow/Development/python/workspace/cylc-flow/cylc/flow/tests/Z.py", line 
3, in __str__
    raise ValueError('Z error')
ValueError: Z error
```

Jinja uses something similar to the class Z to raise errors when the template 
has undefined variables. The curious part is that if instead of simply 
formatting the string with "%s" % (Z()) you use "%s" % (str(Z())) or if you 
callZ().__str__(), then the traceback reports the correct line number.

Not sure if intentional, but would be nice if the traceback reported the 
correct line number, and I think other applications could end up having the 
same issue.

This is my first issue, so I apologize if I did not include enough information, 
or if there is anything missing in this ticket. Let me know if I need to update 
it with more information or formatting the code.

Would be happy to work on this if this is considered an easy-to-fix issue.

Thanks all for your work on Python,

Bruno P. Kinoshita

----------
components: Interpreter Core
messages: 348277
nosy: kinow
priority: normal
severity: normal
status: open
title: Wrong lineno in traceback when formatting strings with % and multilines
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37647>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to