3
3
#endif
4
4
5
5
#include "Python.h"
6
+ #include "pycore_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED()
6
7
#include "pycore_long.h" // _PyLong_GetOne()
7
8
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
8
9
@@ -298,15 +299,20 @@ get_weak_cache(zoneinfo_state *state, PyTypeObject *type)
298
299
}
299
300
}
300
301
302
+ /*[clinic input]
303
+ @critical_section
304
+ @classmethod
305
+ zoneinfo.ZoneInfo.__new__
306
+
307
+ key: object
308
+
309
+ Create a new ZoneInfo instance.
310
+ [clinic start generated code]*/
311
+
301
312
static PyObject *
302
- zoneinfo_new (PyTypeObject * type , PyObject * args , PyObject * kw )
313
+ zoneinfo_ZoneInfo_impl (PyTypeObject * type , PyObject * key )
314
+ /*[clinic end generated code: output=95e61dab86bb95c3 input=ef73d7a83bf8790e]*/
303
315
{
304
- PyObject * key = NULL ;
305
- static char * kwlist [] = {"key" , NULL };
306
- if (PyArg_ParseTupleAndKeywords (args , kw , "O" , kwlist , & key ) == 0 ) {
307
- return NULL ;
308
- }
309
-
310
316
zoneinfo_state * state = zoneinfo_get_state_by_self (type );
311
317
PyObject * instance = zone_from_strong_cache (state , type , key );
312
318
if (instance != NULL || PyErr_Occurred ()) {
@@ -467,6 +473,7 @@ zoneinfo_ZoneInfo_no_cache_impl(PyTypeObject *type, PyTypeObject *cls,
467
473
}
468
474
469
475
/*[clinic input]
476
+ @critical_section
470
477
@classmethod
471
478
zoneinfo.ZoneInfo.clear_cache
472
479
@@ -481,7 +488,7 @@ Clear the ZoneInfo cache.
481
488
static PyObject *
482
489
zoneinfo_ZoneInfo_clear_cache_impl (PyTypeObject * type , PyTypeObject * cls ,
483
490
PyObject * only_keys )
484
- /*[clinic end generated code: output=114d9b7c8a22e660 input=e32ca3bb396788ba ]*/
491
+ /*[clinic end generated code: output=114d9b7c8a22e660 input=35944715df26d24e ]*/
485
492
{
486
493
zoneinfo_state * state = zoneinfo_get_state_by_cls (cls );
487
494
PyObject * weak_cache = get_weak_cache (state , type );
@@ -816,14 +823,10 @@ zoneinfo_ZoneInfo__unpickle_impl(PyTypeObject *type, PyTypeObject *cls,
816
823
/*[clinic end generated code: output=556712fc709deecb input=6ac8c73eed3de316]*/
817
824
{
818
825
if (from_cache ) {
819
- PyObject * val_args = PyTuple_Pack (1 , key );
820
- if (val_args == NULL ) {
821
- return NULL ;
822
- }
823
-
824
- PyObject * rv = zoneinfo_new (type , val_args , NULL );
825
-
826
- Py_DECREF (val_args );
826
+ PyObject * rv ;
827
+ Py_BEGIN_CRITICAL_SECTION (type );
828
+ rv = zoneinfo_ZoneInfo_impl (type , key );
829
+ Py_END_CRITICAL_SECTION ();
827
830
return rv ;
828
831
}
829
832
else {
@@ -858,8 +861,7 @@ load_timedelta(zoneinfo_state *state, long seconds)
858
861
0 , seconds , 0 , 1 , PyDateTimeAPI -> DeltaType );
859
862
860
863
if (tmp != NULL ) {
861
- rv = PyDict_SetDefault (state -> TIMEDELTA_CACHE , pyoffset , tmp );
862
- Py_XINCREF (rv );
864
+ PyDict_SetDefaultRef (state -> TIMEDELTA_CACHE , pyoffset , tmp , & rv );
863
865
Py_DECREF (tmp );
864
866
}
865
867
}
@@ -2368,6 +2370,7 @@ strong_cache_free(StrongCacheNode *root)
2368
2370
static void
2369
2371
remove_from_strong_cache (zoneinfo_state * state , StrongCacheNode * node )
2370
2372
{
2373
+ _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (state -> ZoneInfoType );
2371
2374
if (state -> ZONEINFO_STRONG_CACHE == node ) {
2372
2375
state -> ZONEINFO_STRONG_CACHE = node -> next ;
2373
2376
}
@@ -2422,6 +2425,7 @@ eject_from_strong_cache(zoneinfo_state *state, const PyTypeObject *const type,
2422
2425
return 0 ;
2423
2426
}
2424
2427
2428
+ _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (state -> ZoneInfoType );
2425
2429
StrongCacheNode * cache = state -> ZONEINFO_STRONG_CACHE ;
2426
2430
StrongCacheNode * node = find_in_strong_cache (cache , key );
2427
2431
if (node != NULL ) {
@@ -2478,6 +2482,7 @@ zone_from_strong_cache(zoneinfo_state *state, const PyTypeObject *const type,
2478
2482
return NULL ; // Strong cache currently only implemented for base class
2479
2483
}
2480
2484
2485
+ _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (state -> ZoneInfoType );
2481
2486
StrongCacheNode * cache = state -> ZONEINFO_STRONG_CACHE ;
2482
2487
StrongCacheNode * node = find_in_strong_cache (cache , key );
2483
2488
@@ -2504,6 +2509,7 @@ update_strong_cache(zoneinfo_state *state, const PyTypeObject *const type,
2504
2509
return ;
2505
2510
}
2506
2511
2512
+ _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (state -> ZoneInfoType );
2507
2513
StrongCacheNode * new_node = strong_cache_node_new (key , zone );
2508
2514
if (new_node == NULL ) {
2509
2515
return ;
@@ -2631,7 +2637,7 @@ static PyType_Slot zoneinfo_slots[] = {
2631
2637
{Py_tp_getattro , PyObject_GenericGetAttr },
2632
2638
{Py_tp_methods , zoneinfo_methods },
2633
2639
{Py_tp_members , zoneinfo_members },
2634
- {Py_tp_new , zoneinfo_new },
2640
+ {Py_tp_new , zoneinfo_ZoneInfo },
2635
2641
{Py_tp_dealloc , zoneinfo_dealloc },
2636
2642
{Py_tp_traverse , zoneinfo_traverse },
2637
2643
{Py_tp_clear , zoneinfo_clear },
0 commit comments