Skip to content

Commit 54081df

Browse files
authored
Add clocks (#1737)
* Add clocks * Disable test
1 parent 42e6a0e commit 54081df

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

Src/IronPython.Modules/time.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Threading;
1818

1919
using IronPython.Runtime;
20+
using IronPython.Runtime.Exceptions;
2021
using IronPython.Runtime.Operations;
2122
using IronPython.Runtime.Types;
2223

@@ -106,18 +107,31 @@ public static double clock() {
106107

107108
public static double perf_counter() => clock();
108109

110+
public static double process_time() => Process.GetCurrentProcess().TotalProcessorTime.TotalSeconds;
111+
109112
public static string ctime(CodeContext/*!*/ context)
110113
=> asctime(context, localtime());
111114

112115
public static string ctime(CodeContext/*!*/ context, object? seconds)
113116
=> asctime(context, localtime(seconds));
114117

115-
public static object get_clock_info([NotNone] string name) {
118+
public static object get_clock_info(CodeContext/*!*/ context, [NotNone] string name) {
116119
// TODO: Fill with correct values
117-
if (name == "monotonic")
118-
return new SimpleNamespace(new Dictionary<string, object?> { { "adjustable", false }, { "implementation", "Stopwatch.GetTimestamp" }, { "monotonic", true }, { "resolution", 0.015625 } });
119-
120-
throw new NotImplementedException();
120+
switch (name) {
121+
case "monotonic":
122+
return new SimpleNamespace(new Dictionary<string, object?> { { "adjustable", false }, { "implementation", "Stopwatch.GetTimestamp" }, { "monotonic", true }, { "resolution", 0.015625 } });
123+
case "perf_counter":
124+
return new SimpleNamespace(new Dictionary<string, object?> { { "adjustable", false }, { "implementation", "Stopwatch.ElapsedTicks" }, { "monotonic", true }, { "resolution", 1e-7 } });
125+
case "process_time":
126+
return new SimpleNamespace(new Dictionary<string, object?> { { "adjustable", false }, { "implementation", "Process.TotalProcessorTime" }, { "monotonic", true }, { "resolution", 1e-7 } });
127+
case "clock":
128+
PythonOps.Warn(context, PythonExceptions.DeprecationWarning, "time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead");
129+
return new SimpleNamespace(new Dictionary<string, object?> { { "adjustable", false }, { "implementation", "Stopwatch.ElapsedTicks" }, { "monotonic", true }, { "resolution", 1e-7 } });
130+
case "time":
131+
return new SimpleNamespace(new Dictionary<string, object?> { { "adjustable", true }, { "implementation", "DateTime.Now" }, { "monotonic", false }, { "resolution", 0.015625 } });
132+
default:
133+
throw PythonOps.ValueError("unknown clock");
134+
}
121135
}
122136

123137
public static void sleep(double tm) {

Tests/test_time_stdlib.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@
66
## Run selected tests from test_time from StdLib
77
##
88

9-
from iptest import is_ironpython, generate_suite, run_test
9+
from iptest import is_ironpython, is_osx, is_netcoreapp21, generate_suite, run_test
1010

1111
import test.test_time
1212

1313
def load_tests(loader, standard_tests, pattern):
1414
tests = loader.loadTestsFromModule(test.test_time)
1515

1616
if is_ironpython:
17-
1817
failing_tests = [
1918
test.test_time.TestAsctime4dyear('test_large_year'), # ValueError: year is too high
2019
test.test_time.TestAsctime4dyear('test_negative'), # ValueError: year is too low
2120
test.test_time.TimeTestCase('test_asctime'), # ValueError: year is too high
2221
test.test_time.TimeTestCase('test_asctime_bounding_check'), # ValueError: Hour, Minute, and Second parameters describe an un-representable DateTime.
23-
test.test_time.TimeTestCase('test_clock'), # NotImplementedError: get_clock_info('clock')
24-
test.test_time.TimeTestCase('test_get_clock_info'), # NotImplementedError: get_clock_info
2522
test.test_time.TimeTestCase('test_insane_timestamps'), # ValueError: unreasonable date/time
2623
test.test_time.TimeTestCase('test_mktime_error'), # ValueError: year is too low
27-
test.test_time.TimeTestCase('test_process_time'), # AttributeError: 'module' object has no attribute 'process_time'
2824
test.test_time.TimeTestCase('test_strftime_bounding_check'), # ValueError: Hour, Minute, and Second parameters describe an un-representable DateTime.
29-
test.test_time.TimeTestCase('test_time'), # NotImplementedError: get_clock_info('time')
3025
test.test_time.TimeTestCase('test_default_values_for_zero'), # AssertionError: '2000 01 01 00 00 00 1 001' != '2000 01 01 00 00 00 6 001'
3126
]
3227

28+
if is_netcoreapp21 and is_osx:
29+
failing_tests += [
30+
test.test_time.TimeTestCase('test_process_time'), # AssertionError
31+
]
32+
3333
skip_tests = []
3434

3535
return generate_suite(tests, failing_tests, skip_tests)

WhatsNewInPython33.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Deprecated Python modules, functions and methods
117117
- [ ] `platform.popen()`: use the `subprocess` module. Check especially the "Replacing Older Functions with the `subprocess` Module" section (issue 11377).
118118
- [ ] The Windows `bytes` API has been deprecated in the `os` module. Use Unicode filenames, instead of `bytes` filenames, to not depend on the ANSI code page anymore and to support any filename.
119119
- [x] The `xml.etree.cElementTree` module is deprecated. The accelerator is used automatically whenever available.
120-
- [ ] The behaviour of `time.clock()` depends on the platform: use the new `time.perf_counter()` or `time.process_time()` function instead, depending on your requirements, to have a well defined behaviour.
120+
- [x] The behaviour of `time.clock()` depends on the platform: use the new `time.perf_counter()` or `time.process_time()` function instead, depending on your requirements, to have a well defined behaviour.
121121
- [x] ~~The `os.stat_float_times()` function is deprecated.~~ (Never implemented, but removed in Python 3.7)
122122
- `abc` module:
123123
+ [ ] `abc.abstractproperty` has been deprecated, use `property` with `abc.abstractmethod()` instead.

0 commit comments

Comments
 (0)