Skip to content

Commit

Permalink
Initialize the thread local in parser module in every thread (#267)
Browse files Browse the repository at this point in the history
* Initialize the thread local in parser module in every thread

* Remove redundant codes
  • Loading branch information
aisk committed May 14, 2024
1 parent a16e0db commit 9db168d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions thriftpy2/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,6 @@ def set_info(self, info):
return self.index + 1


threadlocal.incomplete_type = CurrentIncompleteType()


def p_ref_type(p):
'''ref_type : IDENTIFIER'''
ref_type = threadlocal.thrift_stack[-1]
Expand Down Expand Up @@ -515,11 +512,6 @@ def p_type_annotation(p):
p[0] = p[1], None # Without Value


threadlocal.thrift_stack = []
threadlocal.include_dirs_ = ['.']
threadlocal.thrift_cache = {}


def parse(path, module_name=None, include_dirs=None, include_dir=None,
lexer=None, parser=None, enable_cache=True, encoding='utf-8'):
"""Parse a single thrift file to module object, e.g.::
Expand All @@ -543,6 +535,15 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None,
cached, this is enabled by default. If `module_name`
is provided, use it as cache key, else use the `path`.
"""
# threadlocal should be initialized in every threads
initialized = getattr(threadlocal, 'initialized', None)
if initialized is None:
threadlocal.thrift_stack = []
threadlocal.include_dirs_ = ['.']
threadlocal.thrift_cache = {}
threadlocal.incomplete_type = CurrentIncompleteType()
threadlocal.initialized = True

# dead include checking on current stack
for thrift in threadlocal.thrift_stack:
if thrift.__thrift_file__ is not None and \
Expand Down

0 comments on commit 9db168d

Please sign in to comment.