Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bob Tests produces different results between 2/3 #123

Closed
AstraLuma opened this issue Sep 30, 2014 · 7 comments
Closed

Bob Tests produces different results between 2/3 #123

AstraLuma opened this issue Sep 30, 2014 · 7 comments

Comments

@AstraLuma
Copy link

Under Python 2: 'ÜMLäÜTS!'.isupper() isTrue, butu'ÜMLäÜTS!'.isupper()isFalse`.

Under Python 3: 'ÜMLäÜTS!'.isupper() is False

The problem is that the string is using precomposed characters and under Python 2, str is encoded in the source code encoding of the tests (UTF-8).

@sjakobi
Copy link
Contributor

sjakobi commented Sep 30, 2014

The Bob test suite uses unicode_literals to make sure that all strings are interpreted as unicode.

@sjakobi
Copy link
Contributor

sjakobi commented Oct 1, 2014

Have you run into some actual problems with the test suite? Unexpected test result?

Otherwise I'll close this.

@AstraLuma
Copy link
Author

There was a discussion in one of the submissions where they had to use str.decode() with str.isupper() and this was the only way I could figure out to reproduce the behavior.

@sjakobi
Copy link
Contributor

sjakobi commented Oct 1, 2014

It would be super helpful if you could track down that submission or reproduce it!

Otherwise, maybe @kytrinyx can help us find it?

@sjakobi
Copy link
Contributor

sjakobi commented Oct 9, 2014

I'm closing this for now but I'd be happy to discuss (and try to fix) this if the bug can actually be reproduced in a supported version of Python (2.7, 3.3, 3.4).

@sjakobi sjakobi closed this as completed Oct 9, 2014
@englishm
Copy link

I ran into this issue while trying to demonstrate Exercism to a student new to programming. It was an unfortunate hurdle to encounter so early in the process.

There is certainly value in understanding the intricacies of Python unicode handling (and differences between 2 and 3), but I don't think Exercism's hello-world exercise is the right place to be confronted with those things.

$ python2.7 hello_world_test.py                                                        
.E.
======================================================================
ERROR: test_hello_with_umlaut_name (__main__.BobTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "hello_world_test.py", line 26, in test_hello_with_umlaut_name
    hello_world.hello('Jürgen')
  File "/Users/english/exercism/python/hello-world/hello_world.py", line 8, in hello
    return "Hello, {}!".format(name)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 1: ordinal
 not in range(128)                                                                    

----------------------------------------------------------------------
Ran 3 tests in 0.000s

FAILED (errors=1)
$ python3 hello_world_test.py 
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
$ 

For hello_world.py:

def hello(name=''):
    if name != '':
        return "Hello, {}!".format(name)
    else:
      return 'Hello, World!'

(Very nearly the current example.py, so the results should be the same)

And hello_world_test.py:

# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import unittest

import hello_world


class BobTests(unittest.TestCase):

    def test_hello_without_name(self):
        self.assertEqual(
            'Hello, World!',
            hello_world.hello()
        )

    def test_hello_with_name(self):
        self.assertEqual(
            'Hello, Jane!',
            hello_world.hello('Jane')
        )

    def test_hello_with_umlaut_name(self):
        self.assertEqual(
            'Hello, Jürgen!',
            hello_world.hello('Jürgen')
        )

if __name__ == '__main__':
    unittest.main()

Note that 2.7.x is still the default Python installed on many platforms, including macOS:

$ /usr/bin/python --version
Python 2.7.10

@kytrinyx
Copy link
Member

@englishm I've opened a new issue in the Python track about this: #1353

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants