Skip to content

Commit f40b230

Browse files
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 1c83135) Co-authored-by: rtobar <[email protected]>
1 parent fd2be6d commit f40b230

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Lib/xmlrpc/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,22 @@ def __repr__(self):
264264

265265
# Issue #13305: different format codes across platforms
266266
_day0 = datetime(1, 1, 1)
267-
if _day0.strftime('%Y') == '0001': # Mac OS X
267+
def _try(fmt):
268+
try:
269+
return _day0.strftime(fmt) == '0001'
270+
except ValueError:
271+
return False
272+
if _try('%Y'): # Mac OS X
268273
def _iso8601_format(value):
269274
return value.strftime("%Y%m%dT%H:%M:%S")
270-
elif _day0.strftime('%4Y') == '0001': # Linux
275+
elif _try('%4Y'): # Linux
271276
def _iso8601_format(value):
272277
return value.strftime("%4Y%m%dT%H:%M:%S")
273278
else:
274279
def _iso8601_format(value):
275280
return value.strftime("%Y%m%dT%H:%M:%S").zfill(17)
276281
del _day0
282+
del _try
277283

278284

279285
def _strftime(value):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Make :mod:`xmlrpc.client` more robust to C runtimes where the underlying C
2+
``strftime`` function results in a ``ValueError`` when testing for year
3+
formatting options.

0 commit comments

Comments
 (0)