Skip to content

Commit

Permalink
Merge pull request #173 from truebit/issue172
Browse files Browse the repository at this point in the history
try to fix issue #172 when system locale not set
  • Loading branch information
ethe authored May 26, 2023
2 parents d48d58c + b74a8cd commit 0764351
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions thriftpy2/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from thriftpy2._compat import urlopen, urlparse, PY3
from ..thrift import gen_init, TType, TPayload, TException

if not PY3:
from io import open


def p_error(p):
if p is None:
Expand Down Expand Up @@ -573,7 +576,7 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None,
with open(urlparse(path).netloc + urlparse(path).path) as fh:
data = fh.read()
elif len(url_scheme) <= 1:
with open(path) as fh:
with open(path, encoding=encoding) as fh:
data = fh.read()
elif url_scheme in ('http', 'https'):
data = urlopen(path).read()
Expand All @@ -582,8 +585,11 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None,
'with path in protocol \'{}\''.format(
url_scheme))

if PY3 and isinstance(data, bytes):
data = data.decode(encoding)
if PY3:
if isinstance(data, bytes):
data = data.decode(encoding)
else:
data = data.encode(encoding)

if module_name is not None and not module_name.endswith('_thrift'):
raise ThriftParserError('thriftpy2 can only generate module with '
Expand Down

0 comments on commit 0764351

Please sign in to comment.