Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit 62e18fa

Browse files
committed
Fix bug with IDL encoding support. Before this moment we haven't option to specify encoding of IDL files, in parsing always was used OS specific encoding. That worked fine only for IDL in ASCII (without national language comments) or if you was lucky enough, that OS encoding matches IDL encoding.
1 parent b30e41d commit 62e18fa

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

thriftpy/parser/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .parser import parse, parse_fp
1616

1717

18-
def load(path, module_name=None, include_dirs=None, include_dir=None):
18+
def load(path, module_name=None, include_dirs=None, include_dir=None, encoding=None):
1919
"""Load thrift file as a module.
2020
2121
The module loaded and objects inside may only be pickled if module_name
@@ -27,7 +27,7 @@ def load(path, module_name=None, include_dirs=None, include_dir=None):
2727
"""
2828
real_module = bool(module_name)
2929
thrift = parse(path, module_name, include_dirs=include_dirs,
30-
include_dir=include_dir)
30+
include_dir=include_dir, encoding=encoding)
3131

3232
if real_module:
3333
sys.modules[module_name] = thrift

thriftpy/parser/parser.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
from __future__ import absolute_import
9+
from io import open
910

1011
import collections
1112
import os
@@ -55,7 +56,7 @@ def p_include(p):
5556
for include_dir in replace_include_dirs:
5657
path = os.path.join(include_dir, p[2])
5758
if os.path.exists(path):
58-
child = parse(path)
59+
child = parse(path, encoding=thrift.__thrift_encoding__)
5960
setattr(thrift, child.__name__, child)
6061
_add_thrift_meta('includes', child)
6162
return
@@ -482,7 +483,7 @@ def p_type_annotation(p):
482483

483484

484485
def parse(path, module_name=None, include_dirs=None, include_dir=None,
485-
lexer=None, parser=None, enable_cache=True):
486+
lexer=None, parser=None, enable_cache=True, encoding=None):
486487
"""Parse a single thrift file to module object, e.g.::
487488
488489
>>> from thriftpy.parser.parser import parse
@@ -503,6 +504,9 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None,
503504
:param enable_cache: if this is set to be `True`, parsed module will be
504505
cached, this is enabled by default. If `module_name`
505506
is provided, use it as cache key, else use the `path`.
507+
:param encoding: encoding is the name of the encoding used to decode or encode the file.
508+
This should only be used in text mode. The default encoding is platform dependent,
509+
but any encoding supported by Python can be passed.
506510
"""
507511
if os.name == 'nt' and sys.version_info < (3, 2):
508512
os.path.samefile = lambda f1, f2: os.stat(f1) == os.stat(f2)
@@ -537,10 +541,10 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None,
537541

538542
url_scheme = urlparse(path).scheme
539543
if url_scheme == 'file':
540-
with open(urlparse(path).netloc + urlparse(path).path) as fh:
544+
with open(urlparse(path).netloc + urlparse(path).path, encoding=encoding) as fh:
541545
data = fh.read()
542546
elif url_scheme == '':
543-
with open(path) as fh:
547+
with open(path, encoding=encoding) as fh:
544548
data = fh.read()
545549
elif url_scheme in ('http', 'https'):
546550
data = urlopen(path).read()
@@ -559,6 +563,7 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None,
559563

560564
thrift = types.ModuleType(module_name)
561565
setattr(thrift, '__thrift_file__', path)
566+
setattr(thrift, '__thrift_encoding__', encoding)
562567
thrift_stack.append(thrift)
563568
lexer.lineno = 1
564569
parser.parse(data)

0 commit comments

Comments
 (0)