From e7bf49685071a4d78dca463c371c02901af90d77 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Wed, 17 Jun 2020 18:40:22 -0400 Subject: [PATCH] Fix: python agent: 'time' has no attribute 'clock' The time.clock() function was removed in python 3.8 and is marked as deprecated since python 3.3. See PEP 418 for more details [1]. Solution ===== When the python version is greater than 3.2 use the `time.perf_counter()` function [2]. Otherwise, fall back to `time.clock()`. Introduce a compat module to the lttngust agent package providing the `_clock` function. [1] https://www.python.org/dev/peps/pep-0418/ [2] https://docs.python.org/3/library/time.html#time.perf_counter Signed-off-by: Jonathan Rajotte Signed-off-by: Mathieu Desnoyers Change-Id: I3d6d8b24309d45d43b634dc2a6b4d5dbc12da3aa --- configure.ac | 1 + python-lttngust/lttngust/agent.py | 5 +++-- python-lttngust/lttngust/compat.py | 30 ++++++++++++++++++++++++++++++ python-lttngust/lttngust/debug.py | 5 +++-- 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 python-lttngust/lttngust/compat.py diff --git a/configure.ac b/configure.ac index eb1bb3e6..243762ce 100644 --- a/configure.ac +++ b/configure.ac @@ -576,6 +576,7 @@ AC_CONFIG_FILES([ AC_CONFIG_LINKS([ python-lttngust/lttngust/agent.py:python-lttngust/lttngust/agent.py python-lttngust/lttngust/cmd.py:python-lttngust/lttngust/cmd.py + python-lttngust/lttngust/compat.py:python-lttngust/lttngust/compat.py python-lttngust/lttngust/debug.py:python-lttngust/lttngust/debug.py python-lttngust/lttngust/loghandler.py:python-lttngust/lttngust/loghandler.py ]) diff --git a/python-lttngust/lttngust/agent.py b/python-lttngust/lttngust/agent.py index ebfa2de1..7b750bcc 100644 --- a/python-lttngust/lttngust/agent.py +++ b/python-lttngust/lttngust/agent.py @@ -21,6 +21,7 @@ from __future__ import print_function from __future__ import division import lttngust.debug as dbg import lttngust.loghandler +import lttngust.compat import lttngust.cmd from io import open import threading @@ -368,9 +369,9 @@ def _init_threads(): try: dbg._pdebug('waiting for registration done (expecting {}, timeout is {} s)'.format(reg_expecting, cur_timeout)) - t1 = time.clock() + t1 = lttngust.compat._clock() reg_queue.get(timeout=cur_timeout) - t2 = time.clock() + t2 = lttngust.compat._clock() reg_expecting -= 1 dbg._pdebug('unblocked') diff --git a/python-lttngust/lttngust/compat.py b/python-lttngust/lttngust/compat.py new file mode 100644 index 00000000..9d0dcbb5 --- /dev/null +++ b/python-lttngust/lttngust/compat.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2020 - Jonathan Rajotte +# +# This library is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this library; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +import sys +import time + + +# Support for deprecation of time.clock(). +# Deprecated since python 3.3 and removed in python 3.8. +# See PEP 418 for more details. +def _clock(): + if sys.version_info > (3,2): + clock = time.perf_counter() + else: + clock = time.clock() + return clock diff --git a/python-lttngust/lttngust/debug.py b/python-lttngust/lttngust/debug.py index 6f0e81b1..8ac6b9e5 100644 --- a/python-lttngust/lttngust/debug.py +++ b/python-lttngust/lttngust/debug.py @@ -16,6 +16,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA from __future__ import unicode_literals, print_function +import lttngust.compat import time import sys import os @@ -30,12 +31,12 @@ if _ENABLE_DEBUG: def _pwarning(msg): fname = inspect.stack()[1][3] fmt = '[{:.6f}] LTTng-UST warning: {}(): {}' - print(fmt.format(time.clock(), fname, msg), file=sys.stderr) + print(fmt.format(lttngust.compat._clock(), fname, msg), file=sys.stderr) def _pdebug(msg): fname = inspect.stack()[1][3] fmt = '[{:.6f}] LTTng-UST debug: {}(): {}' - print(fmt.format(time.clock(), fname, msg), file=sys.stderr) + print(fmt.format(lttngust.compat._clock(), fname, msg), file=sys.stderr) _pdebug('debug is enabled') else: -- 2.34.1