@@ -2363,21 +2363,26 @@ _posix_free(void *module)
2363
2363
_posix_clear ((PyObject * )module );
2364
2364
}
2365
2365
2366
- static void
2366
+ static int
2367
2367
fill_time (PyObject * module , PyObject * v , int index , time_t sec , unsigned long nsec )
2368
2368
{
2369
- PyObject * s = _PyLong_FromTime_t (sec );
2370
- PyObject * ns_fractional = PyLong_FromUnsignedLong (nsec );
2369
+ assert (!PyErr_Occurred ());
2370
+
2371
+ int res = -1 ;
2371
2372
PyObject * s_in_ns = NULL ;
2372
2373
PyObject * ns_total = NULL ;
2373
2374
PyObject * float_s = NULL ;
2374
2375
2375
- if (!(s && ns_fractional ))
2376
+ PyObject * s = _PyLong_FromTime_t (sec );
2377
+ PyObject * ns_fractional = PyLong_FromUnsignedLong (nsec );
2378
+ if (!(s && ns_fractional )) {
2376
2379
goto exit ;
2380
+ }
2377
2381
2378
2382
s_in_ns = PyNumber_Multiply (s , get_posix_state (module )-> billion );
2379
- if (!s_in_ns )
2383
+ if (!s_in_ns ) {
2380
2384
goto exit ;
2385
+ }
2381
2386
2382
2387
ns_total = PyNumber_Add (s_in_ns , ns_fractional );
2383
2388
if (!ns_total )
@@ -2394,46 +2399,64 @@ fill_time(PyObject *module, PyObject *v, int index, time_t sec, unsigned long ns
2394
2399
s = NULL ;
2395
2400
float_s = NULL ;
2396
2401
ns_total = NULL ;
2402
+
2403
+ assert (!PyErr_Occurred ());
2404
+ res = 0 ;
2405
+
2397
2406
exit :
2398
2407
Py_XDECREF (s );
2399
2408
Py_XDECREF (ns_fractional );
2400
2409
Py_XDECREF (s_in_ns );
2401
2410
Py_XDECREF (ns_total );
2402
2411
Py_XDECREF (float_s );
2412
+ return res ;
2403
2413
}
2404
2414
2405
2415
/* pack a system stat C structure into the Python stat tuple
2406
2416
(used by posix_stat() and posix_fstat()) */
2407
2417
static PyObject *
2408
2418
_pystat_fromstructstat (PyObject * module , STRUCT_STAT * st )
2409
2419
{
2410
- unsigned long ansec , mnsec , cnsec ;
2420
+ assert (!PyErr_Occurred ());
2421
+
2411
2422
PyObject * StatResultType = get_posix_state (module )-> StatResultType ;
2412
2423
PyObject * v = PyStructSequence_New ((PyTypeObject * )StatResultType );
2413
- if (v == NULL )
2424
+ if (v == NULL ) {
2414
2425
return NULL ;
2426
+ }
2415
2427
2416
- PyStructSequence_SET_ITEM (v , 0 , PyLong_FromLong ((long )st -> st_mode ));
2428
+ #define SET_ITEM (pos , expr ) \
2429
+ do { \
2430
+ PyObject *obj = (expr); \
2431
+ if (obj == NULL) { \
2432
+ goto error; \
2433
+ } \
2434
+ PyStructSequence_SET_ITEM(v, (pos), obj); \
2435
+ } while (0)
2436
+
2437
+ SET_ITEM (0 , PyLong_FromLong ((long )st -> st_mode ));
2417
2438
static_assert (sizeof (unsigned long long ) >= sizeof (st -> st_ino ),
2418
2439
"stat.st_ino is larger than unsigned long long" );
2419
- PyStructSequence_SET_ITEM ( v , 1 , PyLong_FromUnsignedLongLong (st -> st_ino ));
2440
+ SET_ITEM ( 1 , PyLong_FromUnsignedLongLong (st -> st_ino ));
2420
2441
#ifdef MS_WINDOWS
2421
- PyStructSequence_SET_ITEM ( v , 2 , PyLong_FromUnsignedLong (st -> st_dev ));
2442
+ SET_ITEM ( 2 , PyLong_FromUnsignedLong (st -> st_dev ));
2422
2443
#else
2423
- PyStructSequence_SET_ITEM ( v , 2 , _PyLong_FromDev (st -> st_dev ));
2444
+ SET_ITEM ( 2 , _PyLong_FromDev (st -> st_dev ));
2424
2445
#endif
2425
- PyStructSequence_SET_ITEM ( v , 3 , PyLong_FromLong ((long )st -> st_nlink ));
2446
+ SET_ITEM ( 3 , PyLong_FromLong ((long )st -> st_nlink ));
2426
2447
#if defined(MS_WINDOWS )
2427
- PyStructSequence_SET_ITEM ( v , 4 , PyLong_FromLong (0 ));
2428
- PyStructSequence_SET_ITEM ( v , 5 , PyLong_FromLong (0 ));
2448
+ SET_ITEM ( 4 , PyLong_FromLong (0 ));
2449
+ SET_ITEM ( 5 , PyLong_FromLong (0 ));
2429
2450
#else
2430
- PyStructSequence_SET_ITEM ( v , 4 , _PyLong_FromUid (st -> st_uid ));
2431
- PyStructSequence_SET_ITEM ( v , 5 , _PyLong_FromGid (st -> st_gid ));
2451
+ SET_ITEM ( 4 , _PyLong_FromUid (st -> st_uid ));
2452
+ SET_ITEM ( 5 , _PyLong_FromGid (st -> st_gid ));
2432
2453
#endif
2433
2454
static_assert (sizeof (long long ) >= sizeof (st -> st_size ),
2434
2455
"stat.st_size is larger than long long" );
2435
- PyStructSequence_SET_ITEM ( v , 6 , PyLong_FromLongLong (st -> st_size ));
2456
+ SET_ITEM ( 6 , PyLong_FromLongLong (st -> st_size ));
2436
2457
2458
+ // Set st_atime, st_mtime and st_ctime
2459
+ unsigned long ansec , mnsec , cnsec ;
2437
2460
#if defined(HAVE_STAT_TV_NSEC )
2438
2461
ansec = st -> st_atim .tv_nsec ;
2439
2462
mnsec = st -> st_mtim .tv_nsec ;
@@ -2449,64 +2472,62 @@ _pystat_fromstructstat(PyObject *module, STRUCT_STAT *st)
2449
2472
#else
2450
2473
ansec = mnsec = cnsec = 0 ;
2451
2474
#endif
2452
- fill_time (module , v , 7 , st -> st_atime , ansec );
2453
- fill_time (module , v , 8 , st -> st_mtime , mnsec );
2454
- fill_time (module , v , 9 , st -> st_ctime , cnsec );
2475
+ if (fill_time (module , v , 7 , st -> st_atime , ansec ) < 0 ) {
2476
+ goto error ;
2477
+ }
2478
+ if (fill_time (module , v , 8 , st -> st_mtime , mnsec ) < 0 ) {
2479
+ goto error ;
2480
+ }
2481
+ if (fill_time (module , v , 9 , st -> st_ctime , cnsec ) < 0 ) {
2482
+ goto error ;
2483
+ }
2455
2484
2456
2485
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2457
- PyStructSequence_SET_ITEM (v , ST_BLKSIZE_IDX ,
2458
- PyLong_FromLong ((long )st -> st_blksize ));
2486
+ SET_ITEM (ST_BLKSIZE_IDX , PyLong_FromLong ((long )st -> st_blksize ));
2459
2487
#endif
2460
2488
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2461
- PyStructSequence_SET_ITEM (v , ST_BLOCKS_IDX ,
2462
- PyLong_FromLong ((long )st -> st_blocks ));
2489
+ SET_ITEM (ST_BLOCKS_IDX , PyLong_FromLong ((long )st -> st_blocks ));
2463
2490
#endif
2464
2491
#ifdef HAVE_STRUCT_STAT_ST_RDEV
2465
- PyStructSequence_SET_ITEM (v , ST_RDEV_IDX ,
2466
- PyLong_FromLong ((long )st -> st_rdev ));
2492
+ SET_ITEM (ST_RDEV_IDX , PyLong_FromLong ((long )st -> st_rdev ));
2467
2493
#endif
2468
2494
#ifdef HAVE_STRUCT_STAT_ST_GEN
2469
- PyStructSequence_SET_ITEM (v , ST_GEN_IDX ,
2470
- PyLong_FromLong ((long )st -> st_gen ));
2495
+ SET_ITEM (ST_GEN_IDX , PyLong_FromLong ((long )st -> st_gen ));
2471
2496
#endif
2472
2497
#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2473
2498
{
2474
- PyObject * val ;
2475
- unsigned long bsec ,bnsec ;
2499
+ unsigned long bsec , bnsec ;
2476
2500
bsec = (long )st -> st_birthtime ;
2477
2501
#ifdef HAVE_STAT_TV_NSEC2
2478
2502
bnsec = st -> st_birthtimespec .tv_nsec ;
2479
2503
#else
2480
2504
bnsec = 0 ;
2481
2505
#endif
2482
- val = PyFloat_FromDouble (bsec + 1e-9 * bnsec );
2483
- PyStructSequence_SET_ITEM (v , ST_BIRTHTIME_IDX ,
2484
- val );
2506
+ SET_ITEM (ST_BIRTHTIME_IDX , PyFloat_FromDouble (bsec + bnsec * 1e-9 ));
2485
2507
}
2486
2508
#endif
2487
2509
#ifdef HAVE_STRUCT_STAT_ST_FLAGS
2488
- PyStructSequence_SET_ITEM (v , ST_FLAGS_IDX ,
2489
- PyLong_FromLong ((long )st -> st_flags ));
2510
+ SET_ITEM (ST_FLAGS_IDX , PyLong_FromLong ((long )st -> st_flags ));
2490
2511
#endif
2491
2512
#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2492
- PyStructSequence_SET_ITEM ( v , ST_FILE_ATTRIBUTES_IDX ,
2493
- PyLong_FromUnsignedLong (st -> st_file_attributes ));
2513
+ SET_ITEM ( ST_FILE_ATTRIBUTES_IDX ,
2514
+ PyLong_FromUnsignedLong (st -> st_file_attributes ));
2494
2515
#endif
2495
2516
#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2496
- PyStructSequence_SET_ITEM (v , ST_FSTYPE_IDX ,
2497
- PyUnicode_FromString (st -> st_fstype ));
2517
+ SET_ITEM (ST_FSTYPE_IDX , PyUnicode_FromString (st -> st_fstype ));
2498
2518
#endif
2499
2519
#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2500
- PyStructSequence_SET_ITEM (v , ST_REPARSE_TAG_IDX ,
2501
- PyLong_FromUnsignedLong (st -> st_reparse_tag ));
2520
+ SET_ITEM (ST_REPARSE_TAG_IDX , PyLong_FromUnsignedLong (st -> st_reparse_tag ));
2502
2521
#endif
2503
2522
2504
- if (PyErr_Occurred ()) {
2505
- Py_DECREF (v );
2506
- return NULL ;
2507
- }
2508
-
2523
+ assert (!PyErr_Occurred ());
2509
2524
return v ;
2525
+
2526
+ error :
2527
+ Py_DECREF (v );
2528
+ return NULL ;
2529
+
2530
+ #undef SET_ITEM
2510
2531
}
2511
2532
2512
2533
/* POSIX methods */
0 commit comments