qapi: Simplify inclusion cycle detection

We maintain a stack of filenames in include_hist for convenient cycle
detection.

As error_path() demonstrates, the same information is readily
available in the expr_info, so just use that, and drop include_hist.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Markus Armbruster 2015-06-09 16:54:09 +02:00
parent 8608d25251
commit a136608727
1 changed files with 8 additions and 10 deletions

View File

@ -101,15 +101,10 @@ def __str__(self):
class QAPISchema: class QAPISchema:
def __init__(self, fp, include_hist = [], def __init__(self, fp, previously_included = [], incl_info = None):
previously_included = [], incl_info = None):
""" include_hist is a stack used to detect inclusion cycles
previously_included is a global state used to avoid multiple
inclusions of the same file"""
abs_fname = os.path.abspath(fp.name) abs_fname = os.path.abspath(fp.name)
fname = fp.name fname = fp.name
self.fname = fname self.fname = fname
self.include_hist = include_hist + [(fname, abs_fname)]
previously_included.append(abs_fname) previously_included.append(abs_fname)
self.incl_info = incl_info self.incl_info = incl_info
self.src = fp.read() self.src = fp.read()
@ -135,10 +130,13 @@ def __init__(self, fp, include_hist = [],
% include) % include)
incl_abs_fname = os.path.join(os.path.dirname(abs_fname), incl_abs_fname = os.path.join(os.path.dirname(abs_fname),
include) include)
for elem in self.include_hist: # catch inclusion cycle
if incl_abs_fname == elem[1]: inf = expr_info
while inf:
if incl_abs_fname == os.path.abspath(inf['file']):
raise QAPIExprError(expr_info, "Inclusion loop for %s" raise QAPIExprError(expr_info, "Inclusion loop for %s"
% include) % include)
inf = inf['parent']
# skip multiple include of the same file # skip multiple include of the same file
if incl_abs_fname in previously_included: if incl_abs_fname in previously_included:
continue continue
@ -147,8 +145,8 @@ def __init__(self, fp, include_hist = [],
except IOError, e: except IOError, e:
raise QAPIExprError(expr_info, raise QAPIExprError(expr_info,
'%s: %s' % (e.strerror, include)) '%s: %s' % (e.strerror, include))
exprs_include = QAPISchema(fobj, self.include_hist, exprs_include = QAPISchema(fobj, previously_included,
previously_included, expr_info) expr_info)
self.exprs.extend(exprs_include.exprs) self.exprs.extend(exprs_include.exprs)
else: else:
expr_elem = {'expr': expr, expr_elem = {'expr': expr,