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

Make fails with UnicodeDecodeError #830

Closed
Spielzeug opened this issue Sep 24, 2017 · 18 comments
Closed

Make fails with UnicodeDecodeError #830

Spielzeug opened this issue Sep 24, 2017 · 18 comments
Assignees
Labels
I: No breaking change Previously written code will work as before, no one should note anything changing (aside the fix) S: High Should be handled next T: Bug Wrong statements in the code or documentation ZC: Installation DO NOT USE THIS LABEL ZP: PR Created DO NOT USE THIS LABEL

Comments

@Spielzeug
Copy link

Spielzeug commented Sep 24, 2017

Under archlinux, make fails either using python2 or python3:

cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr -DPYTHON_EXECUTABLE=/usr/bin/python2 -DPYTHON_INCLUDE_DIR=/usr/include/python2.7 -DPYTHON_LIBRARY=/usr/lib/libpython2.7.so -DPYTHON_BASENAME=-python2.7 ../nest-2.12.0

OR

cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr -DPYTHON_EXECUTABLE=/usr/bin/python -DPYTHON_INCLUDE_DIR=/usr/include/python3.6 -DPYTHON_LIBRARY=/usr/lib/libpython3.6.so -DPYTHON_BASENAME=-python3.6 ../nest-2.12.0

CMake output: https://pastebin.com/3esMhsM7

after this make command fails with:

Traceback (most recent call last):
File "generate_help.py", line 76, in
filetext = f.read()
File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1128: ordinal not in range(128)
make[2]: *** [doc/CMakeFiles/generate_help.dir/build.make:58: generate_help] Error 1
make[1]: *** [CMakeFiles/Makefile2:216: doc/CMakeFiles/generate_help.dir/all] Error 2
make: *** [Makefile:163: all] Error 2

Tested the generate_help.py invokation with python2 and python3, it works with python2. Had to change cmake generated build.make:
generate_help: doc/CMakeFiles/generate_help.dir/build.make
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold "Extracting help information; this may take a litte while."
cd /home/rei/Downloads/nest-2.12.0/extras/help_generator && python -B generate_help.py /home/rei/Downloads/nest-2.12.0 /home/rei/Downloads/nest-build
cd /home/rei/Downloads/nest-2.12.0/extras/help_generator && python -B generate_helpindex.py /home/rei/Downloads/nest-build/doc
.PHONY : generate_help

to

generate_help: doc/CMakeFiles/generate_help.dir/build.make
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold "Extracting help information; this may take a litte while."
cd /home/rei/Downloads/nest-2.12.0/extras/help_generator && python2 -B generate_help.py /home/rei/Downloads/nest-2.12.0 /home/rei/Downloads/nest-build
cd /home/rei/Downloads/nest-2.12.0/extras/help_generator && python2 -B generate_helpindex.py /home/rei/Downloads/nest-build/doc
.PHONY : generate_help

Can someone point out what should I have done to avoid this messy change?

@sanjayankur31
Copy link
Contributor

It seems to be an encoding issue - explained quite well here. Funnily enough, I don't see it neither with python2 nor python3 on Fedora 26 here, so could be something to do with the system defaults there? Maybe check your default encodings? In a python interpreter, import sys and check the output of sys.getdefaultencoding()? I get ascii for python2 and utf-8 for python3 here.

@Spielzeug
Copy link
Author

It is an encoding problem, my problem is that I do not know how to avoid it properly. I too have ascii for python2 and utf-8 for python3. For now I circumvented the problem as I said above and have nest installed and running, for the future it would be nice to know how to force python encoding during cmake configuration generation.

@heplesser heplesser added ZC: Installation DO NOT USE THIS LABEL I: No breaking change Previously written code will work as before, no one should note anything changing (aside the fix) ZP: Pending DO NOT USE THIS LABEL S: High Should be handled next T: Bug Wrong statements in the code or documentation labels Oct 3, 2017
@heplesser
Copy link
Contributor

@steffengraber Could you take a look at this? I assume the problem occurs because some .sli file contains non-ASCII characters. Then, if the file is opened using f = open(file, 'r'), this will lead to trouble in Python 3. We probably have to check two things here:

  1. How to handle file opening properly under Py2 and Py3 (in Py3, add encoding='utf-8' as argument to open(), but that won't run under Py2.
  2. Ensure that all NEST code files are UTF-8 encoded.

@steffengraber
Copy link
Contributor

@heplesser We can use the io module and write io.open(“file”, “r”, encoding=”utf-8”) instead of open( ... ). It is available since 2.6 and standard in python3.
I have to test it.

@heplesser
Copy link
Contributor

We can use the following to scan the code base for files that are not valid UTF-8 files:

for r, d, f in os.walk('.'):
       for fn in f:
            try:
                open(os.path.join(r, fn), 'r', encoding='utf-8').read()
            except:
                print('Error:', os.path.join(r, fn))

@jougs
Copy link
Contributor

jougs commented Oct 12, 2017

@heplesser: Thanks for the code. Here's my extended version, which should work with Python 2 and 3:

import os
from io import open

exclude_paths = [".git"]
exclude_files = [".jpg", ".pdf", ".png"]

for r, d, f in os.walk('.'):
    for fn in f:
       check = True
       for ep in exclude_paths:
           if r.startswith(os.path.join(".", ep)):
                check = False
                break
       for ef in exclude_files:
           if fn.endswith(ef):
                check = False
                break
       if check:
            try:
                open(os.path.join(r, fn), 'r', encoding='utf-8').read()
            except:
                print('Error:', os.path.join(r, fn))

On my machine (Ubuntu 17.04), this does not report any errors. @Spielzeug, can you please save the code to a file in the NEST source directory on your machine and execute it? Thanks!

@planetmija
Copy link

planetmija commented Nov 16, 2017

@jougs : I tried it with my own fresh compiled python 3.6.3, but I don't get any output:

$ python bla.py 
3.6.3 (default, Nov 16 2017, 17:53:58) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

OR

$ python bla.py 
3.5.3 |Intel Corporation| (default, Apr 27 2017, 18:08:47) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]

But I get the same compile error:

File "/opt/bwhpc/common/devel/python/3.6.3.20171114/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]

EDIT: bla.py is your code + print(sys.version)

@planetmija
Copy link

@jougs : Sorry, forgot, that I already deleted the build dir. Here are the files:

3.6.3 (default, Nov 16 2017, 17:53:58) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
Error: ./CMakeFiles/feature_tests.bin
Error: ./CMakeFiles/CheckTypeSize/UINT16_T_SIZE.bin
Error: ./CMakeFiles/CheckTypeSize/LONG_LONG_SIZE.bin
Error: ./CMakeFiles/3.9.0/CMakeDetermineCompilerABI_CXX.bin
Error: ./CMakeFiles/3.9.0/CMakeDetermineCompilerABI_C.bin
Error: ./CMakeFiles/3.9.0/CompilerIdCXX/a.out
Error: ./CMakeFiles/3.9.0/CompilerIdC/a.out
Error: ./CMakeFiles/FindOpenMP/ompver_CXX.bin
Error: ./CMakeFiles/FindOpenMP/ompver_C.bin
Error: ./libnestutil/libnestutil.so
Error: ./libnestutil/CMakeFiles/nestutil.dir/stopwatch.cpp.o
Error: ./libnestutil/CMakeFiles/nestutil.dir/logging_event.cpp.o
Error: ./libnestutil/CMakeFiles/nestutil.dir/propagator_stability.cpp.o
Error: ./libnestutil/CMakeFiles/nestutil.dir/numerics.cpp.o

@krisdamato
Copy link

Any updates on this? I'm getting the same error when running make install on both Python 3.5.4 and 2.7 in Ubuntu 16.04, so the above workaround does not work.

@steffengraber
Copy link
Contributor

@krisdamato
I tried it with a fresh install of Ubuntu 16.04.3 with all the latest updates (Python 2.7.12, Python 3.5.2) in a Virtualbox and could not reproduce your problems.
Additionally I tried Python 3.6.4. Even with that I could not create the error.

That's what I did in detail:

Install the dependencies:

sudo apt-get install -y build-essential cmake libltdl-dev libreadline6-dev libncurses5-dev libgsl-dev python-all-dev python-numpy python-scipy python-matplotlib ipython openmpi-bin libopenmpi-dev python-nose cython

Getting NEST

wget https://github.com/nest/nest-simulator/archive/master.zip
unzip master.zip
mkdir build
mkdir install
cd build

Installing NEST with Python 2

cmake -DCMAKE_INSTALL_PREFIX:PATH=../install -Dwith-python=2 -DPYTHON_EXECUTABLE=/usr/bin/python2 ../nest-simulator-master
make
make install

or: Installing NEST with Python 3

cmake -DCMAKE_INSTALL_PREFIX:PATH=../install -Dwith-python=3 -DPYTHON_EXECUTABLE=/usr/bin/python3 ../nest-simulator-master
make
make install

Could you please explain exactly what you did?

@krisdamato
Copy link

krisdamato commented Jan 9, 2018 via email

@steffengraber
Copy link
Contributor

@krisdamato An official docker image is in preparation.

@steffengraber
Copy link
Contributor

I tried the installation with non-UTF-8 locales settings (en_US) of the system and could now recover the error message.
When I reset everything to en_US. UTF-8 make runs without error messages.

@jougs
Copy link
Contributor

jougs commented Feb 7, 2018

@steffengraber: can you please run the code from my comment with the non-unicode locale and see if there are files with errors? Thanks!

@steffengraber
Copy link
Contributor

@jougs Sorry, checked again in new master with python 2 and 3. No errors.

@terhorstd
Copy link
Contributor

@steffengraber : could you add an , encoding="utf8" to the open() statements in the help generation? This would probably fix this issue (and would be more correct procedure).

@steffengraber
Copy link
Contributor

Because encoding='utf-8' as argument to open() won't run under Python2 I decided to use the io-module (io.open(file”, “r”, encoding=”utf-8”)).

@heplesser heplesser added ZP: PR Created DO NOT USE THIS LABEL and removed ZP: Pending DO NOT USE THIS LABEL labels Mar 20, 2018
@heplesser
Copy link
Contributor

Fixed by #915.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I: No breaking change Previously written code will work as before, no one should note anything changing (aside the fix) S: High Should be handled next T: Bug Wrong statements in the code or documentation ZC: Installation DO NOT USE THIS LABEL ZP: PR Created DO NOT USE THIS LABEL
Projects
None yet
Development

No branches or pull requests

8 participants