Skip to content

Commit

Permalink
bpo-45386: Handle strftime's ValueError graciously in xmlrpc.client (G…
Browse files Browse the repository at this point in the history
…H-28765) (GH-28935)

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 <rtobarc@gmail.com>
  • Loading branch information
miss-islington and rtobar authored Oct 13, 2021
1 parent 5d74713 commit 9210eff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Lib/xmlrpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,22 @@ def __repr__(self):

# 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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 9210eff

Please sign in to comment.