Skip to content

Commit 5c33ee5

Browse files
authored
Merge branch 'main' into fix_buildbot_for_https_test
2 parents 6c0d651 + d862799 commit 5c33ee5

File tree

5 files changed

+56
-25
lines changed

5 files changed

+56
-25
lines changed

Include/internal/pycore_import.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ struct _import_runtime_state {
1919
used exclusively for when the extensions dict is access/modified
2020
from an arbitrary thread. */
2121
PyThreadState main_tstate;
22+
/* A lock to guard the dict. */
23+
PyThread_type_lock mutex;
2224
/* A dict mapping (filename, name) to PyModuleDef for modules.
2325
Only legacy (single-phase init) extension modules are added
2426
and only if they support multiple initialization (m_size >- 0)

Lib/calendar.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import sys
99
import datetime
10+
from enum import IntEnum, global_enum
1011
import locale as _locale
1112
from itertools import repeat
1213

@@ -16,6 +17,9 @@
1617
"timegm", "month_name", "month_abbr", "day_name", "day_abbr",
1718
"Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
1819
"LocaleHTMLCalendar", "weekheader",
20+
"Day", "Month", "JANUARY", "FEBRUARY", "MARCH",
21+
"APRIL", "MAY", "JUNE", "JULY",
22+
"AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER",
1923
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
2024
"SATURDAY", "SUNDAY"]
2125

@@ -37,9 +41,35 @@ def __str__(self):
3741
return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self.weekday
3842

3943

40-
# Constants for months referenced later
41-
January = 1
42-
February = 2
44+
# Constants for months
45+
@global_enum
46+
class Month(IntEnum):
47+
JANUARY = 1
48+
FEBRUARY = 2
49+
MARCH = 3
50+
APRIL = 4
51+
MAY = 5
52+
JUNE = 6
53+
JULY = 7
54+
AUGUST = 8
55+
SEPTEMBER = 9
56+
OCTOBER = 10
57+
NOVEMBER = 11
58+
DECEMBER = 12
59+
60+
61+
# Constants for days
62+
@global_enum
63+
class Day(IntEnum):
64+
MONDAY = 0
65+
TUESDAY = 1
66+
WEDNESDAY = 2
67+
THURSDAY = 3
68+
FRIDAY = 4
69+
SATURDAY = 5
70+
SUNDAY = 6
71+
72+
4373

4474
# Number of days per month (except for February in leap years)
4575
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
@@ -95,9 +125,6 @@ def __len__(self):
95125
month_name = _localized_month('%B')
96126
month_abbr = _localized_month('%b')
97127

98-
# Constants for weekdays
99-
(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
100-
101128

102129
def isleap(year):
103130
"""Return True for leap years, False for non-leap years."""
@@ -125,12 +152,12 @@ def monthrange(year, month):
125152
if not 1 <= month <= 12:
126153
raise IllegalMonthError(month)
127154
day1 = weekday(year, month, 1)
128-
ndays = mdays[month] + (month == February and isleap(year))
155+
ndays = mdays[month] + (month == FEBRUARY and isleap(year))
129156
return day1, ndays
130157

131158

132159
def _monthlen(year, month):
133-
return mdays[month] + (month == February and isleap(year))
160+
return mdays[month] + (month == FEBRUARY and isleap(year))
134161

135162

136163
def _prevmonth(year, month):
@@ -260,10 +287,7 @@ def yeardatescalendar(self, year, width=3):
260287
Each month contains between 4 and 6 weeks and each week contains 1-7
261288
days. Days are datetime.date objects.
262289
"""
263-
months = [
264-
self.monthdatescalendar(year, i)
265-
for i in range(January, January+12)
266-
]
290+
months = [self.monthdatescalendar(year, m) for m in Month]
267291
return [months[i:i+width] for i in range(0, len(months), width) ]
268292

269293
def yeardays2calendar(self, year, width=3):
@@ -273,10 +297,7 @@ def yeardays2calendar(self, year, width=3):
273297
(day number, weekday number) tuples. Day numbers outside this month are
274298
zero.
275299
"""
276-
months = [
277-
self.monthdays2calendar(year, i)
278-
for i in range(January, January+12)
279-
]
300+
months = [self.monthdays2calendar(year, m) for m in Month]
280301
return [months[i:i+width] for i in range(0, len(months), width) ]
281302

282303
def yeardayscalendar(self, year, width=3):
@@ -285,10 +306,7 @@ def yeardayscalendar(self, year, width=3):
285306
yeardatescalendar()). Entries in the week lists are day numbers.
286307
Day numbers outside this month are zero.
287308
"""
288-
months = [
289-
self.monthdayscalendar(year, i)
290-
for i in range(January, January+12)
291-
]
309+
months = [self.monthdayscalendar(year, m) for m in Month]
292310
return [months[i:i+width] for i in range(0, len(months), width) ]
293311

294312

@@ -509,7 +527,7 @@ def formatyear(self, theyear, width=3):
509527
a('\n')
510528
a('<tr><th colspan="%d" class="%s">%s</th></tr>' % (
511529
width, self.cssclass_year_head, theyear))
512-
for i in range(January, January+12, width):
530+
for i in range(JANUARY, JANUARY+12, width):
513531
# months in this row
514532
months = range(i, min(i+width, 13))
515533
a('<tr>')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added Enum for months and days in the calendar module.

Python/import.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,11 @@ remove_module(PyThreadState *tstate, PyObject *name)
413413
Py_ssize_t
414414
_PyImport_GetNextModuleIndex(void)
415415
{
416+
PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
416417
LAST_MODULE_INDEX++;
417-
return LAST_MODULE_INDEX;
418+
Py_ssize_t index = LAST_MODULE_INDEX;
419+
PyThread_release_lock(EXTENSIONS.mutex);
420+
return index;
418421
}
419422

420423
static const char *
@@ -703,21 +706,25 @@ _PyImport_ClearModulesByIndex(PyInterpreterState *interp)
703706
const char *
704707
_PyImport_ResolveNameWithPackageContext(const char *name)
705708
{
709+
PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
706710
if (PKGCONTEXT != NULL) {
707711
const char *p = strrchr(PKGCONTEXT, '.');
708712
if (p != NULL && strcmp(name, p+1) == 0) {
709713
name = PKGCONTEXT;
710714
PKGCONTEXT = NULL;
711715
}
712716
}
717+
PyThread_release_lock(EXTENSIONS.mutex);
713718
return name;
714719
}
715720

716721
const char *
717722
_PyImport_SwapPackageContext(const char *newcontext)
718723
{
724+
PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
719725
const char *oldcontext = PKGCONTEXT;
720726
PKGCONTEXT = newcontext;
727+
PyThread_release_lock(EXTENSIONS.mutex);
721728
return oldcontext;
722729
}
723730

@@ -865,13 +872,13 @@ gets even messier.
865872
static inline void
866873
extensions_lock_acquire(void)
867874
{
868-
// XXX For now the GIL is sufficient.
875+
PyThread_acquire_lock(_PyRuntime.imports.extensions.mutex, WAIT_LOCK);
869876
}
870877

871878
static inline void
872879
extensions_lock_release(void)
873880
{
874-
// XXX For now the GIL is sufficient.
881+
PyThread_release_lock(_PyRuntime.imports.extensions.mutex);
875882
}
876883

877884
/* Magic for extension modules (built-in as well as dynamically

Python/pystate.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
380380
static const _PyRuntimeState initial = _PyRuntimeState_INIT(_PyRuntime);
381381
_Py_COMP_DIAG_POP
382382

383-
#define NUMLOCKS 4
383+
#define NUMLOCKS 5
384384

385385
static int
386386
alloc_for_runtime(PyThread_type_lock locks[NUMLOCKS])
@@ -434,6 +434,7 @@ init_runtime(_PyRuntimeState *runtime,
434434
&runtime->xidregistry.mutex,
435435
&runtime->getargs.mutex,
436436
&runtime->unicode_state.ids.lock,
437+
&runtime->imports.extensions.mutex,
437438
};
438439
for (int i = 0; i < NUMLOCKS; i++) {
439440
assert(locks[i] != NULL);
@@ -518,6 +519,7 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
518519
&runtime->xidregistry.mutex,
519520
&runtime->getargs.mutex,
520521
&runtime->unicode_state.ids.lock,
522+
&runtime->imports.extensions.mutex,
521523
};
522524
for (int i = 0; i < NUMLOCKS; i++) {
523525
FREE_LOCK(*lockptrs[i]);
@@ -546,6 +548,7 @@ _PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
546548
&runtime->xidregistry.mutex,
547549
&runtime->getargs.mutex,
548550
&runtime->unicode_state.ids.lock,
551+
&runtime->imports.extensions.mutex,
549552
};
550553
int reinit_err = 0;
551554
for (int i = 0; i < NUMLOCKS; i++) {

0 commit comments

Comments
 (0)