Skip to content

Commit 1e37fae

Browse files
committed
Simplify logic around tabmodule / outputdir / debugfile
1 parent ec6bb3f commit 1e37fae

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

pyomo/tpl/ply/yacc.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,22 +3271,6 @@ def yacc(method='LALR', debug=yaccdebug, module=None, tabmodule=tab_module, star
32713271
else:
32723272
pdict = get_caller_module_dict(2)
32733273

3274-
if outputdir is None:
3275-
# If no output directory is set, the location of the output files
3276-
# is determined according to the following rules:
3277-
# - If tabmodule specifies a package, files go into that package directory
3278-
# - Otherwise, files go in the same directory as the specifying module
3279-
assert isinstance(tabmodule, types.ModuleType)
3280-
else:
3281-
assert isinstance(tabmodule, str)
3282-
3283-
# Determine if the module is package of a package or not.
3284-
# If so, fix the tabmodule setting so that tables load correctly
3285-
pkg = pdict.get('__package__')
3286-
if pkg and isinstance(tabmodule, str):
3287-
if '.' not in tabmodule:
3288-
tabmodule = pkg + '.' + tabmodule
3289-
32903274
# Set start symbol if it's specified directly using an argument
32913275
if start is not None:
32923276
pdict['start'] = start
@@ -3301,22 +3285,35 @@ def yacc(method='LALR', debug=yaccdebug, module=None, tabmodule=tab_module, star
33013285
# Check signature against table files (if any)
33023286
signature = '' # pinfo.signature() <-- this has unreliable amount of whitespace
33033287

3304-
if outputdir is None:
3288+
if isinstance(tabmodule, types.ModuleType):
33053289
# Read the tables
33063290
lr = LRTable()
33073291
read_signature = lr.read_table(tabmodule)
33083292
if read_signature != signature:
33093293
raise YaccError(f"Parse table signature mismatch")
33103294
lr.bind_callables(pinfo.pdict)
33113295
return LRParser(lr, pinfo.error_func)
3296+
else:
3297+
assert isinstance(tabmodule, str)
3298+
3299+
# Determine if the module is package of a package or not.
3300+
# If so, fix the tabmodule setting so that tables load correctly
3301+
pkg = pdict.get('__package__')
3302+
if pkg and '.' not in tabmodule:
3303+
tabmodule = pkg + '.' + tabmodule
33123304

33133305
if debuglog is None:
33143306
if debug:
3315-
try:
3316-
debuglog = PlyLogger(open(os.path.join(outputdir, debugfile), 'w'))
3317-
except IOError as e:
3318-
errorlog.warning("Couldn't open %r. %s" % (debugfile, e))
3319-
debuglog = NullLogger()
3307+
if debugfile:
3308+
if outputdir:
3309+
debugfile = os.path.join(outputdir, debugfile)
3310+
try:
3311+
debuglog = PlyLogger(open(debugfile, 'w'))
3312+
except IOError as e:
3313+
errorlog.warning("Couldn't open %r. %s" % (debugfile, e))
3314+
debuglog = NullLogger()
3315+
else:
3316+
debuglog = PlyLogger(sys.stdout)
33203317
else:
33213318
debuglog = NullLogger()
33223319

@@ -3485,5 +3482,7 @@ def yacc(method='LALR', debug=yaccdebug, module=None, tabmodule=tab_module, star
34853482
warned_never.append(rejected)
34863483

34873484
# Write the table file
3488-
lr.write_table(tabmodule, outputdir, signature, module_signature)
3489-
return None
3485+
if outputdir:
3486+
lr.write_table(tabmodule, outputdir, signature, module_signature)
3487+
3488+
return LRParser(lr, pinfo.error_func)

0 commit comments

Comments
 (0)