Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Oct 17, 2017

time.clock() and time.get_clock_info('clock') now emit a DeprecationWarning warning.

https://bugs.python.org/issue31803

bpo-31803: time.clock() and time.get_clock_info('clock') now emit a
DeprecationWarning warning.
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you forgot to add a deprecation in get_clock_info('clock')?

for name in clocks:
info = time.get_clock_info(name)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "", DeprecationWarning)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment, otherwise we can forgot why this was added.

But I prefer moving all the code from the loop body to a local function and calling it multiple times.

def test(name):
    ...
test('perf_counter')
test('process_time')
test('time')
if hasattr(time, 'monotonic'):
    test('monotonic')
with self.assertWarns(DeprecationWarning):
    test('clock')

This silences a warning only for clock, tests that a warning for clock is raised, and in case of test failure the traceback contains a line with a timer's name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the code to make the warning check explicit.

Moreover, hasattr(time, 'monotonic') test is outdated: time.monotonic() is always available since Python 3.5. I also fixed that.

Explicitly check that get_clock_info('clock') emits a
DeprecationWarning.

Remove also hasattr(time, 'monotonic') since time.monotonic() is now
always available since Python 3.5.
@vstinner
Copy link
Member Author

vstinner commented Oct 17, 2017

Did you forgot to add a deprecation in get_clock_info('clock')?

get_clock_info('clock') also emits a DeprecationWarning. I modified pyclock() which is used by time.clock() and time.get_clock_info('clock').

I just modified test_get_clock_info() to ensure that time.get_clock_info('clock') raises a warning, instead of ignoring the warning.


def test_clock(self):
time.clock()
with support.check_warnings(('time.clock has been deprecated',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use self.assertWarns()? I think support.check_warnings() is for 2.7 code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know assertWarns(). Why do we still have support.check_warnings()? check_warnings() docstring doesn't mention assertWarns(). It's still widely used in the test suite:

haypo@selma$ grep check_warnings Lib/test/test*py|wc -l
115

Anyway, I modified my PR to use assertWarns().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe all this is inherited from Python 2. Or other developers don't know assertWarns(). Or check_warnings() can be used in the cases when assertWarns() can't (not this case).

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants