diff options
author | David Beazley <dave@dabeaz.com> | 2015-05-07 15:53:44 -0500 |
---|---|---|
committer | David Beazley <dave@dabeaz.com> | 2015-05-07 15:53:44 -0500 |
commit | 192d922697726dc59c7af1480a04e9fcd022cffc (patch) | |
tree | 9d22b800583c2194af401169a7b6d09f17612d04 /ply/yacc.py | |
parent | fee743164e888f03c68ec87aef8fa63877c24c64 (diff) | |
download | ply-192d922697726dc59c7af1480a04e9fcd022cffc.tar.gz |
Fixed issue 63
Diffstat (limited to 'ply/yacc.py')
-rw-r--r-- | ply/yacc.py | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/ply/yacc.py b/ply/yacc.py index f18e3eb..e0b4faf 100644 --- a/ply/yacc.py +++ b/ply/yacc.py @@ -2692,7 +2692,11 @@ class LRGeneratedTable(LRTable): # This function writes the LR parsing tables to a file # ----------------------------------------------------------------------------- - def write_table(self, basemodulename, outputdir='', signature=''): + def write_table(self, tabmodule, outputdir='', signature=''): + if isinstance(tabmodule, types.ModuleType): + raise IOError("Won't overwrite existing tabmodule") + + basemodulename = tabmodule.split('.')[-1] filename = os.path.join(outputdir, basemodulename) + '.py' try: f = open(filename, 'w') @@ -3204,22 +3208,26 @@ def yacc(method='LALR', debug=yaccdebug, module=None, tabmodule=tab_module, star # is determined according to the following rules: # - If tabmodule specifies a package, files go into that package directory # - Otherwise, files go in the same directory as the specifying module - if '.' not in tabmodule: - srcfile = pdict['__file__'] + if isinstance(tabmodule, types.ModuleType): + srcfile = tabmodule.__file__ else: - parts = tabmodule.split('.') - pkgname = '.'.join(parts[:-1]) - exec('import %s' % pkgname) - srcfile = getattr(sys.modules[pkgname], '__file__', '') + if '.' not in tabmodule: + srcfile = pdict['__file__'] + else: + parts = tabmodule.split('.') + pkgname = '.'.join(parts[:-1]) + exec('import %s' % pkgname) + srcfile = getattr(sys.modules[pkgname], '__file__', '') outputdir = os.path.dirname(srcfile) # Determine if the module is package of a package or not. # If so, fix the tabmodule setting so that tables load correctly pkg = pdict.get('__package__') - if pkg and '.' not in tabmodule: - tabmodule = pkg + '.' + tabmodule + if pkg and isinstance(tabmodule, str): + if '.' not in tabmodule: + tabmodule = pkg + '.' + tabmodule + - basetabmodule = tabmodule.split('.')[-1] # Set start symbol if it's specified directly using an argument if start is not None: @@ -3432,7 +3440,7 @@ def yacc(method='LALR', debug=yaccdebug, module=None, tabmodule=tab_module, star # Write the table file if requested if write_tables: try: - lr.write_table(basetabmodule, outputdir, signature) + lr.write_table(tabmodule, outputdir, signature) except IOError as e: errorlog.warning("Couldn't create %r. %s" % (tabmodule, e)) |