summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSegev Finer <segev208@gmail.com>2017-12-01 18:18:10 +0200
committerSegev Finer <segev208@gmail.com>2017-12-01 18:21:07 +0200
commitebf2d286d5dfc4cd255d523366f067df6ee48d14 (patch)
treed27e324c2a8558589ada26edfd6a164deaf9ac93
parent15d42d9d17b41cac7c0937bc858ec9407f0c2d03 (diff)
downloadply-ebf2d286d5dfc4cd255d523366f067df6ee48d14.tar.gz
Calculate the correct tabmodule for parsers defined in a class inside a package
Please verify that I didn't introduce an AttributeError or KeyError by accident. I didn't touch the code that fixes __file__ to preserve any existing behavior. Fixes #140
-rw-r--r--ply/yacc.py6
1 files changed, 5 insertions, 1 deletions
diff --git a/ply/yacc.py b/ply/yacc.py
index 4210239..97c9a1e 100644
--- a/ply/yacc.py
+++ b/ply/yacc.py
@@ -3230,9 +3230,13 @@ def yacc(method='LALR', debug=yaccdebug, module=None, tabmodule=tab_module, star
if module:
_items = [(k, getattr(module, k)) for k in dir(module)]
pdict = dict(_items)
- # If no __file__ attribute is available, try to obtain it from the __module__ instead
+ # If no __file__ or __package__ attributes are available, try to obtain them
+ # from the __module__ instead
if '__file__' not in pdict:
pdict['__file__'] = sys.modules[pdict['__module__']].__file__
+ if '__package__' not in pdict and '__module__' in pdict:
+ if hasattr(sys.modules[pdict['__module__']], '__package__'):
+ pdict['__package__'] = sys.modules[pdict['__module__']].__package__
else:
pdict = get_caller_module_dict(2)