
    h                         S r SSKrSSKr\R                  S   r " S S\5      r " S S\5      r\R                  S5      r	\	4S	 jr
\	\
l        C	SS
 jrS r\" SS5      r\" S\5      r\" S\5      rg)a6  
Provides shims for compatibility between versions of dill and Python.

Compatibility shims should be provided in this file. Here are two simple example
use cases.

Deprecation of constructor function:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Assume that we were transitioning _import_module in _dill.py to
the builtin function importlib.import_module when present.

@move_to(_dill)
def _import_module(import_name):
    ... # code already in _dill.py

_import_module = Getattr(importlib, 'import_module', Getattr(_dill, '_import_module', None))

The code will attempt to find import_module in the importlib module. If not
present, it will use the _import_module function in _dill.

Emulate new Python behavior in older Python versions:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CellType.cell_contents behaves differently in Python 3.6 and 3.7. It is
read-only in Python 3.6 and writable and deletable in 3.7.

if _dill.OLD37 and _dill.HAS_CTYPES and ...:
    @move_to(_dill)
    def _setattr(object, name, value):
        if type(object) is _dill.CellType and name == 'cell_contents':
            _PyCell_Set.argtypes = (ctypes.py_object, ctypes.py_object)
            _PyCell_Set(object, value)
        else:
            setattr(object, name, value)
... # more cases below

_setattr = Getattr(_dill, '_setattr', setattr)

_dill._setattr will be used when present to emulate Python 3.7 functionality in
older versions of Python while defaulting to the standard setattr in 3.7+.

See this PR for the discussion that lead to this system:
https://github.com/uqfoundation/dill/pull/443
    Nz
dill._dillc                   B    \ rS rSrSrS/rS rS rS rS r	S r
S	 rS
rg)Reduce:   ag  
Reduce objects are wrappers used for compatibility enforcement during
unpickle-time. They should only be used in calls to pickler.save and
other Reduce objects. They are only evaluated within unpickler.load.

Pickling a Reduce object makes the two implementations equivalent:

pickler.save(Reduce(*reduction))

pickler.save_reduce(*reduction, obj=reduction)
	reductionc                     UR                  SS5      nU(       a  [        R                  [        5      nO[        R                  [        5      nXl        U$ )ap  
Args:
    *reduction: a tuple that matches the format given here:
      https://docs.python.org/3/library/pickle.html#object.__reduce__
    is_callable: a bool to indicate that the object created by
      unpickling `reduction` is callable. If true, the current Reduce
      is allowed to be used as the function in further save_reduce calls
      or Reduce objects.
is_callableF)getobject__new___CallableReducer   r   )clsr   kwargsr   selfs        E/home/james-whalen/.local/lib/python3.13/site-packages/dill/_shims.pyr   Reduce.__new__G   s=     jj6>>/2D>>&)D"    c                 "    SU R                   < 3$ )Nr   r   r   s    r   __repr__Reduce.__repr__X   s    !^^--r   c                     U $ N r   s    r   __copy__Reduce.__copy__Z       r   c                     U $ r   r   )r   memos     r   __deepcopy__Reduce.__deepcopy__\   r   r   c                     U R                   $ r   r   r   s    r   
__reduce__Reduce.__reduce__^   s    ~~r   c                 "    U R                  5       $ r   r#   )r   protocols     r   __reduce_ex__Reduce.__reduce_ex__`   s      r   r   N)__name__
__module____qualname____firstlineno____doc__	__slots__r   r   r   r    r#   r(   __static_attributes__r   r   r   r   r   :   s.    
 I".!r   r   c                       \ rS rSrS rSrg)r   c   c                 P    U R                  5       nUS   nUS   nU" U6 nU" U0 UD6$ )Nr      r&   )r   argsr   r   funcf_argsobjs          r   __call___CallableReduce.__call__f   s9    OO%	|1FmD#F##r   r   N)r*   r+   r,   r-   r9   r0   r   r   r   r   r   c   s    $r   r   zGetattr.NO_DEFAULTc                 x    U[         R                  L a
  [        X44nO
[        XU44n[        US[	        U5      06$ )a  
A Reduce object that represents the getattr operation. When unpickled, the
Getattr will access an attribute 'name' of 'object' and return the value
stored there. If the attribute doesn't exist, the default value will be
returned if present.

The following statements are equivalent:

Getattr(collections, 'OrderedDict')
Getattr(collections, 'spam', None)
Getattr(*args)

Reduce(getattr, (collections, 'OrderedDict'))
Reduce(getattr, (collections, 'spam', None))
Reduce(getattr, args)

During unpickling, the first two will result in collections.OrderedDict and
None respectively because the first attribute exists and the second one does
not, forcing it to use the default value given in the third argument.
r   )Getattr
NO_DEFAULTgetattrr   callable)r
   namedefaultr   s       r   r<   r<   o   sB    , '$$$vn-	vW56	9<(7*;<<r   c                    ^ ^ U U4S jnU$ )Nc                 l   > Tc  U R                   nOTnU TR                  U'   TR                   U l        U $ r   )r*   __dict__r+   )r6   fnamemoduler@   s     r   	decoratormove_to.<locals>.decorator   s4    <MMEE!% //r   r   )rF   r@   rG   s   `` r   move_torI      s     r   c                    [        5       R                  U 5      nUb(  U[        R                  U '   [        R                  Ul        U[        R                  L a  [        [        U 44nO[        [        X44n[        US[        U5      06$ )aI  
A easier to understand and more compact way of "softly" defining a function.
These two pieces of code are equivalent:

if _dill.OLD3X:
    def _create_class():
        ...
_create_class = register_shim('_create_class', types.new_class)

if _dill.OLD3X:
    @move_to(_dill)
    def _create_class():
        ...
_create_class = Getattr(_dill, '_create_class', types.new_class)

Intuitively, it creates a function or object in the versions of dill/python
that require special reimplementations, and use a core library or default
implementation if that function or object does not exist.
r   )globalsr	   _dillrD   r*   r+   r<   r=   r>   r   r?   )r@   rA   r6   r   s       r   register_shimrM      ss    ( 9==D#t..'$$$udm,	ud45	9<(7*;<<r   _CELL_EMPTY_setattr_delattrr   )r.   inspectsysmodulesrL   r
   r   r   Sentinel__NO_DEFAULTr<   r=   rI   rM   rN   setattrrO   delattrrP   r   r   r   <module>rX      s   *X  
L!'!V '!R$f $ ~~23". =: " 	=H M40W-W-r   