Auto merge of #2160 - micbou:open-file-encoding, r=Valloric

[READY] Fix LookupError exception when opening a file

Fixes #2159.

I think the issue is caused by `locale.getpreferredencoding()` returning an empty string on some configurations (especially on OS X with Python 2). This is [the value used by default when no encoding is specified](https://docs.python.org/2/library/io.html?highlight=open#io.open). The same error is raised if we do:
```python
open( 'some_file', encoding = '' )
```

For references, similar issues: https://github.com/Valloric/ycmd/issues/395, https://github.com/Valloric/ycmd/issues/419

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/valloric/youcompleteme/2160)
<!-- Reviewable:end -->
This commit is contained in:
Homu 2016-05-11 02:01:22 +09:00
commit e0f2da0885
2 changed files with 5 additions and 2 deletions

View File

@ -30,12 +30,13 @@ import os
from nose.tools import eq_ from nose.tools import eq_
from hamcrest import assert_that, has_items from hamcrest import assert_that, has_items
from ycm import syntax_parse from ycm import syntax_parse
from ycmd.utils import ReadFile
def ContentsOfTestFile( test_file ): def ContentsOfTestFile( test_file ):
dir_of_script = os.path.dirname( os.path.abspath( __file__ ) ) dir_of_script = os.path.dirname( os.path.abspath( __file__ ) )
full_path_to_test_file = os.path.join( dir_of_script, 'testdata', test_file ) full_path_to_test_file = os.path.join( dir_of_script, 'testdata', test_file )
return open( full_path_to_test_file ).read() return ReadFile( full_path_to_test_file )

View File

@ -837,7 +837,9 @@ def WriteToPreviewWindow( message ):
def CheckFilename( filename ): def CheckFilename( filename ):
"""Check if filename is openable.""" """Check if filename is openable."""
try: try:
open( filename ).close() # We don't want to check for encoding issues when trying to open the file
# so we open it in binary mode.
open( filename, mode = 'rb' ).close()
except TypeError: except TypeError:
raise RuntimeError( "'{0}' is not a valid filename".format( filename ) ) raise RuntimeError( "'{0}' is not a valid filename".format( filename ) )
except IOError as error: except IOError as error: