Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e39ebe4

Browse files
committed
Merge
2 parents 2c08560 + 855889b commit e39ebe4

8 files changed

Lines changed: 96 additions & 9 deletions

File tree

Doc/library/time.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ The module defines the following functions and data items:
183183

184184
.. versionadded:: 3.3
185185

186+
.. function:: wallclock()
187+
188+
.. index::
189+
single: Wallclock
190+
single: benchmarking
191+
192+
Return the current time in fractions of a second to the system's best ability.
193+
Use this when the most accurate representation of wall-clock is required, i.e.
194+
when "processor time" is inappropriate. The reference point of the returned
195+
value is undefined so only the difference of consecutive calls is valid.
196+
197+
.. versionadded: 3.3
186198
187199
.. function:: ctime([secs])
188200

Doc/library/webbrowser.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,15 @@ Notes:
164164
(4)
165165
Only on Mac OS X platform.
166166

167+
.. versionadded:: 3.3
168+
Support for Chrome/Chromium has been added.
169+
167170
Here are some simple examples::
168171

169-
url = 'http://www.python.org/'
172+
url = 'http://docs.python.org/'
170173

171174
# Open URL in a new tab, if a browser window is already open.
172-
webbrowser.open_new_tab(url + 'doc/')
175+
webbrowser.open_new_tab(url)
173176

174177
# Open URL in new window, raising the window if possible.
175178
webbrowser.open_new(url)

Doc/whatsnew/3.3.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,14 @@ New module: :mod:`faulthandler`.
396396
time
397397
----
398398

399-
* The :mod:`time` module has new :func:`~time.clock_getres` and
400-
:func:`~time.clock_gettime` functions and ``CLOCK_xxx`` constants.
401-
:func:`~time.clock_gettime` can be used with :data:`time.CLOCK_MONOTONIC` to
402-
get a monotonic clock.
399+
The :mod:`time` module has new functions:
403400

404-
(Contributed by Victor Stinner in :issue:`10278`)
401+
* :func:`~time.clock_getres` and :func:`~time.clock_gettime` functions and
402+
``CLOCK_xxx`` constants. :func:`~time.clock_gettime` can be used with
403+
:data:`time.CLOCK_MONOTONIC` to get a monotonic clock.
404+
* :func:`~time.wallclock`: monotonic clock.
405+
406+
(Contributed by Victor Stinner in :issue:`10278`)
405407

406408

407409
ftplib

Lib/bz2.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
__author__ = "Nadeem Vawda <[email protected]>"
1111

1212
import io
13-
import threading
1413
import warnings
1514

15+
try:
16+
from threading import RLock
17+
except ImportError:
18+
from dummy_threading import RLock
19+
1620
from _bz2 import BZ2Compressor, BZ2Decompressor
1721

1822

@@ -53,7 +57,7 @@ def __init__(self, filename=None, mode="r", buffering=None,
5357
"""
5458
# This lock must be recursive, so that BufferedIOBase's
5559
# readline(), readlines() and writelines() don't deadlock.
56-
self._lock = threading.RLock()
60+
self._lock = RLock()
5761
self._fp = None
5862
self._closefp = False
5963
self._mode = _MODE_CLOSED

Lib/test/test_bz2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ def comp():
463463
for t in threads:
464464
t.join()
465465

466+
def testWithoutThreading(self):
467+
bz2 = support.import_fresh_module("bz2", blocked=("threading",))
468+
with bz2.BZ2File(self.filename, "wb") as f:
469+
f.write(b"abc")
470+
with bz2.BZ2File(self.filename, "rb") as f:
471+
self.assertEqual(f.read(), b"abc")
472+
466473
def testMixedIterationAndReads(self):
467474
self.createTempFile()
468475
linelen = len(self.TEXT_LINES[0])

Lib/test/test_time.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,13 @@ def test_mktime_error(self):
332332
self.assertEqual(time.strftime('%Z', tt), tzname)
333333

334334

335+
def test_wallclock(self):
336+
t0 = time.wallclock()
337+
time.sleep(0.1)
338+
t1 = time.wallclock()
339+
t = t1 - t0
340+
self.assertAlmostEqual(t, 0.1, places=2)
341+
335342
class TestLocale(unittest.TestCase):
336343
def setUp(self):
337344
self.oldloc = locale.setlocale(locale.LC_ALL)

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,11 @@ Core and Builtins
447447
Library
448448
-------
449449

450+
- Issue #10278: Add time.wallclock() function, monotonic clock.
451+
452+
- Issue #13809: Fix regression where bz2 module wouldn't work when threads are
453+
disabled. Original patch by Amaury Forgeot d'Arc.
454+
450455
- Issue #13589: Fix some serialization primitives in the aifc module.
451456
Patch by Oleg Plakhotnyuk.
452457

Modules/timemodule.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,52 @@ the local timezone used by methods such as localtime, but this behaviour\n\
737737
should not be relied on.");
738738
#endif /* HAVE_WORKING_TZSET */
739739

740+
static PyObject *
741+
time_wallclock(PyObject *self, PyObject *unused)
742+
{
743+
#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
744+
return time_clock(self, NULL);
745+
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
746+
static int clk_index = 0;
747+
clockid_t clk_ids[] = {
748+
#ifdef CLOCK_MONOTONIC_RAW
749+
CLOCK_MONOTONIC_RAW,
750+
#endif
751+
CLOCK_MONOTONIC
752+
#ifdef CLOCK_REALTIME
753+
/* On Linux, CLOCK_REALTIME uses the same clock than gettimeofday(),
754+
but clock_gettime() has a nanosecond resolution. */
755+
, CLOCK_REALTIME
756+
#endif
757+
};
758+
int ret;
759+
struct timespec tp;
760+
761+
while (0 <= clk_index) {
762+
clockid_t clk_id = clk_ids[clk_index];
763+
ret = clock_gettime(clk_id, &tp);
764+
if (ret == 0)
765+
return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
766+
767+
clk_index++;
768+
if (Py_ARRAY_LENGTH(clk_ids) <= clk_index)
769+
clk_index = -1;
770+
}
771+
return time_time(self, NULL);
772+
#else
773+
return time_time(self, NULL);
774+
#endif
775+
}
776+
777+
PyDoc_STRVAR(wallclock_doc,
778+
"wallclock() -> float\n\
779+
\n\
780+
Return the current time in fractions of a second to the system's best\n\
781+
ability. Use this when the most accurate representation of wall-clock is\n\
782+
required, i.e. when \"processor time\" is inappropriate. The reference point\n\
783+
of the returned value is undefined so only the difference of consecutive\n\
784+
calls is valid.");
785+
740786
static void
741787
PyInit_timezone(PyObject *m) {
742788
/* This code moved from PyInit_time wholesale to allow calling it from
@@ -872,6 +918,7 @@ static PyMethodDef time_methods[] = {
872918
#ifdef HAVE_WORKING_TZSET
873919
{"tzset", time_tzset, METH_NOARGS, tzset_doc},
874920
#endif
921+
{"wallclock", time_wallclock, METH_NOARGS, wallclock_doc},
875922
{NULL, NULL} /* sentinel */
876923
};
877924

0 commit comments

Comments
 (0)