@@ -760,6 +760,59 @@ class CLanguage(Language):
760
760
stop_line = "[{dsl_name} start generated code]*/"
761
761
checksum_line = "/*[{dsl_name} end generated code: {arguments}]*/"
762
762
763
+ PARSER_PROTOTYPE_KEYWORD : Final [str ] = normalize_snippet ("""
764
+ static PyObject *
765
+ {c_basename}({self_type}{self_name}, PyObject *args, PyObject *kwargs)
766
+ """ )
767
+ PARSER_PROTOTYPE_KEYWORD___INIT__ : Final [str ] = normalize_snippet ("""
768
+ static int
769
+ {c_basename}({self_type}{self_name}, PyObject *args, PyObject *kwargs)
770
+ """ )
771
+ PARSER_PROTOTYPE_VARARGS : Final [str ] = normalize_snippet ("""
772
+ static PyObject *
773
+ {c_basename}({self_type}{self_name}, PyObject *args)
774
+ """ )
775
+ PARSER_PROTOTYPE_FASTCALL : Final [str ] = normalize_snippet ("""
776
+ static PyObject *
777
+ {c_basename}({self_type}{self_name}, PyObject *const *args, Py_ssize_t nargs)
778
+ """ )
779
+ PARSER_PROTOTYPE_FASTCALL_KEYWORDS : Final [str ] = normalize_snippet ("""
780
+ static PyObject *
781
+ {c_basename}({self_type}{self_name}, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
782
+ """ )
783
+ PARSER_PROTOTYPE_DEF_CLASS : Final [str ] = normalize_snippet ("""
784
+ static PyObject *
785
+ {c_basename}({self_type}{self_name}, PyTypeObject *{defining_class_name}, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
786
+ """ )
787
+ PARSER_PROTOTYPE_NOARGS : Final [str ] = normalize_snippet ("""
788
+ static PyObject *
789
+ {c_basename}({self_type}{self_name}, PyObject *Py_UNUSED(ignored))
790
+ """ )
791
+ METH_O_PROTOTYPE : Final [str ] = normalize_snippet ("""
792
+ static PyObject *
793
+ {c_basename}({impl_parameters})
794
+ """ )
795
+ DOCSTRING_PROTOTYPE_VAR : Final [str ] = normalize_snippet ("""
796
+ PyDoc_VAR({c_basename}__doc__);
797
+ """ )
798
+ DOCSTRING_PROTOTYPE_STRVAR : Final [str ] = normalize_snippet ("""
799
+ PyDoc_STRVAR({c_basename}__doc__,
800
+ {docstring});
801
+ """ )
802
+ IMPL_DEFINITION_PROTOTYPE : Final [str ] = normalize_snippet ("""
803
+ static {impl_return_type}
804
+ {c_basename}_impl({impl_parameters})
805
+ """ )
806
+ METHODDEF_PROTOTYPE_DEFINE : Final [str ] = normalize_snippet (r"""
807
+ #define {methoddef_name} \
808
+ {{"{name}", {methoddef_cast}{c_basename}{methoddef_cast_end}, {methoddef_flags}, {c_basename}__doc__}},
809
+ """ )
810
+ METHODDEF_PROTOTYPE_IFNDEF : Final [str ] = normalize_snippet ("""
811
+ #ifndef {methoddef_name}
812
+ #define {methoddef_name}
813
+ #endif /* !defined({methoddef_name}) */
814
+ """ )
815
+
763
816
def __init__ (self , filename : str ) -> None :
764
817
super ().__init__ (filename )
765
818
self .cpp = cpp .Monitor (filename )
@@ -862,52 +915,15 @@ def output_templates(
862
915
# methoddef_ifndef
863
916
864
917
return_value_declaration = "PyObject *return_value = NULL;"
865
-
866
- methoddef_define = normalize_snippet ("""
867
- #define {methoddef_name} \\
868
- {{"{name}", {methoddef_cast}{c_basename}{methoddef_cast_end}, {methoddef_flags}, {c_basename}__doc__}},
869
- """ )
918
+ methoddef_define = self .METHODDEF_PROTOTYPE_DEFINE
870
919
if new_or_init and not f .docstring :
871
920
docstring_prototype = docstring_definition = ''
872
921
else :
873
- docstring_prototype = normalize_snippet ("""
874
- PyDoc_VAR({c_basename}__doc__);
875
- """ )
876
- docstring_definition = normalize_snippet ("""
877
- PyDoc_STRVAR({c_basename}__doc__,
878
- {docstring});
879
- """ )
880
- impl_definition = normalize_snippet ("""
881
- static {impl_return_type}
882
- {c_basename}_impl({impl_parameters})
883
- """ )
922
+ docstring_prototype = self .DOCSTRING_PROTOTYPE_VAR
923
+ docstring_definition = self .DOCSTRING_PROTOTYPE_STRVAR
924
+ impl_definition = self .IMPL_DEFINITION_PROTOTYPE
884
925
impl_prototype = parser_prototype = parser_definition = None
885
926
886
- parser_prototype_keyword = normalize_snippet ("""
887
- static PyObject *
888
- {c_basename}({self_type}{self_name}, PyObject *args, PyObject *kwargs)
889
- """ )
890
-
891
- parser_prototype_varargs = normalize_snippet ("""
892
- static PyObject *
893
- {c_basename}({self_type}{self_name}, PyObject *args)
894
- """ )
895
-
896
- parser_prototype_fastcall = normalize_snippet ("""
897
- static PyObject *
898
- {c_basename}({self_type}{self_name}, PyObject *const *args, Py_ssize_t nargs)
899
- """ )
900
-
901
- parser_prototype_fastcall_keywords = normalize_snippet ("""
902
- static PyObject *
903
- {c_basename}({self_type}{self_name}, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
904
- """ )
905
-
906
- parser_prototype_def_class = normalize_snippet ("""
907
- static PyObject *
908
- {c_basename}({self_type}{self_name}, PyTypeObject *{defining_class_name}, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
909
- """ )
910
-
911
927
# parser_body_fields remembers the fields passed in to the
912
928
# previous call to parser_body. this is used for an awful hack.
913
929
parser_body_fields : tuple [str , ...] = ()
@@ -950,19 +966,13 @@ def parser_body(
950
966
if not requires_defining_class :
951
967
# no parameters, METH_NOARGS
952
968
flags = "METH_NOARGS"
953
-
954
- parser_prototype = normalize_snippet ("""
955
- static PyObject *
956
- {c_basename}({self_type}{self_name}, PyObject *Py_UNUSED(ignored))
957
- """ )
969
+ parser_prototype = self .PARSER_PROTOTYPE_NOARGS
958
970
parser_code = []
959
-
960
971
else :
961
972
assert not new_or_init
962
973
963
974
flags = "METH_METHOD|METH_FASTCALL|METH_KEYWORDS"
964
-
965
- parser_prototype = parser_prototype_def_class
975
+ parser_prototype = self .PARSER_PROTOTYPE_DEF_CLASS
966
976
return_error = ('return NULL;' if default_return_converter
967
977
else 'goto exit;' )
968
978
parser_code = [normalize_snippet ("""
@@ -987,10 +997,7 @@ def parser_body(
987
997
988
998
if (isinstance (converters [0 ], object_converter ) and
989
999
converters [0 ].format_unit == 'O' ):
990
- meth_o_prototype = normalize_snippet ("""
991
- static PyObject *
992
- {c_basename}({impl_parameters})
993
- """ )
1000
+ meth_o_prototype = self .METH_O_PROTOTYPE
994
1001
995
1002
if default_return_converter :
996
1003
# maps perfectly to METH_O, doesn't need a return converter.
@@ -1030,8 +1037,7 @@ def parser_body(
1030
1037
# in a big switch statement)
1031
1038
1032
1039
flags = "METH_VARARGS"
1033
- parser_prototype = parser_prototype_varargs
1034
-
1040
+ parser_prototype = self .PARSER_PROTOTYPE_VARARGS
1035
1041
parser_definition = parser_body (parser_prototype , ' {option_group_parsing}' )
1036
1042
1037
1043
elif not requires_defining_class and pos_only == len (parameters ) - pseudo_args :
@@ -1040,15 +1046,15 @@ def parser_body(
1040
1046
# we only need one call to _PyArg_ParseStack
1041
1047
1042
1048
flags = "METH_FASTCALL"
1043
- parser_prototype = parser_prototype_fastcall
1049
+ parser_prototype = self . PARSER_PROTOTYPE_FASTCALL
1044
1050
nargs = 'nargs'
1045
1051
argname_fmt = 'args[%d]'
1046
1052
else :
1047
1053
# positional-only, but no option groups
1048
1054
# we only need one call to PyArg_ParseTuple
1049
1055
1050
1056
flags = "METH_VARARGS"
1051
- parser_prototype = parser_prototype_varargs
1057
+ parser_prototype = self . PARSER_PROTOTYPE_VARARGS
1052
1058
nargs = 'PyTuple_GET_SIZE(args)'
1053
1059
argname_fmt = 'PyTuple_GET_ITEM(args, %d)'
1054
1060
@@ -1145,7 +1151,7 @@ def parser_body(
1145
1151
nargs = f"Py_MIN(nargs, { max_pos } )" if max_pos else "0"
1146
1152
if not new_or_init :
1147
1153
flags = "METH_FASTCALL|METH_KEYWORDS"
1148
- parser_prototype = parser_prototype_fastcall_keywords
1154
+ parser_prototype = self . PARSER_PROTOTYPE_FASTCALL_KEYWORDS
1149
1155
argname_fmt = 'args[%d]'
1150
1156
declarations = declare_parser (f )
1151
1157
declarations += "\n PyObject *argsbuf[%s];" % len (converters )
@@ -1160,7 +1166,7 @@ def parser_body(
1160
1166
else :
1161
1167
# positional-or-keyword arguments
1162
1168
flags = "METH_VARARGS|METH_KEYWORDS"
1163
- parser_prototype = parser_prototype_keyword
1169
+ parser_prototype = self . PARSER_PROTOTYPE_KEYWORD
1164
1170
argname_fmt = 'fastargs[%d]'
1165
1171
declarations = declare_parser (f )
1166
1172
declarations += "\n PyObject *argsbuf[%s];" % len (converters )
@@ -1177,7 +1183,7 @@ def parser_body(
1177
1183
1178
1184
if requires_defining_class :
1179
1185
flags = 'METH_METHOD|' + flags
1180
- parser_prototype = parser_prototype_def_class
1186
+ parser_prototype = self . PARSER_PROTOTYPE_DEF_CLASS
1181
1187
1182
1188
add_label : str | None = None
1183
1189
for i , p in enumerate (parameters ):
@@ -1264,13 +1270,10 @@ def parser_body(
1264
1270
methoddef_define = ''
1265
1271
1266
1272
if f .kind is METHOD_NEW :
1267
- parser_prototype = parser_prototype_keyword
1273
+ parser_prototype = self . PARSER_PROTOTYPE_KEYWORD
1268
1274
else :
1269
1275
return_value_declaration = "int return_value = -1;"
1270
- parser_prototype = normalize_snippet ("""
1271
- static int
1272
- {c_basename}({self_type}{self_name}, PyObject *args, PyObject *kwargs)
1273
- """ )
1276
+ parser_prototype = self .PARSER_PROTOTYPE_KEYWORD___INIT__
1274
1277
1275
1278
fields = list (parser_body_fields )
1276
1279
parses_positional = 'METH_NOARGS' not in flags
@@ -1323,12 +1326,7 @@ def parser_body(
1323
1326
1324
1327
if methoddef_define and f .full_name not in clinic .ifndef_symbols :
1325
1328
clinic .ifndef_symbols .add (f .full_name )
1326
- methoddef_ifndef = normalize_snippet ("""
1327
- #ifndef {methoddef_name}
1328
- #define {methoddef_name}
1329
- #endif /* !defined({methoddef_name}) */
1330
- """ )
1331
-
1329
+ methoddef_ifndef = self .METHODDEF_PROTOTYPE_IFNDEF
1332
1330
1333
1331
# add ';' to the end of parser_prototype and impl_prototype
1334
1332
# (they mustn't be None, but they could be an empty string.)
0 commit comments