简体   繁体   中英

Invalid syntax while running test on Travis

I'm having a problem with Travis on every commit. My tests work on local but on Travis I get this error:

Traceback (most recent call last):
  File "/opt/python/3.2.5/lib/python3.2/unittest/case.py", line 370, in _executeTestPart
    function()
  File "/opt/python/3.2.5/lib/python3.2/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: test.test_parser
Traceback (most recent call last):
  File "/opt/python/3.2.5/lib/python3.2/unittest/loader.py", line 261, in _find_tests
    module = self._get_module_from_name(name)
  File "/opt/python/3.2.5/lib/python3.2/unittest/loader.py", line 239, in _get_module_from_name
    __import__(name)
  File "/home/travis/build/davidmogar/genderator/test/test_parser.py", line 5, in <module>
    import genderator
  File "/home/travis/build/davidmogar/genderator/genderator/__init__.py", line 3, in <module>
    from genderator.parser import Parser
  File "/home/travis/build/davidmogar/genderator/genderator/parser.py", line 5, in <module>
    from .utils import Normalizer
  File "/home/travis/build/davidmogar/genderator/genderator/utils.py", line 63
    u'\N{COMBINING TILDE}'
                        ^
SyntaxError: invalid syntax

Here is the code where that line is:

def remove_accent_marks(text):
        good_accents = {
            u'\N{COMBINING TILDE}',
            u'\N{COMBINING CEDILLA}'
        }

        return ''.join(c for c in unicodedata.normalize('NFKD', text)
                       if unicodedata.category(c) != 'Mn' or c in good_accents)

I have no idea about what is the problem because as I've said, all test are working in local. Here is my .travis.yml file:

language: python
python:
  - "3.2"
  - "3.3"
  - "3.4"
script: python -m unittest discover

Any idea?

The u'...' syntax in Python 3 is only supported in Python 3.3 and up .

The u prefix is only there to support polyglot Python code (supporting both 2 and 3), and can be safely removed if you don't need to support Python 2.

If you need to support both Python 2 and 3.2, you'll have to use a different approach. You could use a from __future__ import to make all string literals in Python 2 produce unicode string objects; this applies per module:

from __future__ import unicode_literals

def remove_accent_marks(text):
    good_accents = {
        '\N{COMBINING TILDE}',
        '\N{COMBINING CEDILLA}'
    }

The strings will be treated as Unicode in both Python 2 and 3.

Or you could create your own polyglot function:

import sys

if sys.version_info[0] < 3:
    u = lambda s: unicode(s.replace(r'\\', r'\\\\'), "unicode_escape")
else:
    u = lambda s: s

and use that on all your Unicode strings:

def remove_accent_marks(text):
    good_accents = {
        u('\N{COMBINING TILDE}'),
        u('\N{COMBINING CEDILLA}')
    }

or you can use the six library to produce that bridge for you:

import six

def remove_accent_marks(text):
    good_accents = {
        six.u('\N{COMBINING TILDE}'),
        six.u('\N{COMBINING CEDILLA}')
    }

You may want to read the Python Porting HOWTO .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM