Skip to content

Commit 7608514

Browse files
miroslavKovacPantheonmbj4668
authored andcommitted
Fix bugs with union and empty literal
Signed-off-by: miroslav.kovac <[email protected]>
1 parent 15c807f commit 7608514

File tree

1 file changed

+51
-54
lines changed

1 file changed

+51
-54
lines changed

pyang/statements.py

+51-54
Original file line numberDiff line numberDiff line change
@@ -2277,56 +2277,45 @@ def check_basic_path(stmt, toks, ctx, x,
22772277
else:
22782278
paths_left.update(paths_right)
22792279
if sys.version < '3':
2280-
for value, path_or_literal in paths_left.iteritems():
2281-
if path_or_literal == 'literal':
2282-
value = value.replace("'", '').replace('"', '').strip()
2283-
for path_stmt in path_stmts:
2284-
type = get_type_of_typedef(path_stmt, ctx)
2285-
check_type(path_stmt.search_one('type'), type,
2286-
value, ctx)
2287-
else:
2288-
path_stmts2 = check_path(value, stmt, ctx)
2289-
if path_stmts2 is None:
2290-
return None
2291-
for path_stmt in path_stmts:
2292-
for path_stmt2 in path_stmts2:
2293-
type = get_type_of_typedef(path_stmt, ctx)
2294-
type2 = get_type_of_typedef(path_stmt2, ctx)
2295-
if type != type2 and not function_exists:
2296-
if not ((type.startswith('uint') and
2297-
type2.startswith('uint')) or
2298-
(type.startswith('int') and
2299-
type2.startswith('int'))):
2300-
raise SyntaxError(
2301-
'Types in path condition "{}" does ' +
2302-
'not equal'.format(stmt.arg))
2280+
iteritems = paths_left.iteritems()
23032281
else:
2304-
for value, path_or_literal in paths_left.items():
2305-
if path_or_literal == 'literal':
2306-
value = value.replace("'", '').replace('"', '').strip()
2307-
for path_stmt in path_stmts:
2308-
type = get_type_of_typedef(path_stmt, ctx)
2309-
check_type(path_stmt.search_one('type'), type,
2310-
value, ctx)
2311-
else:
2312-
path_stmts2 = check_path(value, stmt, ctx)
2313-
if path_stmts2 is None:
2314-
return None
2315-
for path_stmt in path_stmts:
2316-
for path_stmt2 in path_stmts2:
2317-
type = get_type_of_typedef(path_stmt, ctx)
2318-
type2 = get_type_of_typedef(path_stmt2, ctx)
2319-
if type != type2 and not function_exists:
2320-
if not ((type.startswith('uint') and
2321-
type2.startswith('uint')) or
2322-
(type.startswith('int') and
2323-
type2.startswith('int'))):
2324-
raise SyntaxError(
2325-
'Types in path condition "{}" does ' +
2326-
'not equal'.format(stmt.arg))
2327-
2328-
2329-
def get_type_of_typedef(path_stmt, ctx):
2282+
iteritems = paths_left.items()
2283+
for value, path_or_literal in iteritems:
2284+
if path_or_literal == 'literal':
2285+
value = value.replace("'", '').replace('"', '').strip()
2286+
for path_stmt in path_stmts:
2287+
type = get_type_of_typedef(path_stmt, ctx)
2288+
check_type(path_stmt.search_one('type'), type,
2289+
value, ctx)
2290+
else:
2291+
path_stmts2 = check_path(value, stmt, ctx)
2292+
if path_stmts2 is None:
2293+
return None
2294+
for path_stmt in path_stmts:
2295+
for path_stmt2 in path_stmts2:
2296+
types = get_type_of_typedef(path_stmt, ctx, True)
2297+
types2 = get_type_of_typedef(path_stmt2, ctx, True)
2298+
if not isinstance(types, list):
2299+
types = [types]
2300+
if not isinstance(types2, list):
2301+
types2 = [types2]
2302+
found = False
2303+
for type in types:
2304+
for type2 in types2:
2305+
if type == type2 or function_exists:
2306+
found = True
2307+
elif ((type.startswith('uint') and
2308+
type2.startswith('uint')) or
2309+
(type.startswith('int') and
2310+
type2.startswith('int'))):
2311+
found = True
2312+
if not found:
2313+
raise SyntaxError(
2314+
'Types in path condition "{}" does '.format(stmt.arg) +
2315+
'not equal')
2316+
2317+
2318+
def get_type_of_typedef(path_stmt, ctx, check_union=False):
23302319
if path_stmt.keyword == 'identity':
23312320
return 'identityref'
23322321
elif path_stmt.keyword == 'refine':
@@ -2339,7 +2328,12 @@ def get_type_of_typedef(path_stmt, ctx):
23392328
type_stmt = path_stmt.search_one('type')
23402329
name = type_stmt.i_type_spec.name
23412330
if name == 'leafref':
2342-
return get_type_of_typedef(type_stmt.i_type_spec.i_target_node, ctx)
2331+
return get_type_of_typedef(type_stmt.i_type_spec.i_target_node, ctx, check_union)
2332+
elif name == 'union' and check_union:
2333+
ret = []
2334+
for t in type_stmt.search('type'):
2335+
ret.append(t.arg)
2336+
return ret
23432337
else:
23442338
return name
23452339
except AttributeError:
@@ -2376,10 +2370,13 @@ def check_type(stmt, type, literal, ctx):
23762370
elif type == 'bits':
23772371
bits = stmt.i_type_spec.bits
23782372
found = False
2379-
for bit in bits:
2380-
if bit[0] == literal:
2381-
found = True
2382-
break
2373+
if len(literal) == 0:
2374+
found = True
2375+
else:
2376+
for bit in bits:
2377+
if bit[0] == literal:
2378+
found = True
2379+
break
23832380
if not found:
23842381
raise Exception
23852382
elif type == 'empty':

0 commit comments

Comments
 (0)