Fix: python agent: 'time' has no attribute 'clock'
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Wed, 17 Jun 2020 22:40:22 +0000 (18:40 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 30 Jun 2020 15:11:22 +0000 (11:11 -0400)
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 <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I3d6d8b24309d45d43b634dc2a6b4d5dbc12da3aa

configure.ac
python-lttngust/lttngust/agent.py
python-lttngust/lttngust/compat.py [new file with mode: 0644]
python-lttngust/lttngust/debug.py

index eb1bb3e6b52a51bdaf878dafe73e3af395e8b9c8..243762ced438bf52c25918c116a6811e3c7b42be 100644 (file)
@@ -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
 ])
index ebfa2de1dbb8abbf0b51eb89556558efe034ea74..7b750bcc6f96fde62f0dd82a2935a80d678ecc8f 100644 (file)
@@ -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 (file)
index 0000000..9d0dcbb
--- /dev/null
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2020 - Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+#
+# 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
index 6f0e81b17cb690a4754bdd78ca1e6587d6abd939..8ac6b9e5705a2a2d4dc01987dfd9564e942d6d68 100644 (file)
@@ -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:
This page took 0.027752 seconds and 4 git commands to generate.