Skip to content

Commit

Permalink
devopshqGH-467: override __deepcopy__() method for python 3.9 and l…
Browse files Browse the repository at this point in the history
…ower compatibility
  • Loading branch information
miskeens committed Jan 14, 2025
1 parent e386952 commit 631ac89
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion artifactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
pure paths can be used.
"""
import collections
import copy
import datetime
import errno
import fnmatch
Expand All @@ -35,7 +36,7 @@
import re
import sys
import urllib.parse
from itertools import islice
from itertools import islice, chain

import dateutil.parser
import requests
Expand Down Expand Up @@ -1571,6 +1572,33 @@ def __reduce__(self):
pathlib_reduce = super().__reduce__()
return pathlib_reduce[0], pathlib_reduce[1], self.__dict__

def __deepcopy__(self, memo):
"""
Adapted from https://gist.github.com/orbingol/5cbcee7cafcf4e26447d87fe36b6467a#file-copy_deepcopy-py-L65
"""
# Create a new instance
result = self.__class__.__new__(self.__class__)

# Don't copy self reference
memo[id(self)] = result

# Don't copy the cache - if it exists
if hasattr(self, "_cache"):
memo[id(self._cache)] = self._cache.__new__(dict)

# Get all __slots__ of the derived class
slots = chain.from_iterable(getattr(s, "__slots__", []) for s in self.__class__.__mro__)

# Deep copy all other attributes
for var in slots:
# Since we process the whole inheritance chain from __mro__, there might be some attributes from parent
# classes missing in the current object. Marking these attributes as "undefined-attribute" to skip assigning
if getattr(self, var, "undefined-attribute") != "undefined-attribute":
setattr(result, var, copy.deepcopy(getattr(self, var), memo))

# Return updated instance
return result

@property
def top(self):
obj = ArtifactoryPath(self.drive)
Expand Down

0 comments on commit 631ac89

Please sign in to comment.