From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 5280D385842E for ; Thu, 5 Jan 2023 16:30:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5280D385842E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672936215; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KO9YyopE73FEuf0zmBe/UOkfRYmqatKj3QZj1zJNn4I=; b=PaByKvlcjVQqxOSo5CCu/rrx48oiH0e/oluTsGOFNdhDdMIZjjxxDJjrzPKr32Nd/x0dSX mvKeBxTmp/XCtgYt6t4UJvEhRxm36Qid7pCSBsWedYSXX0A5UEaWxtkyEwDefNu6d146JU lsUeozXQhwBPTKTySwirG8ehbTd8k2o= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-661-laIXYxIgPzO2Uu7KMtmupA-1; Thu, 05 Jan 2023 11:30:11 -0500 X-MC-Unique: laIXYxIgPzO2Uu7KMtmupA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EEF4229324B0; Thu, 5 Jan 2023 16:30:07 +0000 (UTC) Received: from localhost (unknown [10.33.36.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id B6F3C492B06; Thu, 5 Jan 2023 16:30:07 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix printers for Python 2 [PR108212] Date: Thu, 5 Jan 2023 16:30:07 +0000 Message-Id: <20230105163007.82096-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested powerpc64le-linux (python 2.7) and x86_64-linux (Python 3.10). Pushed to trunk. -- >8 -- The datetime.timezone.utc singleton doesn't exist in Python 2, but we can create it ourselves by deriving from datetime.tzinfo. libstdc++-v3/ChangeLog: PR libstdc++/108212 * python/libstdcxx/v6/printers.py (_utc_timezone): New global variable. (StdChronoTimePointPrinter::to_string): Use it. --- libstdc++-v3/python/libstdcxx/v6/printers.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index 7e694f48f28..8cfb4f26b0e 100644 --- a/libstdc++-v3/python/libstdcxx/v6/printers.py +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py @@ -19,7 +19,7 @@ import gdb import itertools import re import sys, os, errno -from datetime import datetime, timezone +import datetime ### Python 2 + Python 3 compatibility code @@ -45,6 +45,7 @@ if sys.version_info[0] > 2: izip = zip # Also, int subsumes long long = int + _utc_timezone = datetime.timezone.utc else: ### Python 2 stuff class Iterator: @@ -64,6 +65,20 @@ else: # In Python 2, we still need these from itertools from itertools import imap, izip + # Python 2 does not provide the datetime.UTC singleton. + class UTC(datetime.tzinfo): + """Concrete tzinfo class representing the UTC time zone""" + + def utcoffset(self, dt): + return datetime.timedelta(0) + + def tzname(self, dt): + return "UTC" + + def dst(self, dt): + return datetime.timedelta(0) + _utc_timezone = UTC() + # Try to use the new-style pretty-printing if available. _use_gdb_pp = True try: @@ -1955,7 +1970,7 @@ class StdChronoTimePointPrinter: num, den = printer._ratio() secs = (r * num / den) + offset try: - dt = datetime.fromtimestamp(secs, timezone.utc) + dt = datetime.fromtimestamp(secs, _utc_timezone) time = ' [{:%Y-%m-%d %H:%M:%S}]'.format(dt) except: pass -- 2.39.0