summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-10-13 10:59:45 -0700
committerGitHub <noreply@github.com>2021-10-13 19:59:45 +0200
commitf40b230df91f29fa4a84c1d127b4eab56a6eda27 (patch)
tree0a31e70a791ba8e3a0e7f748dad539ae46633f93
parentfd2be6da2ffec2301a58aefa92f17f0c567fbc55 (diff)
downloadcpython-git-f40b230df91f29fa4a84c1d127b4eab56a6eda27.tar.gz
bpo-45386: Handle strftime's ValueError graciously in xmlrpc.client (GH-28765) (GH-28934)
At import time, the xmlrpc.client module uses different date formats to test strftime so it can format years with 4 digits consistently. Depending on the underlying C library and its strftime implementation some of these calls can result in ValueErrors, blocking the xmlrpc.client module from being imported. This commit changes the behavior of this bit of code to react to ValueError exceptions, treating the format that caused them as an non-viable option. (cherry picked from commit 1c831353816ff699b54e804047a7242a09e98f5b) Co-authored-by: rtobar <rtobarc@gmail.com>
-rw-r--r--Lib/xmlrpc/client.py10
-rw-r--r--Misc/NEWS.d/next/Library/2021-10-07-00-05-05.bpo-45386.q9ORpA.rst3
2 files changed, 11 insertions, 2 deletions
diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py
index 9e7449c88d..a614cef6ab 100644
--- a/Lib/xmlrpc/client.py
+++ b/Lib/xmlrpc/client.py
@@ -264,16 +264,22 @@ boolean = Boolean = bool
# Issue #13305: different format codes across platforms
_day0 = datetime(1, 1, 1)
-if _day0.strftime('%Y') == '0001': # Mac OS X
+def _try(fmt):
+ try:
+ return _day0.strftime(fmt) == '0001'
+ except ValueError:
+ return False
+if _try('%Y'): # Mac OS X
def _iso8601_format(value):
return value.strftime("%Y%m%dT%H:%M:%S")
-elif _day0.strftime('%4Y') == '0001': # Linux
+elif _try('%4Y'): # Linux
def _iso8601_format(value):
return value.strftime("%4Y%m%dT%H:%M:%S")
else:
def _iso8601_format(value):
return value.strftime("%Y%m%dT%H:%M:%S").zfill(17)
del _day0
+del _try
def _strftime(value):
diff --git a/Misc/NEWS.d/next/Library/2021-10-07-00-05-05.bpo-45386.q9ORpA.rst b/Misc/NEWS.d/next/Library/2021-10-07-00-05-05.bpo-45386.q9ORpA.rst
new file mode 100644
index 0000000000..eec77ceccf
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-10-07-00-05-05.bpo-45386.q9ORpA.rst
@@ -0,0 +1,3 @@
+Make :mod:`xmlrpc.client` more robust to C runtimes where the underlying C
+``strftime`` function results in a ``ValueError`` when testing for year
+formatting options.