Thriftpy / thriftpy2

Pure python approach of Apache Thrift.
MIT License
572 stars 91 forks source link

When environment locale not set, thrift parser would throw UnicodeDecodeError due to using "open" directly. #172

Closed truebit closed 1 year ago

truebit commented 3 years ago
File "/Users/someone/Documents/code/pycharm/test/venv/lib/oython3.6/site-packages/thriftpy2/parser/ init py", line 33, in load
include_dir=include_dir)
File "/Users/someone/Documents/code/oycharm/test/venv/lib/oython3.6/site-packaaes/thriftoy2/oarser/oarser.py", line 577, in parse data = fh.read ()
File "/Users/someone/.pyenv/versions/3.6.0/lib/python3.6/encodinqs/ascii.py'", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte @xe6 in position 113: ordinal not in range(128)

prboblematic code in parser.py:

    url_scheme = urlparse(path).scheme
    if url_scheme == 'file':
        with open(urlparse(path).netloc + urlparse(path).path) as fh:
            data = fh.read()
    elif len(url_scheme) <= 1:
        with open(path) as fh:
            data = fh.read()
    elif url_scheme in ('http', 'https'):
        data = urlopen(path).read()
    else:
        raise ThriftParserError('thriftpy2 does not support generating module '
                                'with path in protocol \'{}\''.format(
                                    url_scheme))

Possible solution: check if python2 or python3, py3 supports open(encoding=xxx), py2 could use io.open