| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
""" |
| 3 |
MAYBE: DUPLICATES: #449 |
| 4 |
NOTE: traceback2 (backport for Python2) solves the problem. |
| 5 |
|
| 6 |
def foo(stop): |
| 7 |
raise Exception(u"по русски") |
| 8 |
|
| 9 |
Result: |
| 10 |
|
| 11 |
File "features/steps/steps.py", line 8, in foo |
| 12 |
raise Exception(u"по ����ки") <-- This is not |
| 13 |
Exception: по русски <-- This is OK |
| 14 |
|
| 15 |
It happens here (https://github.com/behave/behave/blob/master/behave/model.py#L1299) |
| 16 |
because traceback.format_exc() creates incorrect text. |
| 17 |
You then convert it using _text() and result is also bad. |
| 18 |
|
| 19 |
To fix it, you may take e.message which is correct and traceback.format_tb(sys.exc_info()[2]) |
| 20 |
which is also correct. |
| 21 |
""" |
| 22 |
|
| 23 |
from __future__ import print_function |
| 24 |
from behave.textutil import text |
| 25 |
from hamcrest.core import assert_that, equal_to |
| 26 |
from hamcrest.library import contains_string |
| 27 |
import six |
| 28 |
import pytest |
| 29 |
if six.PY2: |
| 30 |
import traceback2 as traceback |
| 31 |
else: |
| 32 |
import traceback |
| 33 |
|
| 34 |
|
| 35 |
def problematic_step_impl(context): |
| 36 |
raise Exception(u"по русски") |
| 37 |
|
| 38 |
@pytest.mark.parametrize("encoding", [None, "UTF-8", "unicode_escape"]) |
| 39 |
def test_issue(encoding): |
| 40 |
""" |
| 41 |
with encoding=UTF-8: |
| 42 |
File "/Users/jens/se/behave_main.unicode/tests/issues/test_issue0453.py", line 31, in problematic_step_impl |
| 43 |
raise Exception(u"по русски") |
| 44 |
Exception: \u043f\u043e \u0440\u0443\u0441\u0441\u043a\u0438 |
| 45 |
|
| 46 |
with encoding=unicode_escape: |
| 47 |
File "/Users/jens/se/behave_main.unicode/tests/issues/test_issue0453.py", line 31, in problematic_step_impl |
| 48 |
raise Exception(u"по ÑÑÑÑки") |
| 49 |
Exception: по русски |
| 50 |
""" |
| 51 |
context = None |
| 52 |
text2 = "" |
| 53 |
expected_text = u"по русски" |
| 54 |
try: |
| 55 |
problematic_step_impl(context) |
| 56 |
except Exception: |
| 57 |
text2 = traceback.format_exc() |
| 58 |
|
| 59 |
text3 = text(text2, encoding) |
| 60 |
print(u"EXCEPTION-TEXT: %s" % text3) |
| 61 |
assert_that(text3, contains_string(u'raise Exception(u"по русски"')) |
| 62 |
assert_that(text3, contains_string(u"Exception: по русски")) |