@@ -134,7 +134,8 @@ def extract():
134134 sline [0 ] = sline [0 ].split ("," )[0 ]
135135 # This removes anything after the equal sign as we don't need it
136136 sline [0 ] = sline [0 ].split ("=" )[0 ]
137- print ("\t result += PyModule_AddIntMacro(module, %s);" % sline [0 ].replace ("," , "" ), file = f )
137+ value = sline [0 ].replace ("," , "" )
138+ print (f"\t result += PyModule_AddObjectRef(module, \" { value } \" , PyLong_FromLongLong({ value } ));" , file = f )
138139 continue
139140 if "#define" in line :
140141 sline = line .split ("//" )[0 ].split ()
@@ -144,21 +145,36 @@ def extract():
144145 if len (sline ) >= 3 and re .match (r"^\d+?\.\d+?$" , sline [2 ]) is not None :
145146 # Value is a float
146147 print (
147- '\t result += PyModule_AddObject (module, "{0}", PyFloat_FromDouble({0}));' .format (
148+ '\t result += PyModule_AddObjectRef (module, "{0}", PyFloat_FromDouble({0}));' .format (
148149 sline [1 ]
149150 ),
150151 file = f ,
151152 )
153+ elif len (sline ) >= 3 and re .match (r"^\d+$" , sline [2 ]) is not None :
154+ # We should be an integer at this point
155+ try :
156+ if int (sline [2 ]) < 0 :
157+ # Integer Value is negative
158+ print (f"\t result += PyModule_AddObjectRef(module, \" { sline [1 ]} \" , PyLong_FromLongLong({ sline [1 ]} ));" , file = f )
159+ else :
160+ # Integer Value is positive
161+ print (f"\t result += PyModule_AddObjectRef(module, \" { sline [1 ]} \" , PyLong_FromUnsignedLongLong({ sline [1 ]} ));" , file = f )
162+ except ValueError :
163+ # Not an integer, probably another define
164+ # This comes here: #define SPY_STATUS2_RX_TIMEOUT_ERROR SPY_STATUS2_ISO_RX_TIMEOUT_ERROR
165+ print (f"ERROR: { sline [1 ]} { sline [2 ]} " )
166+ print (f"\t result += PyModule_AddObjectRef(module, \" { sline [1 ]} \" , PyLong_FromLongLong({ sline [1 ]} ));" , file = f )
167+
152168 elif len (sline ) == 2 :
153169 # There is no value, this is used for preprocessor only
154170 # and really shouldn't care about it in python. Going to set it as 0 "just in case"
155171 print (
156- '\t result += PyModule_AddObject (module, "{}", PyLong_FromLong(0));' .format (sline [1 ]),
172+ '\t result += PyModule_AddObjectRef (module, "{}", PyLong_FromLong(0));' .format (sline [1 ]),
157173 file = f ,
158174 )
159175 else :
160- print ("\t result += PyModule_AddIntMacro (module, %s);" % sline [1 ], file = f )
161-
176+ print (f "\t result += PyModule_AddObjectRef (module, \" { sline [ 1 ] } \" , PyLong_FromLongLong( { sline [1 ]} ));" , file = f )
177+
162178 elif "/*" in line :
163179 inside_comment = True
164180 continue
@@ -172,7 +188,7 @@ def extract():
172188 # Remove =X assignments if present.
173189 sline = [x .split ("=" )[0 ] for x in sline ]
174190 for e in sline :
175- print ("\t result += PyModule_AddIntMacro (module, %s);" % e , file = f )
191+ print (f "\t result += PyModule_AddObjectRef (module, \" { e } \" , PyLong_FromLongLong( { e } ));" , file = f )
176192 inside_enum = False
177193 continue
178194 if "icsnVC40Internal" in header_file :
0 commit comments