14
14
from templates .helper import param_traits , type_traits , value_traits
15
15
import ctypes
16
16
import itertools
17
- from typing import Dict , List , Optional
17
+ from typing import Dict , List , Optional , Union
18
18
from version import Version
19
19
20
20
@@ -86,7 +86,9 @@ def _validate_doc(f, d, tags, line_num, meta):
86
86
validate documents meet some basic (easily detectable) requirements of code
87
87
generation
88
88
"""
89
- is_iso = lambda x : re .match (r"[_a-zA-Z][_a-zA-Z0-9]{0,30}" , x )
89
+
90
+ def is_iso (x ):
91
+ return re .match (r"[_a-zA-Z][_a-zA-Z0-9]{0,30}" , x )
90
92
91
93
def __validate_ordinal (d ):
92
94
if "ordinal" in d :
@@ -95,7 +97,7 @@ def __validate_ordinal(d):
95
97
96
98
try :
97
99
ordinal = str (int (d ["ordinal" ]))
98
- except :
100
+ except BaseException :
99
101
ordinal = None
100
102
101
103
if ordinal != d ["ordinal" ]:
@@ -110,7 +112,7 @@ def __validate_version(d, prefix="", base_version=default_version):
110
112
111
113
try :
112
114
version = str (d ["version" ])
113
- except :
115
+ except BaseException :
114
116
version = None
115
117
116
118
if version != d ["version" ]:
@@ -124,7 +126,7 @@ def __validate_tag(d, key, tags, case):
124
126
return x
125
127
return None
126
128
127
- def __validate_desc (desc ):
129
+ def __validate_desc (desc : Union [ dict , str ], prefix : str ):
128
130
if isinstance (desc , dict ):
129
131
for k , v in desc .items ():
130
132
if not isinstance (k , str ):
@@ -134,7 +136,7 @@ def __validate_desc(desc):
134
136
135
137
try :
136
138
version = str (k )
137
- except :
139
+ except BaseException :
138
140
version = None
139
141
140
142
if version != k :
@@ -244,34 +246,36 @@ def __validate_etors(d, tags):
244
246
for i , item in enumerate (d ["etors" ]):
245
247
prefix = "'etors'[%s] " % i
246
248
if not isinstance (item , dict ):
247
- raise Exception (prefix + "must be a mapping: '%s'" % (i , type (item )))
249
+ raise Exception (
250
+ prefix + "must be a mapping: %d, '%s'" % (i , type (item ))
251
+ )
248
252
249
253
if ("desc" not in item ) or ("name" not in item ):
250
254
raise Exception (
251
255
prefix + "requires the following scalar fields: {`desc`, `name`}"
252
256
)
253
257
254
- if "extend" in d and d .get ("extend" ) == True and "value" not in item :
258
+ if "extend" in d and d .get ("extend" ) and "value" not in item :
255
259
raise Exception (
256
260
prefix
257
261
+ "must include a value for experimental features: {`value`: `0xabcd`}"
258
262
)
259
263
260
264
if typed :
261
- type = extract_type (item ["desc" ])
262
- if type is None :
265
+ ty = extract_type (item ["desc" ])
266
+ if ty is None :
263
267
raise Exception (
264
268
prefix
265
269
+ "typed etor "
266
270
+ item ["name" ]
267
271
+ " must begin with a type identifier: [type]"
268
272
)
269
- type_name = _subt (type , tags )
273
+ type_name = _subt (ty , tags )
270
274
if not is_iso (type_name ):
271
275
raise Exception (
272
276
prefix
273
277
+ "type "
274
- + str (type )
278
+ + str (ty )
275
279
+ " in a typed etor "
276
280
+ item ["name" ]
277
281
+ " must be a valid ISO C identifier"
@@ -331,7 +335,9 @@ def has_handle(members, meta):
331
335
if type_traits .is_handle (m ):
332
336
return True
333
337
if type_traits .is_struct (m , meta ):
334
- return has_handle (type_traits .get_struct_members (m ["type" ]), meta )
338
+ return has_handle (
339
+ type_traits .get_struct_members (m ["type" ], meta ), meta
340
+ )
335
341
return False
336
342
337
343
for m in members :
@@ -350,7 +356,7 @@ def has_handle(members, meta):
350
356
# exception messages.
351
357
__validate_struct_range_members (name , member_members , meta )
352
358
353
- def __validate_members (d , tags , meta ):
359
+ def __validate_members (d , meta ):
354
360
if "members" not in d :
355
361
raise Exception (
356
362
"'%s' requires the following sequence of mappings: {`members`}"
@@ -365,15 +371,17 @@ def __validate_members(d, tags, meta):
365
371
for i , item in enumerate (d ["members" ]):
366
372
prefix = "'members'[%s] " % i
367
373
if not isinstance (item , dict ):
368
- raise Exception (prefix + "must be a mapping: '%s'" % (i , type (item )))
374
+ raise Exception (
375
+ prefix + "must be a mapping: %d, '%s'" % (i , type (item ))
376
+ )
369
377
370
378
if ("desc" not in item ) or ("type" not in item ) or ("name" not in item ):
371
379
raise Exception (
372
380
prefix
373
381
+ "requires the following scalar fields: {`desc`, 'type', `name`}"
374
382
)
375
383
376
- annotation = __validate_desc (item ["desc" ])
384
+ annotation = __validate_desc (item ["desc" ], prefix )
377
385
if not annotation :
378
386
raise Exception (
379
387
prefix + "'desc' must start with {'[in]', '[out]', '[in,out]'}"
@@ -409,7 +417,7 @@ def __validate_members(d, tags, meta):
409
417
)
410
418
max_ver = ver
411
419
412
- def __validate_params (d , tags , meta ):
420
+ def __validate_params (d , meta ):
413
421
if "params" not in d :
414
422
raise Exception (
415
423
"'function' requires the following sequence of mappings: {`params`}"
@@ -420,19 +428,25 @@ def __validate_params(d, tags, meta):
420
428
421
429
d_ver = Version (d .get ("version" , default_version ))
422
430
max_ver = d_ver
423
- min = {"[in]" : None , "[out]" : None , "[in,out]" : None }
431
+ min : Dict [str , Union [int , None ]] = {
432
+ "[in]" : None ,
433
+ "[out]" : None ,
434
+ "[in,out]" : None ,
435
+ }
424
436
for i , item in enumerate (d ["params" ]):
425
437
prefix = "'params'[%s] " % i
426
438
if not isinstance (item , dict ):
427
- raise Exception (prefix + "must be a mapping: '%s'" % (i , type (item )))
439
+ raise Exception (
440
+ prefix + "must be a mapping: %d, '%s'" % (i , type (item ))
441
+ )
428
442
429
443
if ("desc" not in item ) or ("type" not in item ) or ("name" not in item ):
430
444
raise Exception (
431
445
prefix
432
446
+ "requires the following scalar fields: {`desc`, 'type', `name`}"
433
447
)
434
448
435
- annotation = __validate_desc (item ["desc" ])
449
+ annotation = __validate_desc (item ["desc" ], prefix )
436
450
if not annotation :
437
451
raise Exception (
438
452
prefix + "'desc' must start with {'[in]', '[out]', '[in,out]'}"
@@ -576,7 +590,7 @@ def __validate_union_tag(d):
576
590
__validate_union_tag (d )
577
591
__validate_type (d , "name" , tags )
578
592
__validate_base (d )
579
- __validate_members (d , tags , meta )
593
+ __validate_members (d , meta )
580
594
__validate_details (d )
581
595
__validate_ordinal (d )
582
596
__validate_version (d )
@@ -594,7 +608,7 @@ def __validate_union_tag(d):
594
608
else :
595
609
__validate_name (d , "name" , tags , case = "camel" )
596
610
597
- __validate_params (d , tags , meta )
611
+ __validate_params (d , meta )
598
612
__validate_details (d )
599
613
__validate_ordinal (d )
600
614
__validate_version (d )
@@ -734,6 +748,7 @@ def _generate_meta(d, ordinal, meta):
734
748
if "enum" == type :
735
749
value = - 1
736
750
max_value = - 1
751
+ max_index = - 1
737
752
bit_mask = 0
738
753
meta [type ][name ]["etors" ] = []
739
754
for idx , etor in enumerate (d ["etors" ]):
@@ -902,7 +917,8 @@ def append_nullchecks(param, accessor: str):
902
917
)
903
918
904
919
def append_enum_checks (param , accessor : str ):
905
- ptypename = type_traits .base (param ["type" ])
920
+ typename = type_traits .base (param ["type" ])
921
+ assert typename
906
922
907
923
prefix = "`"
908
924
if param_traits .is_optional (item ):
@@ -924,20 +940,19 @@ def append_enum_checks(param, accessor: str):
924
940
else :
925
941
if (
926
942
type_traits .is_flags (param ["type" ])
927
- and "bit_mask" in meta ["enum" ][ptypename ].keys ()
943
+ and "bit_mask" in meta ["enum" ][typename ].keys ()
928
944
):
929
945
_append (
930
946
rets ,
931
947
"$X_RESULT_ERROR_INVALID_ENUMERATION" ,
932
948
prefix
933
- + "%s & %s`" % (ptypename .upper ()[:- 2 ] + "_MASK" , accessor ),
949
+ + "%s & %s`" % (typename .upper ()[:- 2 ] + "_MASK" , accessor ),
934
950
)
935
951
else :
936
952
_append (
937
953
rets ,
938
954
"$X_RESULT_ERROR_INVALID_ENUMERATION" ,
939
- prefix
940
- + "%s < %s`" % (meta ["enum" ][ptypename ]["max" ], accessor ),
955
+ prefix + "%s < %s`" % (meta ["enum" ][typename ]["max" ], accessor ),
941
956
)
942
957
943
958
# generate results based on parameters
@@ -958,7 +973,7 @@ def append_enum_checks(param, accessor: str):
958
973
):
959
974
typename = type_traits .base (item ["type" ])
960
975
# walk each entry in the desc for pointers and enums
961
- for i , m in enumerate ( meta ["struct" ][typename ]["members" ]) :
976
+ for m in meta ["struct" ][typename ]["members" ]:
962
977
if param_traits .is_nocheck (m ):
963
978
continue
964
979
@@ -1050,8 +1065,9 @@ def _validate_ext_enum_range(extension, enum) -> bool:
1050
1065
if value in existing_values :
1051
1066
return False
1052
1067
return True
1053
- except :
1054
- return False
1068
+ except BaseException :
1069
+ pass
1070
+ return False
1055
1071
1056
1072
1057
1073
def _extend_enums (enum_extensions , specs , meta ):
@@ -1068,9 +1084,9 @@ def _extend_enums(enum_extensions, specs, meta):
1068
1084
for obj in s ["objects" ]
1069
1085
if obj ["type" ] == "enum" and k == obj ["name" ] and not obj .get ("extend" )
1070
1086
][0 ]
1071
- for i , extension in enumerate ( group ) :
1087
+ for extension in group :
1072
1088
if not _validate_ext_enum_range (extension , matching_enum ):
1073
- raise Exception (f "Invalid enum values." )
1089
+ raise Exception ("Invalid enum values." )
1074
1090
matching_enum ["etors" ].extend (extension ["etors" ])
1075
1091
1076
1092
_refresh_enum_meta (matching_enum , meta )
@@ -1118,7 +1134,7 @@ def parse(section, version, tags, meta, ref):
1118
1134
if not d :
1119
1135
continue
1120
1136
1121
- if d ["type" ] == "enum" and d .get ("extend" ) == True :
1137
+ if d ["type" ] == "enum" and d .get ("extend" ):
1122
1138
# enum extensions are resolved later
1123
1139
enum_extensions .append (d )
1124
1140
continue
0 commit comments