@@ -2396,21 +2396,26 @@ _posix_free(void *module)
2396
2396
_posix_clear ((PyObject * )module );
2397
2397
}
2398
2398
2399
- static void
2399
+ static int
2400
2400
fill_time (PyObject * module , PyObject * v , int s_index , int f_index , int ns_index , time_t sec , unsigned long nsec )
2401
2401
{
2402
- PyObject * s = _PyLong_FromTime_t (sec );
2403
- PyObject * ns_fractional = PyLong_FromUnsignedLong (nsec );
2402
+ assert (!PyErr_Occurred ());
2403
+
2404
+ int res = -1 ;
2404
2405
PyObject * s_in_ns = NULL ;
2405
2406
PyObject * ns_total = NULL ;
2406
2407
PyObject * float_s = NULL ;
2407
2408
2408
- if (!(s && ns_fractional ))
2409
+ PyObject * s = _PyLong_FromTime_t (sec );
2410
+ PyObject * ns_fractional = PyLong_FromUnsignedLong (nsec );
2411
+ if (!(s && ns_fractional )) {
2409
2412
goto exit ;
2413
+ }
2410
2414
2411
2415
s_in_ns = PyNumber_Multiply (s , get_posix_state (module )-> billion );
2412
- if (!s_in_ns )
2416
+ if (!s_in_ns ) {
2413
2417
goto exit ;
2418
+ }
2414
2419
2415
2420
ns_total = PyNumber_Add (s_in_ns , ns_fractional );
2416
2421
if (!ns_total )
@@ -2433,12 +2438,17 @@ fill_time(PyObject *module, PyObject *v, int s_index, int f_index, int ns_index,
2433
2438
PyStructSequence_SET_ITEM (v , ns_index , ns_total );
2434
2439
ns_total = NULL ;
2435
2440
}
2441
+
2442
+ assert (!PyErr_Occurred ());
2443
+ res = 0 ;
2444
+
2436
2445
exit :
2437
2446
Py_XDECREF (s );
2438
2447
Py_XDECREF (ns_fractional );
2439
2448
Py_XDECREF (s_in_ns );
2440
2449
Py_XDECREF (ns_total );
2441
2450
Py_XDECREF (float_s );
2451
+ return res ;
2442
2452
}
2443
2453
2444
2454
#ifdef MS_WINDOWS
@@ -2473,34 +2483,47 @@ _pystat_l128_from_l64_l64(uint64_t low, uint64_t high)
2473
2483
static PyObject *
2474
2484
_pystat_fromstructstat (PyObject * module , STRUCT_STAT * st )
2475
2485
{
2476
- unsigned long ansec , mnsec , cnsec ;
2486
+ assert (!PyErr_Occurred ());
2487
+
2477
2488
PyObject * StatResultType = get_posix_state (module )-> StatResultType ;
2478
2489
PyObject * v = PyStructSequence_New ((PyTypeObject * )StatResultType );
2479
- if (v == NULL )
2490
+ if (v == NULL ) {
2480
2491
return NULL ;
2492
+ }
2493
+
2494
+ #define SET_ITEM (pos , expr ) \
2495
+ do { \
2496
+ PyObject *obj = (expr); \
2497
+ if (obj == NULL) { \
2498
+ goto error; \
2499
+ } \
2500
+ PyStructSequence_SET_ITEM(v, (pos), obj); \
2501
+ } while (0)
2481
2502
2482
- PyStructSequence_SET_ITEM ( v , 0 , PyLong_FromLong ((long )st -> st_mode ));
2503
+ SET_ITEM ( 0 , PyLong_FromLong ((long )st -> st_mode ));
2483
2504
#ifdef MS_WINDOWS
2484
- PyStructSequence_SET_ITEM ( v , 1 , _pystat_l128_from_l64_l64 (st -> st_ino , st -> st_ino_high ));
2485
- PyStructSequence_SET_ITEM ( v , 2 , PyLong_FromUnsignedLongLong (st -> st_dev ));
2505
+ SET_ITEM ( 1 , _pystat_l128_from_l64_l64 (st -> st_ino , st -> st_ino_high ));
2506
+ SET_ITEM ( 2 , PyLong_FromUnsignedLongLong (st -> st_dev ));
2486
2507
#else
2487
2508
static_assert (sizeof (unsigned long long ) >= sizeof (st -> st_ino ),
2488
2509
"stat.st_ino is larger than unsigned long long" );
2489
- PyStructSequence_SET_ITEM ( v , 1 , PyLong_FromUnsignedLongLong (st -> st_ino ));
2490
- PyStructSequence_SET_ITEM ( v , 2 , _PyLong_FromDev (st -> st_dev ));
2510
+ SET_ITEM ( 1 , PyLong_FromUnsignedLongLong (st -> st_ino ));
2511
+ SET_ITEM ( 2 , _PyLong_FromDev (st -> st_dev ));
2491
2512
#endif
2492
- PyStructSequence_SET_ITEM ( v , 3 , PyLong_FromLong ((long )st -> st_nlink ));
2513
+ SET_ITEM ( 3 , PyLong_FromLong ((long )st -> st_nlink ));
2493
2514
#if defined(MS_WINDOWS )
2494
- PyStructSequence_SET_ITEM ( v , 4 , PyLong_FromLong (0 ));
2495
- PyStructSequence_SET_ITEM ( v , 5 , PyLong_FromLong (0 ));
2515
+ SET_ITEM ( 4 , PyLong_FromLong (0 ));
2516
+ SET_ITEM ( 5 , PyLong_FromLong (0 ));
2496
2517
#else
2497
- PyStructSequence_SET_ITEM ( v , 4 , _PyLong_FromUid (st -> st_uid ));
2498
- PyStructSequence_SET_ITEM ( v , 5 , _PyLong_FromGid (st -> st_gid ));
2518
+ SET_ITEM ( 4 , _PyLong_FromUid (st -> st_uid ));
2519
+ SET_ITEM ( 5 , _PyLong_FromGid (st -> st_gid ));
2499
2520
#endif
2500
2521
static_assert (sizeof (long long ) >= sizeof (st -> st_size ),
2501
2522
"stat.st_size is larger than long long" );
2502
- PyStructSequence_SET_ITEM ( v , 6 , PyLong_FromLongLong (st -> st_size ));
2523
+ SET_ITEM ( 6 , PyLong_FromLongLong (st -> st_size ));
2503
2524
2525
+ // Set st_atime, st_mtime and st_ctime
2526
+ unsigned long ansec , mnsec , cnsec ;
2504
2527
#if defined(HAVE_STAT_TV_NSEC )
2505
2528
ansec = st -> st_atim .tv_nsec ;
2506
2529
mnsec = st -> st_mtim .tv_nsec ;
@@ -2516,67 +2539,67 @@ _pystat_fromstructstat(PyObject *module, STRUCT_STAT *st)
2516
2539
#else
2517
2540
ansec = mnsec = cnsec = 0 ;
2518
2541
#endif
2519
- fill_time (module , v , 7 , 10 , 13 , st -> st_atime , ansec );
2520
- fill_time (module , v , 8 , 11 , 14 , st -> st_mtime , mnsec );
2521
- fill_time (module , v , 9 , 12 , 15 , st -> st_ctime , cnsec );
2542
+ if (fill_time (module , v , 7 , 10 , 13 , st -> st_atime , ansec ) < 0 ) {
2543
+ goto error ;
2544
+ }
2545
+ if (fill_time (module , v , 8 , 11 , 14 , st -> st_mtime , mnsec ) < 0 ) {
2546
+ goto error ;
2547
+ }
2548
+ if (fill_time (module , v , 9 , 12 , 15 , st -> st_ctime , cnsec ) < 0 ) {
2549
+ goto error ;
2550
+ }
2522
2551
2523
2552
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2524
- PyStructSequence_SET_ITEM (v , ST_BLKSIZE_IDX ,
2525
- PyLong_FromLong ((long )st -> st_blksize ));
2553
+ SET_ITEM (ST_BLKSIZE_IDX , PyLong_FromLong ((long )st -> st_blksize ));
2526
2554
#endif
2527
2555
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2528
- PyStructSequence_SET_ITEM (v , ST_BLOCKS_IDX ,
2529
- PyLong_FromLong ((long )st -> st_blocks ));
2556
+ SET_ITEM (ST_BLOCKS_IDX , PyLong_FromLong ((long )st -> st_blocks ));
2530
2557
#endif
2531
2558
#ifdef HAVE_STRUCT_STAT_ST_RDEV
2532
- PyStructSequence_SET_ITEM (v , ST_RDEV_IDX ,
2533
- PyLong_FromLong ((long )st -> st_rdev ));
2559
+ SET_ITEM (ST_RDEV_IDX , PyLong_FromLong ((long )st -> st_rdev ));
2534
2560
#endif
2535
2561
#ifdef HAVE_STRUCT_STAT_ST_GEN
2536
- PyStructSequence_SET_ITEM (v , ST_GEN_IDX ,
2537
- PyLong_FromLong ((long )st -> st_gen ));
2562
+ SET_ITEM (ST_GEN_IDX , PyLong_FromLong ((long )st -> st_gen ));
2538
2563
#endif
2539
2564
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME )
2540
2565
{
2541
- PyObject * val ;
2542
- unsigned long bsec ,bnsec ;
2566
+ unsigned long bsec , bnsec ;
2543
2567
bsec = (long )st -> st_birthtime ;
2544
2568
#ifdef HAVE_STAT_TV_NSEC2
2545
2569
bnsec = st -> st_birthtimespec .tv_nsec ;
2546
2570
#else
2547
2571
bnsec = 0 ;
2548
2572
#endif
2549
- val = PyFloat_FromDouble (bsec + 1e-9 * bnsec );
2550
- PyStructSequence_SET_ITEM (v , ST_BIRTHTIME_IDX ,
2551
- val );
2573
+ SET_ITEM (ST_BIRTHTIME_IDX , PyFloat_FromDouble (bsec + bnsec * 1e-9 ));
2552
2574
}
2553
2575
#elif defined(MS_WINDOWS )
2554
- fill_time (module , v , -1 , ST_BIRTHTIME_IDX , ST_BIRTHTIME_NS_IDX ,
2555
- st -> st_birthtime , st -> st_birthtime_nsec );
2576
+ if (fill_time (module , v , -1 , ST_BIRTHTIME_IDX , ST_BIRTHTIME_NS_IDX ,
2577
+ st -> st_birthtime , st -> st_birthtime_nsec ) < 0 ) {
2578
+ goto error ;
2579
+ }
2556
2580
#endif
2557
2581
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
2558
- PyStructSequence_SET_ITEM (v , ST_FLAGS_IDX ,
2559
- PyLong_FromLong ((long )st -> st_flags ));
2582
+ SET_ITEM (ST_FLAGS_IDX , PyLong_FromLong ((long )st -> st_flags ));
2560
2583
#endif
2561
2584
#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2562
- PyStructSequence_SET_ITEM ( v , ST_FILE_ATTRIBUTES_IDX ,
2563
- PyLong_FromUnsignedLong (st -> st_file_attributes ));
2585
+ SET_ITEM ( ST_FILE_ATTRIBUTES_IDX ,
2586
+ PyLong_FromUnsignedLong (st -> st_file_attributes ));
2564
2587
#endif
2565
2588
#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2566
- PyStructSequence_SET_ITEM (v , ST_FSTYPE_IDX ,
2567
- PyUnicode_FromString (st -> st_fstype ));
2589
+ SET_ITEM (ST_FSTYPE_IDX , PyUnicode_FromString (st -> st_fstype ));
2568
2590
#endif
2569
2591
#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2570
- PyStructSequence_SET_ITEM (v , ST_REPARSE_TAG_IDX ,
2571
- PyLong_FromUnsignedLong (st -> st_reparse_tag ));
2592
+ SET_ITEM (ST_REPARSE_TAG_IDX , PyLong_FromUnsignedLong (st -> st_reparse_tag ));
2572
2593
#endif
2573
2594
2574
- if (PyErr_Occurred ()) {
2575
- Py_DECREF (v );
2576
- return NULL ;
2577
- }
2578
-
2595
+ assert (!PyErr_Occurred ());
2579
2596
return v ;
2597
+
2598
+ error :
2599
+ Py_DECREF (v );
2600
+ return NULL ;
2601
+
2602
+ #undef SET_ITEM
2580
2603
}
2581
2604
2582
2605
/* POSIX methods */
0 commit comments