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

Commit 5f1f01e

Browse files
author
misakwa
committed
Cache slot based objects with different keys
1 parent 8c6b369 commit 5f1f01e

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

tests/test_parser.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pytest
44
from thriftpy.thrift import TType
55
from thriftpy.parser import load, load_fp
6-
from thriftpy.parser.parser import thrift_cache
76
from thriftpy.parser.exc import ThriftParserError, ThriftGrammerError
87

98

@@ -264,7 +263,6 @@ def test_issue_215():
264263

265264

266265
def test_load_slots():
267-
thrift_cache.clear()
268266
thrift = load('addressbook.thrift', use_slots=True)
269267

270268
# normal structs will have slots

thriftpy/parser/parser.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ def p_definition_type(p):
436436
thrift_cache = {}
437437

438438

439+
def _get_cache_key(prefix, use_slots=False):
440+
return ('%s:slotted' % prefix) if use_slots else prefix
441+
442+
439443
def parse(path, module_name=None, include_dirs=None, include_dir=None,
440444
lexer=None, parser=None, enable_cache=True, use_slots=False):
441445
"""Parse a single thrift file to module object, e.g.::
@@ -471,7 +475,8 @@ def parse(path, module_name=None, include_dirs=None, include_dir=None,
471475

472476
global thrift_cache
473477

474-
cache_key = module_name or os.path.normpath(path)
478+
cache_prefix = module_name or os.path.normpath(path)
479+
cache_key = _get_cache_key(cache_prefix, use_slots)
475480

476481
if enable_cache and cache_key in thrift_cache:
477482
return thrift_cache[cache_key]
@@ -545,8 +550,10 @@ def parse_fp(source, module_name, lexer=None, parser=None, enable_cache=True, us
545550
raise ThriftParserError('ThriftPy can only generate module with '
546551
'\'_thrift\' suffix')
547552

553+
cache_key = _get_cache_key(module_name, use_slots)
554+
548555
if enable_cache and module_name in thrift_cache:
549-
return thrift_cache[module_name]
556+
return thrift_cache[cache_key]
550557

551558
if not hasattr(source, 'read'):
552559
raise ThriftParserError('Except `source` to be a file-like object with'
@@ -569,7 +576,7 @@ def parse_fp(source, module_name, lexer=None, parser=None, enable_cache=True, us
569576
thrift_stack.pop()
570577

571578
if enable_cache:
572-
thrift_cache[module_name] = thrift
579+
thrift_cache[cache_key] = thrift
573580
return thrift
574581

575582

0 commit comments

Comments
 (0)