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