@@ -82,6 +82,7 @@ def __repr__(self) -> str:
82
82
83
83
Appender = Callable [[str ], None ]
84
84
Outputter = Callable [[], str ]
85
+ TemplateDict = dict [str , str ]
85
86
86
87
class _TextAccumulator (NamedTuple ):
87
88
text : list [str ]
@@ -1969,20 +1970,6 @@ def dump(self):
1969
1970
extensions ['py' ] = PythonLanguage
1970
1971
1971
1972
1972
- # maps strings to callables.
1973
- # these callables must be of the form:
1974
- # def foo(name, default, *, ...)
1975
- # The callable may have any number of keyword-only parameters.
1976
- # The callable must return a CConverter object.
1977
- # The callable should not call builtins.print.
1978
- converters = {}
1979
-
1980
- # maps strings to callables.
1981
- # these callables follow the same rules as those for "converters" above.
1982
- # note however that they will never be called with keyword-only parameters.
1983
- legacy_converters = {}
1984
-
1985
-
1986
1973
# maps strings to callables.
1987
1974
# these callables must be of the form:
1988
1975
# def foo(*, ...)
@@ -2956,7 +2943,7 @@ def parse_arg(self, argname, displayname):
2956
2943
""" .format (argname = argname , paramname = self .parser_name , cast = cast )
2957
2944
return None
2958
2945
2959
- def set_template_dict (self , template_dict : dict [ str , str ]) :
2946
+ def set_template_dict (self , template_dict : TemplateDict ) -> None :
2960
2947
pass
2961
2948
2962
2949
@property
@@ -2979,6 +2966,23 @@ def parser_name(self):
2979
2966
}
2980
2967
2981
2968
2969
+ ConverterType = Callable [..., CConverter ]
2970
+ ConverterDict = dict [str , ConverterType ]
2971
+
2972
+ # maps strings to callables.
2973
+ # these callables must be of the form:
2974
+ # def foo(name, default, *, ...)
2975
+ # The callable may have any number of keyword-only parameters.
2976
+ # The callable must return a CConverter object.
2977
+ # The callable should not call builtins.print.
2978
+ converters : ConverterDict = {}
2979
+
2980
+ # maps strings to callables.
2981
+ # these callables follow the same rules as those for "converters" above.
2982
+ # note however that they will never be called with keyword-only parameters.
2983
+ legacy_converters : ConverterDict = {}
2984
+
2985
+
2982
2986
class bool_converter (CConverter ):
2983
2987
type = 'int'
2984
2988
default_type = bool
@@ -2994,7 +2998,7 @@ def converter_init(self, *, accept={object}):
2994
2998
self .default = bool (self .default )
2995
2999
self .c_default = str (int (self .default ))
2996
3000
2997
- def parse_arg (self , argname , displayname ) :
3001
+ def parse_arg (self , argname : str , displayname : str ) -> str :
2998
3002
if self .format_unit == 'i' :
2999
3003
return """
3000
3004
{paramname} = _PyLong_AsInt({argname});
@@ -3020,10 +3024,10 @@ class defining_class_converter(CConverter):
3020
3024
format_unit = ''
3021
3025
show_in_signature = False
3022
3026
3023
- def converter_init (self , * , type = None ):
3027
+ def converter_init (self , * , type = None ) -> None :
3024
3028
self .specified_type = type
3025
3029
3026
- def render (self , parameter , data ):
3030
+ def render (self , parameter , data ) -> None :
3027
3031
self ._render_self (parameter , data )
3028
3032
3029
3033
def set_template_dict (self , template_dict ):
@@ -3036,7 +3040,7 @@ class char_converter(CConverter):
3036
3040
format_unit = 'c'
3037
3041
c_ignored_default = "'\0 '"
3038
3042
3039
- def converter_init (self ):
3043
+ def converter_init (self ) -> None :
3040
3044
if isinstance (self .default , self .default_type ):
3041
3045
if len (self .default ) != 1 :
3042
3046
fail ("char_converter: illegal default value " + repr (self .default ))
@@ -3045,7 +3049,7 @@ def converter_init(self):
3045
3049
if self .c_default == '"\' "' :
3046
3050
self .c_default = r"'\''"
3047
3051
3048
- def parse_arg (self , argname , displayname ) :
3052
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3049
3053
if self .format_unit == 'c' :
3050
3054
return """
3051
3055
if (PyBytes_Check({argname}) && PyBytes_GET_SIZE({argname}) == 1) {{{{
@@ -3070,11 +3074,11 @@ class unsigned_char_converter(CConverter):
3070
3074
format_unit = 'b'
3071
3075
c_ignored_default = "'\0 '"
3072
3076
3073
- def converter_init (self , * , bitwise = False ):
3077
+ def converter_init (self , * , bitwise : bool = False ) -> None :
3074
3078
if bitwise :
3075
3079
self .format_unit = 'B'
3076
3080
3077
- def parse_arg (self , argname , displayname ) :
3081
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3078
3082
if self .format_unit == 'b' :
3079
3083
return """
3080
3084
{{{{
@@ -3119,7 +3123,7 @@ class short_converter(CConverter):
3119
3123
format_unit = 'h'
3120
3124
c_ignored_default = "0"
3121
3125
3122
- def parse_arg (self , argname , displayname ) :
3126
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3123
3127
if self .format_unit == 'h' :
3124
3128
return """
3125
3129
{{{{
@@ -3149,13 +3153,13 @@ class unsigned_short_converter(CConverter):
3149
3153
default_type = int
3150
3154
c_ignored_default = "0"
3151
3155
3152
- def converter_init (self , * , bitwise = False ):
3156
+ def converter_init (self , * , bitwise : bool = False ) -> None :
3153
3157
if bitwise :
3154
3158
self .format_unit = 'H'
3155
3159
else :
3156
3160
self .converter = '_PyLong_UnsignedShort_Converter'
3157
3161
3158
- def parse_arg (self , argname , displayname ) :
3162
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3159
3163
if self .format_unit == 'H' :
3160
3164
return """
3161
3165
{paramname} = (unsigned short)PyLong_AsUnsignedLongMask({argname});
@@ -3172,15 +3176,15 @@ class int_converter(CConverter):
3172
3176
format_unit = 'i'
3173
3177
c_ignored_default = "0"
3174
3178
3175
- def converter_init (self , * , accept = {int }, type = None ):
3179
+ def converter_init (self , * , accept = {int }, type = None ) -> None :
3176
3180
if accept == {str }:
3177
3181
self .format_unit = 'C'
3178
3182
elif accept != {int }:
3179
3183
fail ("int_converter: illegal 'accept' argument " + repr (accept ))
3180
3184
if type is not None :
3181
3185
self .type = type
3182
3186
3183
- def parse_arg (self , argname , displayname ) :
3187
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3184
3188
if self .format_unit == 'i' :
3185
3189
return """
3186
3190
{paramname} = _PyLong_AsInt({argname});
@@ -3211,13 +3215,13 @@ class unsigned_int_converter(CConverter):
3211
3215
default_type = int
3212
3216
c_ignored_default = "0"
3213
3217
3214
- def converter_init (self , * , bitwise = False ):
3218
+ def converter_init (self , * , bitwise : bool = False ) -> None :
3215
3219
if bitwise :
3216
3220
self .format_unit = 'I'
3217
3221
else :
3218
3222
self .converter = '_PyLong_UnsignedInt_Converter'
3219
3223
3220
- def parse_arg (self , argname , displayname ) :
3224
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3221
3225
if self .format_unit == 'I' :
3222
3226
return """
3223
3227
{paramname} = (unsigned int)PyLong_AsUnsignedLongMask({argname});
@@ -3233,7 +3237,7 @@ class long_converter(CConverter):
3233
3237
format_unit = 'l'
3234
3238
c_ignored_default = "0"
3235
3239
3236
- def parse_arg (self , argname , displayname ) :
3240
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3237
3241
if self .format_unit == 'l' :
3238
3242
return """
3239
3243
{paramname} = PyLong_AsLong({argname});
@@ -3248,13 +3252,13 @@ class unsigned_long_converter(CConverter):
3248
3252
default_type = int
3249
3253
c_ignored_default = "0"
3250
3254
3251
- def converter_init (self , * , bitwise = False ):
3255
+ def converter_init (self , * , bitwise : bool = False ) -> None :
3252
3256
if bitwise :
3253
3257
self .format_unit = 'k'
3254
3258
else :
3255
3259
self .converter = '_PyLong_UnsignedLong_Converter'
3256
3260
3257
- def parse_arg (self , argname , displayname ) :
3261
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3258
3262
if self .format_unit == 'k' :
3259
3263
return """
3260
3264
if (!PyLong_Check({argname})) {{{{
@@ -3272,7 +3276,7 @@ class long_long_converter(CConverter):
3272
3276
format_unit = 'L'
3273
3277
c_ignored_default = "0"
3274
3278
3275
- def parse_arg (self , argname , displayname ) :
3279
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3276
3280
if self .format_unit == 'L' :
3277
3281
return """
3278
3282
{paramname} = PyLong_AsLongLong({argname});
@@ -3287,13 +3291,13 @@ class unsigned_long_long_converter(CConverter):
3287
3291
default_type = int
3288
3292
c_ignored_default = "0"
3289
3293
3290
- def converter_init (self , * , bitwise = False ):
3294
+ def converter_init (self , * , bitwise : bool = False ) -> None :
3291
3295
if bitwise :
3292
3296
self .format_unit = 'K'
3293
3297
else :
3294
3298
self .converter = '_PyLong_UnsignedLongLong_Converter'
3295
3299
3296
- def parse_arg (self , argname , displayname ) :
3300
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3297
3301
if self .format_unit == 'K' :
3298
3302
return """
3299
3303
if (!PyLong_Check({argname})) {{{{
@@ -3309,7 +3313,7 @@ class Py_ssize_t_converter(CConverter):
3309
3313
type = 'Py_ssize_t'
3310
3314
c_ignored_default = "0"
3311
3315
3312
- def converter_init (self , * , accept = {int }):
3316
+ def converter_init (self , * , accept = {int }) -> None :
3313
3317
if accept == {int }:
3314
3318
self .format_unit = 'n'
3315
3319
self .default_type = int
@@ -3318,7 +3322,7 @@ def converter_init(self, *, accept={int}):
3318
3322
else :
3319
3323
fail ("Py_ssize_t_converter: illegal 'accept' argument " + repr (accept ))
3320
3324
3321
- def parse_arg (self , argname , displayname ) :
3325
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3322
3326
if self .format_unit == 'n' :
3323
3327
return """
3324
3328
{{{{
@@ -3340,7 +3344,7 @@ def parse_arg(self, argname, displayname):
3340
3344
class slice_index_converter (CConverter ):
3341
3345
type = 'Py_ssize_t'
3342
3346
3343
- def converter_init (self , * , accept = {int , NoneType }):
3347
+ def converter_init (self , * , accept = {int , NoneType }) -> None :
3344
3348
if accept == {int }:
3345
3349
self .converter = '_PyEval_SliceIndexNotNone'
3346
3350
elif accept == {int , NoneType }:
@@ -3353,7 +3357,7 @@ class size_t_converter(CConverter):
3353
3357
converter = '_PyLong_Size_t_Converter'
3354
3358
c_ignored_default = "0"
3355
3359
3356
- def parse_arg (self , argname , displayname ) :
3360
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3357
3361
if self .format_unit == 'n' :
3358
3362
return """
3359
3363
{paramname} = PyNumber_AsSsize_t({argname}, PyExc_OverflowError);
@@ -3368,7 +3372,7 @@ class fildes_converter(CConverter):
3368
3372
type = 'int'
3369
3373
converter = '_PyLong_FileDescriptor_Converter'
3370
3374
3371
- def _parse_arg (self , argname , displayname ) :
3375
+ def _parse_arg (self , argname : str , displayname : str ) -> str :
3372
3376
return """
3373
3377
{paramname} = PyObject_AsFileDescriptor({argname});
3374
3378
if ({paramname} == -1) {{{{
@@ -3383,7 +3387,7 @@ class float_converter(CConverter):
3383
3387
format_unit = 'f'
3384
3388
c_ignored_default = "0.0"
3385
3389
3386
- def parse_arg (self , argname , displayname ) :
3390
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3387
3391
if self .format_unit == 'f' :
3388
3392
return """
3389
3393
if (PyFloat_CheckExact({argname})) {{{{
@@ -3405,7 +3409,7 @@ class double_converter(CConverter):
3405
3409
format_unit = 'd'
3406
3410
c_ignored_default = "0.0"
3407
3411
3408
- def parse_arg (self , argname , displayname ) :
3412
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3409
3413
if self .format_unit == 'd' :
3410
3414
return """
3411
3415
if (PyFloat_CheckExact({argname})) {{{{
@@ -3428,7 +3432,7 @@ class Py_complex_converter(CConverter):
3428
3432
format_unit = 'D'
3429
3433
c_ignored_default = "{0.0, 0.0}"
3430
3434
3431
- def parse_arg (self , argname , displayname ) :
3435
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3432
3436
if self .format_unit == 'D' :
3433
3437
return """
3434
3438
{paramname} = PyComplex_AsCComplex({argname});
@@ -3504,7 +3508,7 @@ def post_parsing(self):
3504
3508
name = self .name
3505
3509
return f"PyMem_FREE({ name } );\n "
3506
3510
3507
- def parse_arg (self , argname , displayname ) :
3511
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3508
3512
if self .format_unit == 's' :
3509
3513
return """
3510
3514
if (!PyUnicode_Check({argname})) {{{{
@@ -3599,7 +3603,7 @@ class PyBytesObject_converter(CConverter):
3599
3603
format_unit = 'S'
3600
3604
# accept = {bytes}
3601
3605
3602
- def parse_arg (self , argname , displayname ) :
3606
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3603
3607
if self .format_unit == 'S' :
3604
3608
return """
3605
3609
if (!PyBytes_Check({argname})) {{{{
@@ -3616,7 +3620,7 @@ class PyByteArrayObject_converter(CConverter):
3616
3620
format_unit = 'Y'
3617
3621
# accept = {bytearray}
3618
3622
3619
- def parse_arg (self , argname , displayname ) :
3623
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3620
3624
if self .format_unit == 'Y' :
3621
3625
return """
3622
3626
if (!PyByteArray_Check({argname})) {{{{
@@ -3633,7 +3637,7 @@ class unicode_converter(CConverter):
3633
3637
default_type = (str , Null , NoneType )
3634
3638
format_unit = 'U'
3635
3639
3636
- def parse_arg (self , argname , displayname ) :
3640
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3637
3641
if self .format_unit == 'U' :
3638
3642
return """
3639
3643
if (!PyUnicode_Check({argname})) {{{{
@@ -3656,7 +3660,7 @@ class Py_UNICODE_converter(CConverter):
3656
3660
type = 'const Py_UNICODE *'
3657
3661
default_type = (str , Null , NoneType )
3658
3662
3659
- def converter_init (self , * , accept = {str }, zeroes = False ):
3663
+ def converter_init (self , * , accept = {str }, zeroes : bool = False ) -> None :
3660
3664
format_unit = 'Z' if accept == {str , NoneType } else 'u'
3661
3665
if zeroes :
3662
3666
format_unit += '#'
@@ -3678,7 +3682,7 @@ def cleanup(self):
3678
3682
PyMem_Free((void *){name});
3679
3683
""" .format (name = self .name )
3680
3684
3681
- def parse_arg (self , argname , argnum ) :
3685
+ def parse_arg (self , argname : str , argnum : str ) -> str :
3682
3686
if not self .length :
3683
3687
if self .accept == {str }:
3684
3688
return """
@@ -3718,7 +3722,7 @@ class Py_buffer_converter(CConverter):
3718
3722
impl_by_reference = True
3719
3723
c_ignored_default = "{NULL, NULL}"
3720
3724
3721
- def converter_init (self , * , accept = {buffer }):
3725
+ def converter_init (self , * , accept = {buffer }) -> None :
3722
3726
if self .default not in (unspecified , None ):
3723
3727
fail ("The only legal default value for Py_buffer is None." )
3724
3728
@@ -3741,7 +3745,7 @@ def cleanup(self):
3741
3745
name = self .name
3742
3746
return "" .join (["if (" , name , ".obj) {\n PyBuffer_Release(&" , name , ");\n }\n " ])
3743
3747
3744
- def parse_arg (self , argname , displayname ) :
3748
+ def parse_arg (self , argname : str , displayname : str ) -> str :
3745
3749
if self .format_unit == 'y*' :
3746
3750
return """
3747
3751
if (PyObject_GetBuffer({argname}, &{paramname}, PyBUF_SIMPLE) != 0) {{{{
@@ -3790,7 +3794,7 @@ def parse_arg(self, argname, displayname):
3790
3794
return super ().parse_arg (argname , displayname )
3791
3795
3792
3796
3793
- def correct_name_for_self (f ):
3797
+ def correct_name_for_self (f ) -> tuple [ str , str ] :
3794
3798
if f .kind in (CALLABLE , METHOD_INIT ):
3795
3799
if f .cls :
3796
3800
return "PyObject *" , "self"
@@ -3816,7 +3820,7 @@ class self_converter(CConverter):
3816
3820
type = None
3817
3821
format_unit = ''
3818
3822
3819
- def converter_init (self , * , type = None ):
3823
+ def converter_init (self , * , type = None ) -> None :
3820
3824
self .specified_type = type
3821
3825
3822
3826
def pre_render (self ):
0 commit comments