@@ -3113,18 +3113,22 @@ class Py_off_t_return_converter(long_return_converter):
3113
3113
type = 'Py_off_t'
3114
3114
conversion_fn = 'PyLong_FromPy_off_t'
3115
3115
3116
- class path_confname_converter (CConverter):
3116
+ class confname_converter (CConverter):
3117
3117
type="int"
3118
- converter="conv_path_confname "
3118
+ converter="conv_confname "
3119
3119
3120
- class confstr_confname_converter(path_confname_converter ):
3121
- converter='conv_confstr_confname'
3120
+ def converter_init(self, *, table ):
3121
+ self.table = table
3122
3122
3123
- class sysconf_confname_converter(path_confname_converter):
3124
- converter="conv_sysconf_confname"
3123
+ def parse_arg(self, argname, displayname, *, limited_capi):
3124
+ return self.format_code("""
3125
+ if (!{converter}(module, {argname}, &{paramname}, "{table}")) {{{{
3126
+ goto exit;
3127
+ }}}}
3128
+ """, argname=argname, converter=self.converter, table=self.table)
3125
3129
3126
3130
[python start generated code]*/
3127
- /*[python end generated code: output=da39a3ee5e6b4b0d input=1860d32584c2a539 ]*/
3131
+ /*[python end generated code: output=da39a3ee5e6b4b0d input=8189d5ae78244626 ]*/
3128
3132
3129
3133
/*[clinic input]
3130
3134
@@ -13547,46 +13551,38 @@ struct constdef {
13547
13551
};
13548
13552
13549
13553
static int
13550
- conv_confname (PyObject * arg , int * valuep , struct constdef * table ,
13551
- size_t tablesize )
13554
+ conv_confname (PyObject * module , PyObject * arg , int * valuep , const char * tablename )
13552
13555
{
13553
- if (PyLong_Check (arg )) {
13554
- int value = PyLong_AsInt (arg );
13555
- if (value == -1 && PyErr_Occurred ())
13556
- return 0 ;
13557
- * valuep = value ;
13558
- return 1 ;
13559
- }
13560
- else {
13561
- /* look up the value in the table using a binary search */
13562
- size_t lo = 0 ;
13563
- size_t mid ;
13564
- size_t hi = tablesize ;
13565
- int cmp ;
13566
- const char * confname ;
13567
- if (!PyUnicode_Check (arg )) {
13568
- PyErr_SetString (PyExc_TypeError ,
13569
- "config
10000
uration names must be strings or integers" );
13556
+ if (PyUnicode_Check (arg )) {
13557
+ PyObject * table = PyObject_GetAttrString (module , tablename );
13558
+ if (table == NULL ) {
13570
13559
return 0 ;
13571
13560
}
13572
- confname = PyUnicode_AsUTF8 (arg );
13573
- if (confname == NULL )
13561
+
13562
+ arg = PyObject_GetItem (table , arg );
13563
+ Py_DECREF (table );
13564
+ if (arg == NULL ) {
13565
+ PyErr_SetString (
13566
+ PyExc_ValueError , "unrecognized configuration name" );
13574
13567
return 0 ;
13575
- while (lo < hi ) {
13576
- mid = (lo + hi ) / 2 ;
13577
- cmp = strcmp (confname , table [mid ].name );
13578
- if (cmp < 0 )
13579
- hi = mid ;
13580
- else if (cmp > 0 )
13581
- lo = mid + 1 ;
13582
- else {
13583
- * valuep = table [mid ].value ;
13584
- return 1 ;
13585
- }
13586
13568
}
13587
- PyErr_SetString ( PyExc_ValueError , "unrecognized configuration name" );
13588
- return 0 ;
13569
+ } else {
13570
+ Py_INCREF ( arg ); // Match the Py_DECREF below.
13589
13571
}
13572
+
13573
+ int success = 0 ;
13574
+ if (!PyLong_Check (arg )) {
13575
+ PyErr_SetString (PyExc_TypeError ,
13576
+ "configuration names must be strings or integers" );
13577
+ } else {
13578
+ int value = PyLong_AsInt (arg );
13579
+ if (!(value == -1 && PyErr_Occurred ())) {
13580
+ * valuep = value ;
13581
+ success = 1 ;
13582
+ }
13583
+ }
13584
+ Py_DECREF (arg );
13585
+ return success ;
13590
13586
}
13591
13587
13592
13588
@@ -13677,14 +13673,6 @@ static struct constdef posix_constants_pathconf[] = {
13677
13673
{"PC_TIMESTAMP_RESOLUTION" , _PC_TIMESTAMP_RESOLUTION },
13678
13674
#endif
13679
13675
};
13680
-
13681
- static int
13682
- conv_path_confname (PyObject * arg , int * valuep )
13683
- {
13684
- return conv_confname (arg , valuep , posix_constants_pathconf ,
13685
- sizeof (posix_constants_pathconf )
13686
- / sizeof (struct constdef ));
13687
- }
13688
13676
#endif
13689
13677
13690
13678
@@ -13693,7 +13681,7 @@ conv_path_confname(PyObject *arg, int *valuep)
13693
13681
os.fpathconf -> long
13694
13682
13695
13683
fd: fildes
13696
- name: path_confname
13684
+ name: confname(table="pathconf_names")
13697
13685
/
13698
13686
13699
13687
Return the configuration limit name for the file descriptor fd.
@@ -13703,7 +13691,7 @@ If there is no limit, return -1.
13703
13691
13704
13692
static long
13705
13693
os_fpathconf_impl (PyObject * module , int fd , int name )
13706
- /*[clinic end generated code: output=d5b7042425fc3e21 input=5b8d2471cfaae186 ]*/
13694
+ /*[clinic end generated code: output=d5b7042425fc3e21 input=023d44589c9ed6aa ]*/
13707
13695
{
13708
13696
long limit ;
13709
13697
@@ -13721,7 +13709,7 @@ os_fpathconf_impl(PyObject *module, int fd, int name)
13721
13709
/*[clinic input]
13722
13710
os.pathconf -> long
13723
13711
path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
13724
- name: path_confname
13712
+ name: confname(table="pathconf_names")
13725
13713
13726
13714
Return the configuration limit name for the file or directory path.
13727
13715
@@ -13732,7 +13720,7 @@ On some platforms, path may also be specified as an open file descriptor.
13732
13720
13733
13721
static long
13734
13722
os_pathconf_impl (PyObject * module , path_t * path , int name )
13735
- /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e ]*/
13723
+ /*[clinic end generated code: output=5bedee35b293a089 input=6f6072f57b10c787 ]*/
13736
13724
{
13737
13725
long limit ;
13738
13726
@@ -13909,27 +13897,19 @@ static struct constdef posix_constants_confstr[] = {
13909
13897
#endif
13910
13898
};
13911
13899
13912
- static int
13913
- conv_confstr_confname (PyObject * arg , int * valuep )
13914
- {
13915
- return conv_confname (arg , valuep , posix_constants_confstr ,
13916
- sizeof (posix_constants_confstr )
13917
- / sizeof (struct constdef ));
13918
- }
13919
-
13920
13900
13921
13901
/*[clinic input]
13922
13902
os.confstr
13923
13903
13924
- name: confstr_confname
13904
+ name: confname(table="confstr_names")
13925
13905
/
13926
13906
13927
13907
Return a string-valued system configuration variable.
13928
13908
[clinic start generated code]*/
13929
13909
13930
13910
static PyObject *
13931
13911
os_confstr_impl (PyObject * module , int name )
13932
- /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65 ]*/
13912
+ /*[clinic end generated code: output=bfb0b1b1e49b9383 input=4c6ffca2837ec959 ]*/
13933
13913
{
13934
13914
PyObject * result = NULL ;
13935
13915
char buffer [255 ];
@@ -14466,26 +14446,18 @@ static struct constdef posix_constants_sysconf[] = {
14466
14446
#endif
14467
14447
};
14468
14448
14469
- static int
14470
- conv_sysconf_confname (PyObject * arg , int * valuep )
14471
- {
14472
- return conv_confname (arg , valuep , posix_constants_sysconf ,
14473
- sizeof (posix_constants_sysconf )
14474
- / sizeof (struct constdef ));
14475
- }
14476
-
14477
14449
14478
14450
/*[clinic input]
14479
14451
os.sysconf -> long
14480
- name: sysconf_confname
14452
+ name: confname(table="sysconf_names")
14481
14453
/
14482
14454
14483
14455
Return an integer-valued system configuration variable.
14484
14456
[clinic start generated code]*/
14485
14457
14486
14458
static long
14487
14459
os_sysconf_impl (PyObject * module , int name )
14488
- /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4 ]*/
14460
+ /*[clinic end generated code: output=3662f945fc0cc756 input=930b8f23b5d15086 ]*/
14489
14461
{
14490
14462
long value ;
14491
14463
@@ -14498,40 +14470,15 @@ os_sysconf_impl(PyObject *module, int name)
14498
14470
#endif /* HAVE_SYSCONF */
14499
14471
14500
14472
14501
- /* This code is used to ensure that the tables of configuration value names
14502
- * are in sorted order as required by conv_confname(), and also to build
14503
- * the exported dictionaries that are used to publish information about the
14504
- * names available on the host platform.
14505
- *
14506
- * Sorting the table at runtime ensures that the table is properly ordered
14507
- * when used, even for platforms we're not able to test on. It also makes
14508
- * it easier to add additional entries to the tables.
14509
- */
14510
-
14511
- static int
14512
- cmp_constdefs (const void * v1 , const void * v2 )
14513
- {
14514
- const struct constdef * c1 =
14515
- (const struct constdef * ) v1 ;
14516
- const struct constdef * c2 =
14517
- (const struct constdef * ) v2 ;
14518
-
14519
- return strcmp (c1 -> name , c2 -> name );
14520
- }
14521
-
14522
14473
static int
14523
14474
setup_confname_table (struct constdef * table , size_t tablesize ,
14524
14475
const char * tablename , PyObject * module )
14525
14476
{
14526
- PyObject * d = NULL ;
14527
- size_t i ;
14528
-
14529
- qsort (table , tablesize , sizeof (struct constdef ), cmp_constdefs );
14530
- d = PyDict_New ();
14477
+ PyObject * d = PyDict_New ();
14531
14478
if (d == NULL )
14532
14479
return -1 ;
14533
14480
14534
- for (i = 0 ; i < tablesize ; ++ i ) {
14481
+ for (size_t i = 0 ; i < tablesize ; ++ i ) {
14535
14482
PyObject * o = PyLong_FromLong (table [i ].value );
14536
14483
if (o == NULL || PyDict_SetItemString (d , table [i ].name , o ) == -1 ) {
14537
14484
Py_XDECREF (o );
0 commit comments