0

I'm a Python beginner, for school, and I am a little pernickety. My teacher wants me to write a function, returning a sentence with an accent, "print()" show me the good characters, with the accent, but the doctest doesn't.

Here is my code :

def test() :
   """
   >>> test()
   à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û - ü - ÿ - ç
   """
   print("à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û - ü - ÿ - ç")

import doctest
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS, verbose = True)

As I said, print does show me properly the characters.

And here is the doctest, the issue :

Trying:
test()
Expecting:
\xe0 - \xe2 - \xe4 - \xe9 - \xe8 - \xea - \xeb - \xef - \xee - \xf4 - \xf6 - \xf9 - \xfb - \xfc - \xff - \xe7
ok

The test is passed, without a fail, but I really want the Doctest to read those characters without showing the Unicode Hex Character.

How could I fix this?

PS: My teacher use the IDE Thonny, so I naturally followed him, and I know he won't blame me (us, because my mates didn't search further and just change the 'é' to an 'e').

halfer
  • 19,824
  • 17
  • 99
  • 186
Sidimoth
  • 1
  • 1
  • Where do you run it? I tried in a console on Python2/3 in Pycharm and all chars are correctly displayed, not Unicode code. – amarynets Sep 29 '17 at 17:28
  • There is nothing wrong with the test or the doctest runner's behaviour here. You just need to run it in a terminal which renders these glyphs correctly. I have never heard of "IDE Thonny", but I assume it's that thing's fault. – wim Sep 29 '17 at 17:28
  • @ AndMar I run it in the IDE the teacher is using: Thonny, in a console it seems to work, but I didn't try further. I'll try today I think, if I have time to . (Sry, I reply to both of you in the "answer" below, even if I actually tell more about my issue ^^") – Sidimoth Oct 01 '17 at 08:16

1 Answers1

0

So, indeed, Thonny is a part of the issue. If I try in Thonny to print a sentence with an accent, the doctest try it and even if he passes the test, he still print an error, BUT python still print the good sentence.

So I tested a lot of things, and on my Ubuntu Laptop, with Python 3.6, I noticed that if my print() printed more than a sentence, there was a problem.

This is the test code :

def test():
   """
   >>> test()
   é é é
   """
   print('é é é')

And he print :

Trying:
test()
Expecting:
é é é
ok
1 items had no tests:
__main__
1 items passed all tests:
1 tests in __main__.test
1 tests in 2 items.
1 passed and 0 failed.
Test passed.

And if I add a variable in the print, this is what I have :

def test(var):
"""
>>> test(5)
é é é 5
"""
print('é é é', var)

doctest:

Trying:
test(5)
Expecting:
é é é 5
**********************************************************************
File "test.py", line 5, in __main__.test
Failed example:
test(5)
Expected:
é é é 5
Got:
('\xc3\xa9 \xc3\xa9 \xc3\xa9', 5)
1 items had no tests:
__main__
**********************************************************************
1 items had failures:
1 of 1 in __main__.test
1 tests in 2 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.

I have the feeling that I'm misusing the print in Python, and I really don't know why, I searched on Internet and found some solutions like "print("%s", name)" to print a variable IN the print, but when I tried it shows me the "%s" instead of replacing it by the string. I guess I found documentation about Python 2.x instead of 3.x.

So, if I gave you more information about my problem, or if you have the solution, I would be happy to learn why the doctest do this. Anyway, as I said, my teacher won't count this as an error, so it's not an important issue that I have to solve with a deadline, just a problem I focus on.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sidimoth
  • 1
  • 1
  • FYI this is being tracked as bug at https://bitbucket.org/plas/thonny/issues/352/problem-with-non-ascii-chars-and-doctest – bugmenot123 Nov 10 '17 at 13:11