Skip to content

Commit f7cffe0

Browse files
authored
fix: Handle member structure pointers inside a structure (#471)
Signed-off-by: shubham kumar <shubham.kumar@intel.com>
1 parent 51aa747 commit f7cffe0

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

bindings/sysman/python/test/validate_structures.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,15 @@ def parse_python_structure(
101101
fields_str = match.group(1)
102102
fields = []
103103

104-
# Parse each field tuple
105-
field_pattern = r'\(\s*"([^"]+)"\s*,\s*([^\)]+?)\s*\)'
106-
107-
for field_match in re.finditer(field_pattern, fields_str):
108-
field_name = field_match.group(1).strip()
109-
field_type = field_match.group(2).strip().rstrip(",")
110-
fields.append((field_name, field_type))
104+
# Parse line by line to handle nested parentheses like POINTER(...)
105+
for line in fields_str.split("\n"):
106+
# Match: ("field_name", field_type), with optional comment
107+
pattern = r'\s*\(\s*"([^"]+)"\s*,\s*(.+?)\s*\)\s*,?\s*(?:#.*)?$'
108+
field_match = re.match(pattern, line)
109+
if field_match:
110+
field_name = field_match.group(1)
111+
field_type = field_match.group(2).strip().rstrip(",")
112+
fields.append((field_name, field_type))
111113

112114
return fields if fields else None
113115

@@ -126,6 +128,19 @@ def normalize_c_type(c_type: str) -> str:
126128
python_base = C_TO_PYTHON_TYPE_MAP.get(base_type, base_type)
127129
return f"{python_base} * {size}"
128130

131+
# Handle pointers to custom structures:
132+
# e.g., zes_power_limit_ext_desc_t* -> POINTER(zes_power_limit_ext_desc_t)
133+
if c_type.endswith("*"):
134+
# Check if it's a mapped type first (like void*, const void*)
135+
if c_type in C_TO_PYTHON_TYPE_MAP:
136+
return C_TO_PYTHON_TYPE_MAP[c_type]
137+
138+
# Otherwise, it's a pointer to a custom type
139+
base_type = c_type[:-1].strip() # Remove * and trailing spaces
140+
# Check if base type is in map (unlikely for pointers, but be safe)
141+
python_base = C_TO_PYTHON_TYPE_MAP.get(base_type, base_type)
142+
return f"POINTER({python_base})"
143+
129144
# Map type if in dictionary, otherwise keep as-is
130145
return C_TO_PYTHON_TYPE_MAP.get(c_type, c_type)
131146

@@ -150,7 +165,9 @@ def types_are_equivalent(type1: str, type2: str) -> bool:
150165

151166

152167
def compare_structures(
153-
c_fields: List[Tuple[str, str]], py_fields: List[Tuple[str, str]], struct_name: str
168+
c_fields: List[Tuple[str, str]],
169+
py_fields: List[Tuple[str, str]],
170+
struct_name: str,
154171
) -> List[str]:
155172
"""
156173
Compare C and Python structure fields.
@@ -181,18 +198,22 @@ def compare_structures(
181198

182199
# Check field name
183200
if c_name != py_name:
184-
errors.append(
185-
f"Field {i}: name mismatch - C: '{c_name}' ({c_type}), Python: '{py_name}' ({py_type})"
201+
msg = (
202+
f"Field {i}: name mismatch - "
203+
f"C: '{c_name}' ({c_type}), Python: '{py_name}' ({py_type})"
186204
)
187-
# Skip type comparison when names don't match - they're different fields
205+
errors.append(msg)
206+
# Skip type comparison when names don't match
188207
continue
189208

190209
# Check field type (only if names match)
191210
expected_type = normalize_c_type(c_type)
192211
if not types_are_equivalent(expected_type, py_type):
193-
errors.append(
194-
f"Field '{c_name}' (position {i}): type mismatch - C: '{c_type}' -> '{expected_type}', Python: '{py_type}'"
212+
msg = (
213+
f"Field '{c_name}' (position {i}): type mismatch - "
214+
f"C: '{c_type}' -> '{expected_type}', Python: '{py_type}'"
195215
)
216+
errors.append(msg)
196217

197218
return errors
198219

0 commit comments

Comments
 (0)