Skip to content

Commit 581e8c5

Browse files
committed
Merge branch 'main' into pythongh-104102-optimize-glob-parent
2 parents f268157 + 47770a1 commit 581e8c5

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

Doc/library/copyreg.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Such constructors may be factory functions or class instances.
2929

3030
Declares that *function* should be used as a "reduction" function for objects
3131
of type *type*. *function* must return either a string or a tuple
32-
containing two or five elements. See the :attr:`~pickle.Pickler.dispatch_table`
32+
containing between two and six elements. See the :attr:`~pickle.Pickler.dispatch_table`
3333
for more details on the interface of *function*.
3434

3535
The *constructor_ob* parameter is a legacy feature and is now ignored, but if

Lib/calendar.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class Day(IntEnum):
8383
SUNDAY = 6
8484

8585

86-
8786
# Number of days per month (except for February in leap years)
8887
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
8988

@@ -156,7 +155,7 @@ def weekday(year, month, day):
156155
"""Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31)."""
157156
if not datetime.MINYEAR <= year <= datetime.MAXYEAR:
158157
year = 2000 + year % 400
159-
return datetime.date(year, month, day).weekday()
158+
return Day(datetime.date(year, month, day).weekday())
160159

161160

162161
def monthrange(year, month):

Lib/pathlib.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def _is_wildcard_pattern(pat):
5959
# be looked up directly as a file.
6060
return "*" in pat or "?" in pat or "[" in pat
6161

62+
def _is_case_sensitive(flavour):
63+
return flavour.normcase('Aa') == 'Aa'
64+
6265
#
6366
# Globbing helpers
6467
#
@@ -102,25 +105,24 @@ def select_from(self, parent_path):
102105
is_dir = path_cls.is_dir
103106
exists = path_cls.exists
104107
scandir = path_cls._scandir
105-
normcase = path_cls._flavour.normcase
106108
if not is_dir(parent_path):
107109
return iter([])
108-
return self._select_from(parent_path, is_dir, exists, scandir, normcase)
110+
return self._select_from(parent_path, is_dir, exists, scandir)
109111

110112

111113
class _TerminatingSelector:
112114

113-
def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
115+
def _select_from(self, parent_path, is_dir, exists, scandir):
114116
yield parent_path
115117

116118

117119
class _ParentSelector(_Selector):
118120
def __init__(self, name, child_parts, case_sensitive):
119121
_Selector.__init__(self, child_parts, case_sensitive)
120122

121-
def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
123+
def _select_from(self, parent_path, is_dir, exists, scandir):
122124
path = parent_path._make_child_relpath('..')
123-
for p in self.successor._select_from(path, is_dir, exists, scandir, normcase):
125+
for p in self.successor._select_from(path, is_dir, exists, scandir):
124126
yield p
125127

126128

@@ -130,11 +132,11 @@ def __init__(self, name, child_parts, flavour):
130132
self.name = name
131133
_Selector.__init__(self, child_parts, flavour)
132134

133-
def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
135+
def _select_from(self, parent_path, is_dir, exists, scandir):
134136
try:
135137
path = parent_path._make_child_relpath(self.name)
136138
if (is_dir if self.dironly else exists)(path):
137-
for p in self.successor._select_from(path, is_dir, exists, scandir, normcase):
139+
for p in self.successor._select_from(path, is_dir, exists, scandir):
138140
yield p
139141
except PermissionError:
140142
return
@@ -143,10 +145,11 @@ def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
143145
class _WildcardSelector(_Selector):
144146

145147
def __init__(self, pat, child_parts, flavour):
146-
self.match = re.compile(fnmatch.translate(flavour.normcase(pat))).fullmatch
148+
flags = re.NOFLAG if _is_case_sensitive(flavour) else re.IGNORECASE
149+
self.match = re.compile(fnmatch.translate(pat), flags=flags).fullmatch
147150
_Selector.__init__(self, child_parts, flavour)
148151

149-
def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
152+
def _select_from(self, parent_path, is_dir, exists, scandir):
150153
try:
151154
# We must close the scandir() object before proceeding to
152155
# avoid exhausting file descriptors when globbing deep trees.
@@ -165,9 +168,9 @@ def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
165168
raise
166169
continue
167170
name = entry.name
168-
if self.match(normcase(name)):
171+
if self.match(name):
169172
path = parent_path._make_child_relpath(name)
170-
for p in self.successor._select_from(path, is_dir, exists, scandir, normcase):
173+
for p in self.successor._select_from(path, is_dir, exists, scandir):
171174
yield p
172175
except PermissionError:
173176
return
@@ -199,13 +202,13 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
199202
except PermissionError:
200203
return
201204

202-
def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
205+
def _select_from(self, parent_path, is_dir, exists, scandir):
203206
try:
204207
yielded = set()
205208
try:
206209
successor_select = self.successor._select_from
207210
for starting_point in self._iterate_directories(parent_path, is_dir, scandir):
208-
for p in successor_select(starting_point, is_dir, exists, scandir, normcase):
211+
for p in successor_select(starting_point, is_dir, exists, scandir):
209212
if p not in yielded:
210213
yield p
211214
yielded.add(p)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update the return type of ``weekday`` to the newly added Day attribute
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve performance of :meth:`pathlib.Path.glob` by using
2+
:data:`re.IGNORECASE` to implement case-insensitive matching.

0 commit comments

Comments
 (0)