Skip to content

Commit 27d16e4

Browse files
committed
Deploying to gh-pages from @ 2a5f362 🚀
1 parent 17f3a4f commit 27d16e4

File tree

613 files changed

+9832
-9019
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

613 files changed

+9832
-9019
lines changed

_downloads/6dc1f3f4f0e6ca13cb42ddf4d6cbc8af/tzinfo_examples.py

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,70 @@
1-
from datetime import tzinfo, timedelta, datetime
2-
3-
ZERO = timedelta(0)
4-
HOUR = timedelta(hours=1)
5-
SECOND = timedelta(seconds=1)
1+
import datetime as dt
62

73
# A class capturing the platform's idea of local time.
84
# (May result in wrong values on historical times in
95
# timezones where UTC offset and/or the DST rules had
106
# changed in the past.)
11-
import time as _time
7+
import time
8+
9+
ZERO = dt.timedelta(0)
10+
HOUR = dt.timedelta(hours=1)
11+
SECOND = dt.timedelta(seconds=1)
1212

13-
STDOFFSET = timedelta(seconds = -_time.timezone)
14-
if _time.daylight:
15-
DSTOFFSET = timedelta(seconds = -_time.altzone)
13+
STDOFFSET = dt.timedelta(seconds=-time.timezone)
14+
if time.daylight:
15+
DSTOFFSET = dt.timedelta(seconds=-time.altzone)
1616
else:
1717
DSTOFFSET = STDOFFSET
1818

1919
DSTDIFF = DSTOFFSET - STDOFFSET
2020

21-
class LocalTimezone(tzinfo):
2221

23-
def fromutc(self, dt):
24-
assert dt.tzinfo is self
25-
stamp = (dt - datetime(1970, 1, 1, tzinfo=self)) // SECOND
26-
args = _time.localtime(stamp)[:6]
22+
class LocalTimezone(dt.tzinfo):
23+
24+
def fromutc(self, when):
25+
assert when.tzinfo is self
26+
stamp = (when - dt.datetime(1970, 1, 1, tzinfo=self)) // SECOND
27+
args = time.localtime(stamp)[:6]
2728
dst_diff = DSTDIFF // SECOND
2829
# Detect fold
29-
fold = (args == _time.localtime(stamp - dst_diff))
30-
return datetime(*args, microsecond=dt.microsecond,
31-
tzinfo=self, fold=fold)
30+
fold = (args == time.localtime(stamp - dst_diff))
31+
return dt.datetime(*args, microsecond=when.microsecond,
32+
tzinfo=self, fold=fold)
3233

33-
def utcoffset(self, dt):
34-
if self._isdst(dt):
34+
def utcoffset(self, when):
35+
if self._isdst(when):
3536
return DSTOFFSET
3637
else:
3738
return STDOFFSET
3839

39-
def dst(self, dt):
40-
if self._isdst(dt):
40+
def dst(self, when):
41+
if self._isdst(when):
4142
return DSTDIFF
4243
else:
4344
return ZERO
4445

45-
def tzname(self, dt):
46-
return _time.tzname[self._isdst(dt)]
46+
def tzname(self, when):
47+
return time.tzname[self._isdst(when)]
4748

48-
def _isdst(self, dt):
49-
tt = (dt.year, dt.month, dt.day,
50-
dt.hour, dt.minute, dt.second,
51-
dt.weekday(), 0, 0)
52-
stamp = _time.mktime(tt)
53-
tt = _time.localtime(stamp)
49+
def _isdst(self, when):
50+
tt = (when.year, when.month, when.day,
51+
when.hour, when.minute, when.second,
52+
when.weekday(), 0, 0)
53+
stamp = time.mktime(tt)
54+
tt = time.localtime(stamp)
5455
return tt.tm_isdst > 0
5556

57+
5658
Local = LocalTimezone()
5759

5860

5961
# A complete implementation of current DST rules for major US time zones.
6062

61-
def first_sunday_on_or_after(dt):
62-
days_to_go = 6 - dt.weekday()
63+
def first_sunday_on_or_after(when):
64+
days_to_go = 6 - when.weekday()
6365
if days_to_go:
64-
dt += timedelta(days_to_go)
65-
return dt
66+
when += dt.timedelta(days_to_go)
67+
return when
6668

6769

6870
# US DST Rules
@@ -75,21 +77,22 @@ def first_sunday_on_or_after(dt):
7577
#
7678
# In the US, since 2007, DST starts at 2am (standard time) on the second
7779
# Sunday in March, which is the first Sunday on or after Mar 8.
78-
DSTSTART_2007 = datetime(1, 3, 8, 2)
80+
DSTSTART_2007 = dt.datetime(1, 3, 8, 2)
7981
# and ends at 2am (DST time) on the first Sunday of Nov.
80-
DSTEND_2007 = datetime(1, 11, 1, 2)
82+
DSTEND_2007 = dt.datetime(1, 11, 1, 2)
8183
# From 1987 to 2006, DST used to start at 2am (standard time) on the first
8284
# Sunday in April and to end at 2am (DST time) on the last
8385
# Sunday of October, which is the first Sunday on or after Oct 25.
84-
DSTSTART_1987_2006 = datetime(1, 4, 1, 2)
85-
DSTEND_1987_2006 = datetime(1, 10, 25, 2)
86+
DSTSTART_1987_2006 = dt.datetime(1, 4, 1, 2)
87+
DSTEND_1987_2006 = dt.datetime(1, 10, 25, 2)
8688
# From 1967 to 1986, DST used to start at 2am (standard time) on the last
8789
# Sunday in April (the one on or after April 24) and to end at 2am (DST time)
8890
# on the last Sunday of October, which is the first Sunday
8991
# on or after Oct 25.
90-
DSTSTART_1967_1986 = datetime(1, 4, 24, 2)
92+
DSTSTART_1967_1986 = dt.datetime(1, 4, 24, 2)
9193
DSTEND_1967_1986 = DSTEND_1987_2006
9294

95+
9396
def us_dst_range(year):
9497
# Find start and end times for US DST. For years before 1967, return
9598
# start = end for no DST.
@@ -100,63 +103,63 @@ def us_dst_range(year):
100103
elif 1966 < year < 1987:
101104
dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986
102105
else:
103-
return (datetime(year, 1, 1), ) * 2
106+
return (dt.datetime(year, 1, 1), ) * 2
104107

105108
start = first_sunday_on_or_after(dststart.replace(year=year))
106109
end = first_sunday_on_or_after(dstend.replace(year=year))
107110
return start, end
108111

109112

110-
class USTimeZone(tzinfo):
113+
class USTimeZone(dt.tzinfo):
111114

112115
def __init__(self, hours, reprname, stdname, dstname):
113-
self.stdoffset = timedelta(hours=hours)
116+
self.stdoffset = dt.timedelta(hours=hours)
114117
self.reprname = reprname
115118
self.stdname = stdname
116119
self.dstname = dstname
117120

118121
def __repr__(self):
119122
return self.reprname
120123

121-
def tzname(self, dt):
122-
if self.dst(dt):
124+
def tzname(self, when):
125+
if self.dst(when):
123126
return self.dstname
124127
else:
125128
return self.stdname
126129

127-
def utcoffset(self, dt):
128-
return self.stdoffset + self.dst(dt)
130+
def utcoffset(self, when):
131+
return self.stdoffset + self.dst(when)
129132

130-
def dst(self, dt):
131-
if dt is None or dt.tzinfo is None:
133+
def dst(self, when):
134+
if when is None or when.tzinfo is None:
132135
# An exception may be sensible here, in one or both cases.
133136
# It depends on how you want to treat them. The default
134137
# fromutc() implementation (called by the default astimezone()
135-
# implementation) passes a datetime with dt.tzinfo is self.
138+
# implementation) passes a datetime with when.tzinfo is self.
136139
return ZERO
137-
assert dt.tzinfo is self
138-
start, end = us_dst_range(dt.year)
140+
assert when.tzinfo is self
141+
start, end = us_dst_range(when.year)
139142
# Can't compare naive to aware objects, so strip the timezone from
140-
# dt first.
141-
dt = dt.replace(tzinfo=None)
142-
if start + HOUR <= dt < end - HOUR:
143+
# when first.
144+
when = when.replace(tzinfo=None)
145+
if start + HOUR <= when < end - HOUR:
143146
# DST is in effect.
144147
return HOUR
145-
if end - HOUR <= dt < end:
146-
# Fold (an ambiguous hour): use dt.fold to disambiguate.
147-
return ZERO if dt.fold else HOUR
148-
if start <= dt < start + HOUR:
148+
if end - HOUR <= when < end:
149+
# Fold (an ambiguous hour): use when.fold to disambiguate.
150+
return ZERO if when.fold else HOUR
151+
if start <= when < start + HOUR:
149152
# Gap (a non-existent hour): reverse the fold rule.
150-
return HOUR if dt.fold else ZERO
153+
return HOUR if when.fold else ZERO
151154
# DST is off.
152155
return ZERO
153156

154-
def fromutc(self, dt):
155-
assert dt.tzinfo is self
156-
start, end = us_dst_range(dt.year)
157+
def fromutc(self, when):
158+
assert when.tzinfo is self
159+
start, end = us_dst_range(when.year)
157160
start = start.replace(tzinfo=self)
158161
end = end.replace(tzinfo=self)
159-
std_time = dt + self.stdoffset
162+
std_time = when + self.stdoffset
160163
dst_time = std_time + HOUR
161164
if end <= dst_time < end + HOUR:
162165
# Repeated hour

_sources/c-api/frame.rst.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ See also :ref:`Reflection <reflection>`.
5050
5151
Return a :term:`strong reference`, or ``NULL`` if *frame* has no outer
5252
frame.
53+
This raises no exceptions.
5354
5455
.. versionadded:: 3.9
5556

_sources/c-api/memory.rst.txt

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,11 @@ The following function sets, modeled after the ANSI C standard, but specifying
208208
behavior when requesting zero bytes, are available for allocating and releasing
209209
memory from the Python heap.
210210
211-
The :ref:`default memory allocator <default-memory-allocators>` uses the
212-
:ref:`pymalloc memory allocator <pymalloc>`.
211+
In the GIL-enabled build (default build) the
212+
:ref:`default memory allocator <default-memory-allocators>` uses the
213+
:ref:`pymalloc memory allocator <pymalloc>`, whereas in the
214+
:term:`free-threaded build`, the default is the
215+
:ref:`mimalloc memory allocator <mimalloc>` instead.
213216
214217
.. warning::
215218
@@ -219,6 +222,11 @@ The :ref:`default memory allocator <default-memory-allocators>` uses the
219222
220223
The default allocator is now pymalloc instead of system :c:func:`malloc`.
221224
225+
.. versionchanged:: 3.13
226+
227+
In the :term:`free-threaded <free threading>` build, the default allocator
228+
is now :ref:`mimalloc <mimalloc>`.
229+
222230
.. c:function:: void* PyMem_Malloc(size_t n)
223231
224232
Allocates *n* bytes and returns a pointer of type :c:expr:`void*` to the
@@ -344,7 +352,9 @@ memory from the Python heap.
344352
the :ref:`Customize Memory Allocators <customize-memory-allocators>` section.
345353
346354
The :ref:`default object allocator <default-memory-allocators>` uses the
347-
:ref:`pymalloc memory allocator <pymalloc>`.
355+
:ref:`pymalloc memory allocator <pymalloc>`. In the
356+
:term:`free-threaded <free threading>` build, the default is the
357+
:ref:`mimalloc memory allocator <mimalloc>` instead.
348358
349359
.. warning::
350360
@@ -424,23 +434,24 @@ Default Memory Allocators
424434
425435
Default memory allocators:
426436
427-
=============================== ==================== ================== ===================== ====================
428-
Configuration Name PyMem_RawMalloc PyMem_Malloc PyObject_Malloc
429-
=============================== ==================== ================== ===================== ====================
430-
Release build ``"pymalloc"`` ``malloc`` ``pymalloc`` ``pymalloc``
431-
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
432-
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
433-
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
434-
=============================== ==================== ================== ===================== ====================
437+
=================================== ======================= ==================== ====================== ======================
438+
Configuration Name PyMem_RawMalloc PyMem_Malloc PyObject_Malloc
439+
=================================== ======================= ==================== ====================== ======================
440+
Release build ``"pymalloc"`` ``malloc`` ``pymalloc`` ``pymalloc``
441+
Debug build ``"pymalloc_debug"`` ``malloc`` + debug ``pymalloc`` + debug ``pymalloc`` + debug
442+
Release build, without pymalloc ``"malloc"`` ``malloc`` ``malloc`` ``malloc``
443+
Debug build, without pymalloc ``"malloc_debug"`` ``malloc`` + debug ``malloc`` + debug ``malloc`` + debug
444+
Free-threaded build ``"mimalloc"`` ``mimalloc`` ``mimalloc`` ``mimalloc``
445+
Free-threaded debug build ``"mimalloc_debug"`` ``mimalloc`` + debug ``mimalloc`` + debug ``mimalloc`` + debug
446+
=================================== ======================= ==================== ====================== ======================
435447
436448
Legend:
437449
438450
* Name: value for :envvar:`PYTHONMALLOC` environment variable.
439451
* ``malloc``: system allocators from the standard C library, C functions:
440452
:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`.
441453
* ``pymalloc``: :ref:`pymalloc memory allocator <pymalloc>`.
442-
* ``mimalloc``: :ref:`mimalloc memory allocator <mimalloc>`. The pymalloc
443-
allocator will be used if mimalloc support isn't available.
454+
* ``mimalloc``: :ref:`mimalloc memory allocator <mimalloc>`.
444455
* "+ debug": with :ref:`debug hooks on the Python memory allocators
445456
<pymem-debug-hooks>`.
446457
* "Debug build": :ref:`Python build in debug mode <debug-build>`.
@@ -733,9 +744,27 @@ The mimalloc allocator
733744
734745
.. versionadded:: 3.13
735746
736-
Python supports the mimalloc allocator when the underlying platform support is available.
737-
mimalloc "is a general purpose allocator with excellent performance characteristics.
738-
Initially developed by Daan Leijen for the runtime systems of the Koka and Lean languages."
747+
Python supports the `mimalloc <https://github.com/microsoft/mimalloc/>`__
748+
allocator when the underlying platform support is available.
749+
mimalloc is a general purpose allocator with excellent performance
750+
characteristics, initially developed by Daan Leijen for the runtime systems
751+
of the Koka and Lean languages.
752+
753+
Unlike :ref:`pymalloc <pymalloc>`, which is optimized for small objects (512
754+
bytes or fewer), mimalloc handles allocations of any size.
755+
756+
In the :term:`free-threaded <free threading>` build, mimalloc is the default
757+
and **required** allocator for the :c:macro:`PYMEM_DOMAIN_MEM` and
758+
:c:macro:`PYMEM_DOMAIN_OBJ` domains. It cannot be disabled in free-threaded
759+
builds. The free-threaded build uses per-thread mimalloc heaps, which allows
760+
allocation and deallocation to proceed without locking in most cases.
761+
762+
In the default (non-free-threaded) build, mimalloc is available but not the
763+
default allocator. It can be selected at runtime using
764+
:envvar:`PYTHONMALLOC`\ ``=mimalloc`` (or ``mimalloc_debug`` to include
765+
:ref:`debug hooks <pymem-debug-hooks>`). It can be disabled at build time
766+
using the :option:`--without-mimalloc` configure option, but this option
767+
cannot be combined with :option:`--disable-gil`.
739768
740769
tracemalloc C API
741770
=================

0 commit comments

Comments
 (0)