Skip to content

Commit 207bb7e

Browse files
committed
Fix enum crash
1 parent 68c5a07 commit 207bb7e

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

CppHeaderParser/CppHeaderParser.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,15 +1597,19 @@ def finalize_vars(self):
15971597

15981598
elif nestedEnum:
15991599
enum = nestedEnum
1600-
if enum["type"] is int:
1600+
etype = enum.get("type")
1601+
if etype is int:
16011602
var["ctypes_type"] = "ctypes.c_int"
16021603
var["raw_type"] = "int"
16031604

1604-
elif enum["type"] is str:
1605+
elif etype is str:
16051606
var["ctypes_type"] = "ctypes.c_char_p"
16061607
var["raw_type"] = "char*"
16071608

1608-
var["enum"] = var["method"]["path"] + "::" + enum["name"]
1609+
if "method" in var:
1610+
var["enum"] = var["method"]["path"] + "::" + enum["name"]
1611+
else:
1612+
var["enum"] = enum["name"]
16091613
var["fundamental"] = True
16101614

16111615
elif nestedStruct:
@@ -1663,10 +1667,11 @@ def finalize_vars(self):
16631667
if b in klass._public_enums:
16641668
trace_print("...found nested enum", b)
16651669
enum = klass._public_enums[b]
1666-
if enum["type"] is int:
1670+
etype = enum.get("type")
1671+
if etype is int:
16671672
var["ctypes_type"] = "ctypes.c_int"
16681673
var["raw_type"] = "int"
1669-
elif enum["type"] is str:
1674+
elif etype is str:
16701675
var["ctypes_type"] = "ctypes.c_char_p"
16711676
var["raw_type"] = "char*"
16721677
try:
@@ -1700,10 +1705,11 @@ def finalize_vars(self):
17001705
): # falling back, this is a big ugly
17011706
enum = self.global_enums[b]
17021707
assert a in enum["namespace"].split("::")
1703-
if enum["type"] is int:
1708+
etype = enum.get("type")
1709+
if etype is int:
17041710
var["ctypes_type"] = "ctypes.c_int"
17051711
var["raw_type"] = "int"
1706-
elif enum["type"] is str:
1712+
elif etype is str:
17071713
var["ctypes_type"] = "ctypes.c_char_p"
17081714
var["raw_type"] = "char*"
17091715
var["fundamental"] = True
@@ -1931,18 +1937,18 @@ def finalize(self):
19311937

19321938
elif meth["returns"] in cls._public_enums:
19331939
enum = cls._public_enums[meth["returns"]]
1934-
meth["returns_enum"] = enum["type"]
1940+
meth["returns_enum"] = enum.get("type")
19351941
meth["returns_fundamental"] = True
1936-
if enum["type"] == int:
1942+
if enum.get("type") == int:
19371943
meth["returns"] = "int"
19381944
else:
19391945
meth["returns"] = "char*"
19401946

19411947
elif meth["returns"] in self.global_enums:
19421948
enum = self.global_enums[meth["returns"]]
1943-
meth["returns_enum"] = enum["type"]
1949+
meth["returns_enum"] = enum.get("type")
19441950
meth["returns_fundamental"] = True
1945-
if enum["type"] == int:
1951+
if enum.get("type") == int:
19461952
meth["returns"] = "int"
19471953
else:
19481954
meth["returns"] = "char*"
@@ -1958,9 +1964,9 @@ def finalize(self):
19581964
meth["returns_unknown"] = True
19591965
elif b in self.global_enums:
19601966
enum = self.global_enums[b]
1961-
meth["returns_enum"] = enum["type"]
1967+
meth["returns_enum"] = enum.get("type")
19621968
meth["returns_fundamental"] = True
1963-
if enum["type"] == int:
1969+
if enum.get("type") == int:
19641970
meth["returns"] = "int"
19651971
else:
19661972
meth["returns"] = "char*"
@@ -1975,9 +1981,9 @@ def finalize(self):
19751981
if b in klass._public_enums:
19761982
trace_print("...found nested enum", b)
19771983
enum = klass._public_enums[b]
1978-
meth["returns_enum"] = enum["type"]
1984+
meth["returns_enum"] = enum.get("type")
19791985
meth["returns_fundamental"] = True
1980-
if enum["type"] == int:
1986+
if enum.get("type") == int:
19811987
meth["returns"] = "int"
19821988
else:
19831989
meth["returns"] = "char*"

test/test_CppHeaderParser.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3522,5 +3522,28 @@ def test_nothing(self):
35223522
self.assertEqual(fn["parameters"][0]["enum"], "A::C")
35233523

35243524

3525+
class EnumCrash_TestCase(unittest.TestCase):
3526+
def setUp(self):
3527+
self.cppHeader = CppHeaderParser.CppHeader(
3528+
"""
3529+
enum HAL_Type {
3530+
HAL_UNASSIGNED = 0,
3531+
};
3532+
3533+
struct HAL_Value {
3534+
union {
3535+
HAL_Bool v_boolean;
3536+
} data;
3537+
enum HAL_Type type;
3538+
};
3539+
3540+
""",
3541+
"string",
3542+
)
3543+
3544+
def test_nothing(self):
3545+
pass
3546+
3547+
35253548
if __name__ == "__main__":
35263549
unittest.main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy