From 91c0274bc441edf07f8923009244bd068e9368d9 Mon Sep 17 00:00:00 2001 From: Stefan Krah Date: Mon, 2 Apr 2012 20:51:08 +0200 Subject: Improve comments. --- Modules/_decimal/_decimal.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index d5d1134..32e336b 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -1510,7 +1510,7 @@ current_context(void) #define CURRENT_CONTEXT_ADDR(ctx) \ ctx = CTX(current_context()) -/* Return current context, increment reference */ +/* Return a new reference to the current context */ static PyObject * PyDec_GetCurrentContext(void) { @@ -1614,7 +1614,7 @@ current_context(void) ctx = CTX(_c_t_x_o_b_j); \ } -/* Return current context, increment reference */ +/* Return a new reference to the current context */ static PyObject * PyDec_GetCurrentContext(void) { @@ -1759,7 +1759,7 @@ static PyTypeObject PyDecContextManager_Type = 0, /* tp_print */ (getattrfunc) 0, /* tp_getattr */ (setattrfunc) 0, /* tp_setattr */ - 0, /* tp_compare */ + 0, /* tp_reserved */ (reprfunc) 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ @@ -2699,7 +2699,7 @@ ctx_create_decimal(PyObject *context, PyObject *args) /******************************************************************************/ -/* Implicit conversions to Decimal */ +/* Implicit conversions to Decimal */ /******************************************************************************/ /* Try to convert PyObject v to a new PyDecObject conv. If the conversion @@ -2796,7 +2796,7 @@ convert_op(int type_err, PyObject **conv, PyObject *v, PyObject *context) /******************************************************************************/ -/* Implicit conversions to Decimal for comparison */ +/* Implicit conversions to Decimal for comparison */ /******************************************************************************/ /* Convert rationals for comparison */ -- cgit v0.12 From 1470f35bc62c3cf21b209fa8e353d8ea9e71b46b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 3 Apr 2012 00:31:17 +0200 Subject: Add time.CLOCK_HIGHRES constant, needed on Solaris --- Doc/library/time.rst | 9 +++++++++ Modules/timemodule.c | 3 +++ 2 files changed, 12 insertions(+) diff --git a/Doc/library/time.rst b/Doc/library/time.rst index 581b68a..f87fa64 100644 --- a/Doc/library/time.rst +++ b/Doc/library/time.rst @@ -159,6 +159,15 @@ The module defines the following functions and data items: .. versionadded:: 3.3 +.. data:: CLOCK_HIGHRES + + The Solaris OS has a CLOCK_HIGHRES timer that attempts to use an optimal + hardware source, and may give close to nanosecond resolution. CLOCK_HIGHRES + is the nonadjustable, high-resolution clock. + + .. versionadded:: 3.3 + + .. data:: CLOCK_MONOTONIC Clock that cannot be set and represents monotonic time since some diff --git a/Modules/timemodule.c b/Modules/timemodule.c index f44e0c4..0fe1b17 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -962,6 +962,9 @@ PyInit_timezone(PyObject *m) { #ifdef CLOCK_MONOTONIC_RAW PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW); #endif +#ifdef CLOCK_HIGHRES + PyModule_AddIntMacro(m, CLOCK_HIGHRES); +#endif #ifdef CLOCK_PROCESS_CPUTIME_ID PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID); #endif -- cgit v0.12 From 30d79471bbc97e81b123806d41f1e14ba54062cf Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 3 Apr 2012 00:45:07 +0200 Subject: Expose clock_settime() as time.clock_settime() --- Doc/library/time.rst | 7 +++++++ Lib/test/test_time.py | 11 +++++++++++ Modules/timemodule.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/Doc/library/time.rst b/Doc/library/time.rst index f87fa64..8156b01 100644 --- a/Doc/library/time.rst +++ b/Doc/library/time.rst @@ -151,6 +151,13 @@ The module defines the following functions and data items: .. versionadded:: 3.3 +.. function:: clock_settime(clk_id, time) + + Set the time of the specified clock *clk_id*. + + .. versionadded:: 3.3 + + .. data:: CLOCK_REALTIME System-wide real-time clock. Setting this clock requires appropriate diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 28d018a..fb2489c 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -47,6 +47,17 @@ class TimeTestCase(unittest.TestCase): self.assertGreater(res, 0.0) self.assertLessEqual(res, 1.0) + @unittest.skipUnless(hasattr(time, 'clock_settime'), + 'need time.clock_settime()') + def test_clock_settime(self): + t = time.clock_gettime(time.CLOCK_REALTIME) + try: + time.clock_settime(time.CLOCK_REALTIME, t) + except PermissionError: + pass + + self.assertRaises(OSError, time.clock_settime, time.CLOCK_MONOTONIC, 0) + def test_conversions(self): self.assertEqual(time.ctime(self.t), time.asctime(time.localtime(self.t))) diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 0fe1b17..23f3ddd 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -158,6 +158,33 @@ PyDoc_STRVAR(clock_gettime_doc, "clock_gettime(clk_id) -> floating point number\n\ \n\ Return the time of the specified clock clk_id."); + +static PyObject * +time_clock_settime(PyObject *self, PyObject *args) +{ + clockid_t clk_id; + PyObject *obj; + struct timespec tp; + int ret; + + if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj)) + return NULL; + + if (_PyTime_ObjectToTimespec(obj, &tp.tv_sec, &tp.tv_nsec) == -1) + return NULL; + + ret = clock_settime((clockid_t)clk_id, &tp); + if (ret != 0) { + PyErr_SetFromErrno(PyExc_IOError); + return NULL; + } + Py_RETURN_NONE; +} + +PyDoc_STRVAR(clock_settime_doc, +"clock_settime(clk_id, time)\n\ +\n\ +Set the time of the specified clock clk_id."); #endif #ifdef HAVE_CLOCK_GETRES @@ -983,6 +1010,9 @@ static PyMethodDef time_methods[] = { #ifdef HAVE_CLOCK_GETTIME {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc}, #endif +#ifdef HAVE_CLOCK_GETTIME + {"clock_settime", time_clock_settime, METH_VARARGS, clock_settime_doc}, +#endif #ifdef HAVE_CLOCK_GETRES {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc}, #endif -- cgit v0.12 From 927d87470a0b6f7b0ca3f3a1b39fd3bdc0ec919a Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Mon, 2 Apr 2012 20:33:56 -0400 Subject: If a module injects something into sys.modules as a side-effect of importation, then respect that injection. Discovered thanks to Lib/xml/parsers/expat.py injecting xml.parsers.expat.errors and etree now importing that directly as a module. --- Lib/importlib/_bootstrap.py | 3 +++ Lib/importlib/test/import_/test_packages.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index e0f86fc..d81e949 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -927,6 +927,9 @@ def _find_and_load(name, import_): if parent: if parent not in sys.modules: import_(parent) + # Crazy side-effects! + if name in sys.modules: + return sys.modules[name] # Backwards-compatibility; be nicer to skip the dict lookup. parent_module = sys.modules[parent] try: diff --git a/Lib/importlib/test/import_/test_packages.py b/Lib/importlib/test/import_/test_packages.py index faadc32..9590d5f 100644 --- a/Lib/importlib/test/import_/test_packages.py +++ b/Lib/importlib/test/import_/test_packages.py @@ -27,6 +27,19 @@ class ParentModuleTests(unittest.TestCase): with self.assertRaises(ImportError): import_util.import_('sys.no_submodules_here') + def test_module_not_package_but_side_effects(self): + # If a module injects something into sys.modules as a side-effect, then + # pick up on that fact. + name = 'mod' + subname = name + '.b' + def module_injection(): + sys.modules[subname] = 'total bunk' + mock_modules = util.mock_modules('mod', + module_code={'mod': module_injection}) + with mock_modules as mock: + with util.import_state(meta_path=[mock]): + submodule = import_util.import_(subname) + def test_main(): from test.support import run_unittest -- cgit v0.12