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.
Related
I am using the PyYAML-3.10 as part of a Python program on macOS 10, using Python version 2.7.10. I am not able to make sense of these compilation errors. Since PyYAML-3.10 is a stable version of PyYAML, it should give no compilation errors. The errors are listed below. Any suggestions would be appreciated.
File "pyR#TE.py", line 3, in <module>
import yaml
File "/Users/PyR#TE/pyrate-1.0.0/yaml/__init__.py", line 8, in <module>
from .loader import *
File "/Users/PyR#TE/pyrate-1.0.0/yaml/loader.py", line 4, in <module>
from .reader import *
File "/Users/PyR#TE/pyrate-1.0.0/yaml/reader.py", line 45, in <module>
class Reader(object):
File "/Users/PyR#TE/pyrate-1.0.0/yaml/reader.py", line 137, in Reader
NON_PRINTABLE = re.compile('[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD]')
raise error, v # invalid expression
sre_constants.error: bad character range
It seems that PyYAMP-3.10 is not compatible with Python 2. (Did you mean "PyYAML", by the way? I could not find a reference to a "PyYAMP" package anywhere.) The compilation error you are seeing is from re.compile - when Python is trying to compile a regular expression.
I tried using the line in your error message containing re.compile in Python 2 and Python 3.
Python 2:
>>> import re
>>> re.compile('[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD]')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/re.py", line 194, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py", line 251, in _compile
raise error, v # invalid expression
sre_constants.error: bad character range
>>>
Python 3:
>>> import re
>>> re.compile('[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD]')
re.compile('[^\t\n\r -~\x85\xa0-\ud7ff\ue000-�]')
>>>
So your options are either to find a package that supports Python 2, or to upgrade your code to Python 3. I recommend upgrading, as Python 2 is no longer supported.
I have the following Fortran code:
!routines.f90
module mymodule
contains
function add(a, b)
real(8), intent(in):: a
real(8), intent(in):: b
real(8) :: add
add = a + b
end function
end module
Instead of using the command: python -m numpy.f2py -m routines -c routines.f90, I want to compile from within a python script, as follows:
#main.py
import numpy as np
import numpy.f2py as f2py
with open(r'routines.f90', 'r') as f:
source = f.read()
f2py.compile(source, modulename='routines')
print('OK')
But when I try to execute this script: python main.py I get the following error:
Traceback (most recent call last):
File "main.py", line 7, in <module>
f2py.compile(source, modulename='routines')
File "/home/user1/anaconda3/lib/python3.6/site-packages/numpy/f2py/__init__.py", line 59, in compile
f.write(source)
File "/home/user1/anaconda3/lib/python3.6/tempfile.py", line 485, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
Could you please tell me what is the issue?
open(r'routines.f90', 'r') opens your file for reading text (a.k.a. str), but, apparently, f2py.compile requires that its first argument be of type bytes. To satisfy that, open your file in binary mode:
open(r'routines.f90', 'rb')
(Also, there's no need for the first r in r'routines...', you can just do 'routines.f90', although it doesn't change much).
I'm trying to use RestrictedPython(see documentation) with Python 3.6.
The following code yields different results in Python 2.7 and in 3.6:
from RestrictedPython import compile_restricted
from RestrictedPython.Guards import safe_builtins
restricted_globals = dict(__builtins__ = safe_builtins)
src = '''
open('/etc/passwd')
'''
code = compile_restricted(src, '<string>', 'exec')
exec(code) in restricted_globals
In Python 2.7, the result is as expected:
Traceback (most recent call last):
File "...Playground.py", line 10, in <module>
exec(code) in restricted_globals
File "<string>", line 2, in <module>
NameError: name 'open' is not defined
But in Python 3.6, the results is:
Traceback (most recent call last):
File "...Playground.py", line 10, in <module>
exec(code) in restricted_globals
File "<string>", line 2, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/etc/passwd'
The open name/method is now obviously known, which should not be the case. Why does this code behave different when used in 3.6 than when used in 2.7?
The way you should write your code for version 3.6 is presented here:
https://github.com/zopefoundation/RestrictedPython
Under "Problematic Code Example".
Namely, the following code will work, that is, open will be not defined:
from RestrictedPython import compile_restricted
from RestrictedPython import safe_builtins
source_code = """
open('/etc/passwd')
"""
byte_code = compile_restricted(source_code, '<inline>', 'exec')
exec(byte_code, {'__builtins__': safe_builtins}, {})
As for the reason why this happens, I don't have one, but I wanted to present a way of making it work anyways.
I have a litany of unit tests that are run on Travis CI and only on PY3.2 it goes belly up. How can I solve this without using six.u()?
def test_parse_utf8(self):
s = String("foo", 12, encoding="utf8")
self.assertEqual(s.parse(b"hello joh\xd4\x83n"), u"hello joh\u0503n")
======================================================================
ERROR: Failure: SyntaxError (invalid syntax (test_strings.py, line 37))
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/virtualenv/python3.2.5/lib/python3.2/site-packages/nose/failure.py", line 39, in runTest
raise self.exc_val.with_traceback(self.tb)
File "/home/travis/virtualenv/python3.2.5/lib/python3.2/site-packages/nose/loader.py", line 414, in loadTestsFromName
addr.filename, addr.module)
File "/home/travis/virtualenv/python3.2.5/lib/python3.2/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/home/travis/virtualenv/python3.2.5/lib/python3.2/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/home/travis/build/construct/construct/tests/test_strings.py", line 37
self.assertEqual(s.build(u"hello joh\u0503n"), b"hello joh\xd4\x83n")
^
SyntaxError: invalid syntax
Trying to get this to work:
PY3 = sys.version_info[0] == 3
def u(s): return s if PY3 else s.decode("utf-8")
self.assertEqual(s.parse(b"hello joh\xd4\x83n"), u("hello joh\u0503n"))
Quote from https://pythonhosted.org/six/
On Python 2, u() doesn’t know what the encoding of the literal is.
Each byte is converted directly to the unicode codepoint of the same
value. Because of this, it’s only safe to use u() with strings of
ASCII data.
But the whole point of using unicode is to not be restricted to ASCII.
I think you're out of luck here.
Either use six.u() or drop support for Python 3.2.
Could you instead do from __future__ import unicode_literals and not use the u syntax anywhere?
from __future__ import unicode_literals makes string literals without a preceding u in earlier versions of Python act as in Python 3, that is default to unicode. So if you do from __future__ import unicode_literals and change all u"strings" to "strings", your string literals will be unicode in all versions. This will not affect b literals.
I taken the implementation of six.u() and discarded six.
import sys
PY3 = sys.version_info[0] == 3
def u(s): return s if PY3 else unicode(s.replace(r'\\', r'\\\\'), "unicode_escape")
I'm using Pyshark and Python 2.6 on OS X 10.10. I simply try to import pyshark in my code, and this error is thrown. Any idea of what could be going wrong?
/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 "/Users/spencergardner/Google Drive/development/python-sockets/sniff.py"
Traceback (most recent call last):
File "/Users/spencergardner/Google Drive/development/python-sockets/sniff.py", line 1, in <module>
import pyshark
File "/Library/Python/2.6/site-packages/pyshark/__init__.py", line 1, in <module>
from pyshark.capture.live_capture import LiveCapture
File "/Library/Python/2.6/site-packages/pyshark/capture/live_capture.py", line 1, in <module>
from pyshark.capture.capture import Capture
File "/Library/Python/2.6/site-packages/pyshark/capture/capture.py", line 12, in <module>
from pyshark.tshark.tshark_xml import packet_from_xml_packet, psml_structure_from_xml
File "/Library/Python/2.6/site-packages/pyshark/tshark/tshark_xml.py", line 5, in <module>
from pyshark.packet.layer import Layer
File "/Library/Python/2.6/site-packages/pyshark/packet/layer.py", line 57
return {slot: getattr(self, slot) for slot in self.__slots__}
^
SyntaxError: invalid syntax
The error is due to using a dictionary comprehension, a language feature that was introduced to Python 2 in 2.7, not the 2.6 you're trying to use. Apple ships OS X 10.10 with both 2.7 and 2.6. Is there a reason you can't use 2.7 instead?