@@ -427,26 +427,24 @@ win32_warn_bytes_api()
427
427
#endif
428
428
429
429
static int
430
- _fd_converter (PyObject * o , int * p , int default_value ) {
431
- long long_value ;
432
- if (o == Py_None ) {
433
- * p = default_value ;
434
- return 1 ;
435
- }
436
- if (PyFloat_Check (o )) {
437
- PyErr_SetString (PyExc_TypeError ,
438
- "integer argument expected, got float" );
430
+ _fd_converter (PyObject * o , int * p , const char * allowed )
431
+ {
432
+ int overflow ;
433
+ long long_value = PyLong_AsLongAndOverflow (o , & overflow );
434
+ if (PyFloat_Check (o ) ||
435
+ (long_value == -1 && !overflow && PyErr_Occurred ())) {
436
+ PyErr_Clear ();
437
+ PyErr_Format (PyExc_TypeError ,
438
+ "argument should be %s, not %.200s" ,
439
+ allowed , Py_TYPE (o )-> tp_name );
439
440
return 0 ;
440
441
}
441
- long_value = PyLong_AsLong (o );
442
- if (long_value == -1 && PyErr_Occurred ())
443
- return 0 ;
444
- if (long_value > INT_MAX ) {
442
+ if (overflow > 0 || long_value > INT_MAX ) {
445
443
PyErr_SetString (PyExc_OverflowError ,
446
444
"signed integer is greater than maximum" );
447
445
return 0 ;
448
446
}
449
- if (long_value < INT_MIN ) {
447
+ if (overflow < 0 || long_value < INT_MIN ) {
450
448
PyErr_SetString (PyExc_OverflowError ,
451
449
"signed integer is less than minimum" );
452
450
return 0 ;
@@ -456,8 +454,13 @@ _fd_converter(PyObject *o, int *p, int default_value) {
456
454
}
457
455
458
456
static int
459
- dir_fd_converter (PyObject * o , void * p ) {
460
- return _fd_converter (o , (int * )p , DEFAULT_DIR_FD );
457
+ dir_fd_converter (PyObject * o , void * p )
458
+ {
459
+ if (o == Py_None ) {
460
+ * (int * )p = DEFAULT_DIR_FD ;
461
+ return 1 ;
462
+ }
463
+ return _fd_converter (o , (int * )p , "integer" );
461
464
}
462
465
463
466
@@ -634,17 +637,16 @@ path_converter(PyObject *o, void *p) {
634
637
}
635
638
else {
636
639
PyErr_Clear ();
637
- bytes = PyBytes_FromObject (o );
640
+ if (PyObject_CheckBuffer (o ))
641
+ bytes = PyBytes_FromObject (o );
642
+ else
643
+ bytes = NULL ;
638
644
if (!bytes ) {
639
645
PyErr_Clear ();
640
646
if (path -> allow_fd ) {
641
647
int fd ;
642
- /*
643
- * note: _fd_converter always permits None.
644
- * but we've already done our None check.
645
- * so o cannot be None at this point.
646
- */
647
- int result = _fd_converter (o , & fd , -1 );
648
+ int result = _fd_converter (o , & fd ,
649
+ "string, bytes or integer" );
648
650
if (result ) {
649
651
path -> wide = NULL ;
650
652
path -> narrow = NULL ;
@@ -705,15 +707,17 @@ argument_unavailable_error(char *function_name, char *argument_name) {
705
707
}
706
708
707
709
static int
708
- dir_fd_unavailable (PyObject * o , void * p ) {
709
- int * dir_fd = ( int * ) p ;
710
- int return_value = _fd_converter ( o , dir_fd , DEFAULT_DIR_FD ) ;
711
- if (!return_value )
710
+ dir_fd_unavailable (PyObject * o , void * p )
711
+ {
712
+ int dir_fd ;
713
+ if (!dir_fd_converter ( o , & dir_fd ) )
712
714
return 0 ;
713
- if (* dir_fd == DEFAULT_DIR_FD )
714
- return 1 ;
715
- argument_unavailable_error (NULL , "dir_fd" );
716
- return 0 ;
715
+ if (dir_fd != DEFAULT_DIR_FD ) {
716
+ argument_unavailable_error (NULL , "dir_fd" );
717
+ return 0 ;
718
+ }
719
+ * (int * )p = dir_fd ;
720
+ return 1 ;
717
721
}
718
722
719
723
static int
0 commit comments