o
    !wÖi›  ã                   @   s|   d Z ddlmZ ddlZddlZddlmZ ddgZG dd„ deƒZ	G d	d
„ d
eƒZ
G dd„ dejƒZG dd„ dejƒZdS )aâ  
JPype Pickle Module
--------------------

This module contains overloaded Pickler and Unpickler classes that operate
on Java classes. Pickling of Java objects is restricted to classes
that implement Serializable.  Mixed pickle files containing both
Java and Python objects are allowed.  Only one copy of each Java object
will appear in the pickle file even it is appears multiple times in the
data structure.

JPicklers and JUnpickler use Java ObjectOutputStream and ObjectInputStream
to serialize objects. All of the usual Java serialization errors may be
thrown.

This is backed by the native cPickler implementation.

Example:

.. code-block:: python

  myobj = jpype.JClass('java.util.ArrayList')()
  myobj.add("test")

  from jpype.pickle import JPickler, JUnpickler
  with open("test.pic", "wb") as fd:
    JPickler(fd).dump(myobj)

  with open("test.pic", "rb") as fd:
    newobj = JUnpickler(fd).load()


Proxies and other JPype specific module resources cannot be pickled currently.

é    )Úabsolute_importN)Údispatch_tableÚJPicklerÚ
JUnpicklerc                   @   s   e Zd Zdd„ ZdS )ÚJUnserializerc                 G   s
   t  d¡‚)Nz#Unpickling Java requires JUnpickler)ÚpickleÚUnpicklingError©ÚselfÚargs© r   úI/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/jpype/pickle.pyÚ__call__E   s   
zJUnserializer.__call__N©Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   D   s    r   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú
_JDispatchzëDispatch for Java classes and objects.

    Python does not have a good way to register a reducer that applies to
    many classes, thus we will substitute the usual dictionary with a
    class that can produce reducers as needed.
    c                 C   s(   t  d¡ƒ | _tƒ | _|| _| j| _d S )Nzorg.jpype.pickle.Encoder)Ú_jpypeÚJClassÚ_encoderr   Ú_builderÚ	_dispatchÚreduceÚ_call)r
   Údispatchr   r   r   Ú__init__Q   s   z_JDispatch.__init__c                 C   s$   t |tjtjfƒs| j |¡S | jS ©N)Ú
issubclassr   r   ÚJObjectr   Úgetr   ©r
   Úclsr   r   r   r    Z   s   z_JDispatch.getc                 C   s"   t |tjtjfƒs| j| S | jS r   )r   r   r   r   r   r   r!   r   r   r   Ú__getitem__`   s   
z_JDispatch.__getitem__c                 C   s   t | j |¡ƒ}| j|ffS r   )Úbytesr   Úpackr   )r
   ÚobjÚbyter   r   r   r   e   s   z_JDispatch.reduceN)r   r   r   Ú__doc__r   r    r#   r   r   r   r   r   r   I   s    	r   c                   @   s   e Zd ZdZdd„ ZdS )r   aŠ  Pickler overloaded to support Java objects

    Parameters:
        file: a file or other writeable object.
        *args: any arguments support by the native pickler.

    Raises:
        java.io.NotSerializableException: if a class is not serializable or
            one of its members
        java.io.InvalidClassException: an error occures in constructing a
            serialization.

    c                 O   s*   t jj| |g|¢R i |¤Ž ttƒ| _d S r   )r   ÚPicklerr   r   r   ©r
   Úfiler   Úkwargsr   r   r   r   y   s   zJPickler.__init__N)r   r   r   r(   r   r   r   r   r   r   j   s    c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   a9  Unpickler overloaded to support Java objects

    Parameters:
        file: a file or other readable object.
        *args: any arguments support by the native unpickler.

    Raises:
        java.lang.ClassNotFoundException: if a serialized class is not
            found by the current classloader.
        java.io.InvalidClassException: if the serialVersionUID for the
            class does not match, usually as a result of a new jar
            version.
        java.io.StreamCorruptedException: if the pickle file has been
            altered or corrupted.

    c                 O   s.   t  d¡ƒ | _tjj| |g|¢R i |¤Ž d S )Nzorg.jpype.pickle.Decoder)r   r   Ú_decoderr   Ú	Unpicklerr   r*   r   r   r   r   ’   s    zJUnpickler.__init__c                    s6   |dkr| j ‰ G ‡ fdd„dtƒ}|S tj | ||¡S )z™Specialization for Java classes.

        We just need to substitute the stub class for a real
        one which points to our decoder instance.
        r   c                       s   e Zd Z‡ fdd„ZdS )z,JUnpickler.find_class.<locals>.JUnserializerc                    s   ˆ   |d ¡S )Nr   )Úunpackr	   ©Údecoderr   r   r       s   z5JUnpickler.find_class.<locals>.JUnserializer.__call__Nr   r   r0   r   r   r   Ÿ   s    )r-   Úobjectr   r.   Ú
find_class)r
   Úmoduler"   r   r   r0   r   r3   –   s
   zJUnpickler.find_classN)r   r   r   r(   r   r3   r   r   r   r   r   €   s    )r(   Ú
__future__r   r   r   Úcopyregr   Ú__ALL__r2   r   r   r)   r   r.   r   r   r   r   r   Ú<module>   s   #!