
    \j                        d Z ddlmZ ddlZddlZddlmZmZ dZ G d dej                        Z
 G d d	ej                        Zdd
d	 	 	 	 	 	 	 ddZy)a>  
Helpers for safer and/or more explicit pickle usage in NLTK.

- RestrictedUnpickler: blocks unpickling of *any* globals (classes/functions).
  Intended for loading NLTK data packages where we control the serialization.
- WarningUnpickler: emits a security warning before unpickling (does not make
  unpickling safe).
    )annotationsN)AnyBinaryIOzSecurity warning: loading pickles can execute arbitrary code. Only load pickle files from trusted sources and never from untrusted or unauthenticated locations.c                      e Zd ZdZddZy)RestrictedUnpicklerzW
    Unpickler that prevents any class or function from being used during loading.
    c                :    t        j                  d| d| d      )Nzglobal '.z' is forbidden)pickleUnpicklingError)selfmodulenames      8/root/env/lib/python3.12/site-packages/nltk/picklesec.py
find_classzRestrictedUnpickler.find_class$   s"    $$xxqn%MNN    N)r   strr   r   returnr   )__name__
__module____qualname____doc__r    r   r   r   r      s    Or   r   c                  6     e Zd ZdZddd fdZd fdZ xZS )WarningUnpicklerz6Unpickler that emits PICKLE_WARNING once per instance.Ncontextc               B    t        |   |fi | || _        d| _        y )NF)super__init___context_warned)r   filer   kwargs	__class__s       r   r   zWarningUnpickler.__init__,   s#    ((r   c                    | j                   sK| j                  t        nt         d| j                   d}t        j                  |t
        d       d| _         t        |          S )Nz ()   )
stacklevelT)r!   r    PICKLE_WARNINGwarningswarnRuntimeWarningr   load)r   msgr$   s     r   r-   zWarningUnpickler.load1   sZ    || ==( &'r$--: 
 MM#~!<DLw|~r   )r"   r   r   
str | Noner#   r   )r   r   )r   r   r   r   r   r-   __classcell__)r$   s   @r   r   r   )   s    @@D 
	 	r   r   F)r   
restrictedc               n    |rt        |       j                         S t        | |      j                         S )z
    Convenience wrapper similar to pickle.load(file).

    - If restricted=True, uses RestrictedUnpickler (no warning by default).
    - If restricted=False, uses WarningUnpickler and emits a warning.
    r   )r   r-   r   )r"   r   r1   s      r   pickle_loadr3   =   s1     "4(--//D'27799r   )r"   r   r   r/   r1   boolr   r   )r   
__future__r   r
   r*   typingr   r   r)   	Unpicklerr   r   r3   r   r   r   <module>r8      st    #    $ O&** Ov'' * .2e:
: *:?C::r   