Edgewall Software
Modify

#13603 closed defect (fixed)

Python 3.12.0b1: shutil.rmtree: DeprecationWarning: onerror argument is deprecated, use onexc instead

Reported by: Jun Omae Owned by: Jun Omae
Priority: normal Milestone: 1.6
Component: general Version:
Severity: normal Keywords:
Cc: Branch:
Release Notes:

Use datetime.fromtimestamp(..., utc) instead of deprecated datetime.utcfromtimestamp to avoid deprecation messages since Python 3.12.

API Changes:
Internal Changes:

Description

The following is warned on invoking shutil.rmtree(onerror=...) with Python 3.12.

trac/test.py:555: DeprecationWarning: onerror argument is deprecated, use onexc instead
  shutil.rmtree(path, onerror=onerror)

See also: https://docs.python.org/3.12/library/shutil.html?highlight=onexc#shutil.rmtree

Patch:

  • trac/test.py

    index e6102be75..197b63315 100755
    a b def locate(fn):  
    527527    return None
    528528
    529529
     530if hasattr(inspect, 'getfullargspec'):  # Python 3.11+
     531    _rmtree_argspec = inspect.getfullargspec(shutil.rmtree)
     532    # onexc is added in Python 3.12 and onerror is deprecated
     533    _rmtree_has_onexc = 'onexc' in getattr(_rmtree_argspec, 'kwonlyargs', [])
     534    del _rmtree_argspec
     535else:
     536    _rmtree_has_onexc = False
     537
     538
    530539def rmtree(path):
    531540    def onerror(function, path, excinfo, retry=1):
    532541        # `os.unlink` and `os.remove` fail for a readonly file on Windows.
    def rmtree(path):  
    552561        # Use unicode characters in order to allow non-ansi characters
    553562        # on Windows.
    554563        path = str(path, sys.getfilesystemencoding())
    555     shutil.rmtree(path, onerror=onerror)
     564    kwargs = {'onexc' if _rmtree_has_onexc else 'onerror': onerror}
     565    shutil.rmtree(path, **kwargs)
    556566
    557567
    558568INCLUDE_FUNCTIONAL_TESTS = True

Attachments (0)

Change History (2)

comment:1 by Jun Omae, 11 months ago

I missed that onexc and onerror have different third parameter.

def onerror(function, path, excinfo): ...  # `excinfo` is sys.exc_info()
def onexc(function, path, e): ...  # `e` is an instance of Exception

New proposed changes in [a822d8e86/jomae.git] (jomae.git@t13603).

comment:2 by Jun Omae, 11 months ago

Release Notes: modified (diff)
Resolution: fixed
Status: assignedclosed

Fixed in [17706].

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Jun Omae.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Jun Omae to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.