U
    du                  <   @   sb  U d Z ddlZddlZddlZddlZddlZddlZddlZddlm	Z
 ddlmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/ dd	l0m1Z1m2Z2m3Z3m4Z4 dd
l5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZAmBZBmCZC ddlDmEZE ddlFmGZG ddlHmIZI ddlJmKZK ddlLmMZM ddlNmOZOmPZP ddlQmRZR erddlSmTZTmUZU zddlVmWZW dZXW n eYk
r   dZXY nX dddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTg<ZZdUZ[dVZ\dWZ]dXZ^dYZ_dZZ`d[Zad\Zbd]Zcd^Zdd_Zed`ZfdaZgdbZhdcZiddZjdeZkdfZldgZmdhZndiZoepdjjqZrepdkjsZtepdkjqZuepdljqZvepdmjqZwepdnjqZxeeeeyf dodpdIZze{e|edqdrdsZ}eee{eeeee{e{f dtdudvZ~eee{e2ee|e{f dwdxdyZeee{eeeeee{f dtdzd{Zeee{e{e2eee|e{f d|d}d~Zee{e{ee{e{f dddZeee{e{e2eeee{f d|ddZeee{e{e2e|eee{f dddZeee{e{e2eee"e,ejf e{f dddZeee{eeeeeKe{f dtddZeee{eeeeee{f dtddZeee{e{e2eee"eje8f e{f dddZeee{e{e2e|ee/e{f dddZeee{e{e2e|ee/e{f dddZeee{ee2eeeMe{f dddZeee{e{e2e|ee=e{f dddZeee{eeeeeRe{f dtddZeee{eeeeeEe{f dtddZeee{eeeee?e{f dtddZee[eee\eee]eee^eee_eee`dd eeaeeebeeeceeeddd eeeeeefeeegeeeheeeieeeje~eekeeeleeemeeendd eeodd iZee{edeee{f f f ed< eXrd,eee{e{e2eedddZnd-eee{e{e2eedddZe!deeef dZd.ee{e{e2eeedddZd/eee{e{e2eeedddZee2edddZeXr0eWjZepdjjZepdkjZepdljZepdmjZepdnjZedd edD Zeeddf dddJZe"e|ef edddZe"e|ef edddZe|edddZeeeeedddÄZeeeeedddńZeeee2edƜddȄZee=ee2edƜddʄZeee ee2edƜdd̄Zee|eeeddd΄Zee,eeedddЄZeejee2edќddӄZeeKeeedԜddքZeeeeeddd؄ZeejeeedddڄZee8eeeddd܄Zeeeeedݜdd߄ZeeMeeedddZee/ee2edќddZee{eeedddZeeeeedddZeeeeedddZee?eeedddZeeeeedݜddZeeeeedݜddZeeeeejee8eeeeee{eeee|eeeedeejee,eeEee/ee=eeGeeIeeKeeMeeOeePeeRee?eejeiZeeeeeeeeed	Zedd eD ZÐd0eeee2eeedddZeeee2edddZŐd1eee2eedddZeX
reWjZedZde1fee|ef ee2edddKZe$d2ddee|ef dd dLZe$ddddddLZʐd3dde"ee|ef df dddLZddddddZeXrbeWjZe$d4ddd	dd
dMZe$ddddddMZ̐d5ddddddMZeeeeeef dddZeyedddZeXreWjZeddddZee2eee dddZe$d6edddddNZe$edddddNZѐd7edddddNZe$d8e"eef ddd d!dOZe$e"eef ddd d"dOZҐd9e"eef ddd d#dOZeed$d%dPZG d&dQ dQeZedd'dRZddd(d)Zeed*r^eje֐d+ dS (:  a  BSON (Binary JSON) encoding and decoding.

The mapping from Python types to BSON types is as follows:

=======================================  =============  ===================
Python Type                              BSON Type      Supported Direction
=======================================  =============  ===================
None                                     null           both
bool                                     boolean        both
int [#int]_                              int32 / int64  py -> bson
`bson.int64.Int64`                       int64          both
float                                    number (real)  both
str                                      string         both
list                                     array          both
dict / `SON`                             object         both
datetime.datetime [#dt]_ [#dt2]_         date           both
`bson.regex.Regex`                       regex          both
compiled re [#re]_                       regex          py -> bson
`bson.binary.Binary`                     binary         both
`bson.objectid.ObjectId`                 oid            both
`bson.dbref.DBRef`                       dbref          both
None                                     undefined      bson -> py
`bson.code.Code`                         code           both
str                                      symbol         bson -> py
bytes [#bytes]_                          binary         both
=======================================  =============  ===================

.. [#int] A Python int will be saved as a BSON int32 or BSON int64 depending
   on its size. A BSON int32 will always decode to a Python int. A BSON
   int64 will always decode to a :class:`~bson.int64.Int64`.
.. [#dt] datetime.datetime instances will be rounded to the nearest
   millisecond when saved
.. [#dt2] all datetime.datetime instances are treated as *naive*. clients
   should always use UTC.
.. [#re] :class:`~bson.regex.Regex` instances and regular expression
   objects from ``re.compile()`` are both saved as BSON regular expressions.
   BSON regular expressions are decoded as :class:`~bson.regex.Regex`
   instances.
.. [#bytes] The bytes type is encoded as BSON binary with
   subtype 0. It will be decoded back to bytes.
    N)utf_8_decode)utf_8_encode)abc)IOTYPE_CHECKINGAnyBinaryIOCallableDict	GeneratorIteratorListMappingMutableMappingNoReturnOptionalSequenceTupleTypeTypeVarUnioncastoverload)ALL_UUID_SUBTYPESCSHARP_LEGACYJAVA_LEGACYOLD_UUID_SUBTYPESTANDARDUUID_SUBTYPEBinaryUuidRepresentation)Code)DEFAULT_CODEC_OPTIONSCodecOptionsDatetimeConversion_raw_document_class)EPOCH_AWAREEPOCH_NAIVE
DatetimeMS_datetime_to_millis_millis_to_datetimeutc)DBRef)
Decimal128)InvalidBSONInvalidDocumentInvalidStringData)Int64MaxKeyMinKeyObjectId)Regex)RE_TYPESON)	Timestamp)_DocumentType_ReadableBuffer)_cbsonTFr   r   r   r   r   r   r   r    r!   r"   r#   r,   r-   r.   r/   r0   r1   r3   r5   r7   r8   r9   r:   r;   r+   r&   r'   BSONNUMBSONSTRBSONOBJBSONARRBSONBINBSONUNDBSONOIDBSONBOOBSONDATBSONNULBSONRGXBSONREFBSONCODBSONSYMBSONCWSBSONINTBSONTIMBSONLONBSONDECBSONMINBSONMAXget_data_and_viewgen_list_nameencodedecode
decode_alldecode_iterdecode_file_iteris_validBSONhas_cr$   r(                              	   
                                 z<dz<iz<iBz<qz<II)datareturnc                 C   s.   t | ttfr| t| fS t| }| |fS N)
isinstancebytes	bytearray
memoryviewtobytes)rs   view r|   1/tmp/pip-unpacked-wheel-oblwsawz/bson/__init__.pyrT      s    )element_typeelement_namert   c                 C   s   t dt|  |dS )zUnknown type helper.z\Detected unknown BSON type {!r} for fieldname '{}'. Are you using the latest driver version?N)r.   formatchrrV   )r~   r   r|   r|   r}   _raise_unknown_type   s    
 r   )rs   r{   positiondummy0dummy1dummy2rt   c                 C   s   t | |d |d fS )z"Decode a BSON int32 to python int.r      )_UNPACK_INT_FROMrs   r{   r   r   r   r   r|   r|   r}   _get_int   s    r   )rs   r{   r   optsrt   c                 C   s.   |  d|}t||| |jdd |d fS )z'Decode a BSON 'C' string to python str.    Tr      )index_utf_8_decodeunicode_decode_error_handler)rs   r{   r   r   endr|   r|   r}   _get_c_string   s    r   c                 C   s   t | |d |d fS )z%Decode a BSON double to python float.r      )_UNPACK_FLOAT_FROMr   r|   r|   r}   
_get_float  s    r   )rs   r{   r   obj_endr   dummyrt   c                 C   st   t | |d }|d7 }|dk s*|| |k r2td|| d }| | dkrRtdt||| |jdd |d fS )z#Decode a BSON string to python str.r   r   r   zinvalid string lengthzinvalid end of stringT)r   r.   r   r   )rs   r{   r   r   r   r   lengthr   r|   r|   r}   _get_string	  s    r   )rs   r   r   rt   c              
   C   s   zt | |d }W n0 tjk
rB } ztt|W 5 d}~X Y nX || d }| | dkrdtd||krttd|dkr||krtd||fS )z+Validate and return a BSON document's size.r   Nr   bad eoozinvalid object length)r   structerrorr.   str)rs   r   r   obj_sizeexcr   r|   r|   r}   _get_object_size  s    r   c           	      C   s   t | ||\}}t|jr:|| ||d  ||| fS t| ||d ||}||7 }t|dtrd|krt|dttdfrt|	d|	dd|	dd||fS ||fS )zEDecode a BSON subdocument to opts.document_class or bson.dbref.DBRef.r   r   z$refz$idz$dbN)
r   r%   document_class_elements_to_dictrv   getr   typer,   pop)	rs   r{   r   r   r   r   r   r   objr|   r|   r}   _get_object(  s    
 (r   )rs   r{   r   r   r   r   rt   c                 C   s  t | |d }|| d }| | dkr.td|d7 }|d8 }g }|j}	| j}
t}|jj}||k r| | }|
d|d }z|| | |||||\}}W n tk
r   t|| Y nX |r|	t
|}|dk	r||}|	| qZ||d krtd||d fS )z#Decode a BSON array to python list.r   r   r   r   r   Nzbad array length)r   r.   appendr   _ELEMENT_GETTERtype_registry_decoder_mapKeyErrorr   r   r   )rs   r{   r   r   r   r   sizer   resultr   r   getterZdecoder_mapr~   valuecustom_decoderr|   r|   r}   
_get_array=  sB         
r   )rs   r{   r   r   r   r   rt   c                 C   s   t | |\}}|d7 }|dkrLt| |d }|d7 }||d krHtd|}|| }	|dk sd|	|krltd|tkr|j}
t| ||	 |}|
tjks|tkr|
t	ks|t
kr|
t	kr||	fS ||
|	fS |dkr| ||	 }nt| ||	 |}||	fS )z:Decode a BSON binary to bson.binary.Binary or python UUID.      r   r   z,invalid binary (st 2) - lengths don't match!zbad binary object length)_UNPACK_LENGTH_SUBTYPE_FROMr   r.   r   uuid_representationr   r    ZUNSPECIFIEDr   r   r   Zas_uuid)rs   r{   r   r   r   r   r   subtypeZlength2r   Zuuid_repZbinary_valuer   r|   r|   r}   _get_binaryg  s<    r   c                 C   s   |d }t | || |fS )z1Decode a BSON ObjectId to bson.objectid.ObjectId.   r6   rs   r{   r   r   r   r   r   r|   r|   r}   _get_oid  s    r   c                 C   sD   |d }| || }|dkr$d|fS |dkr4d|fS t d| dS )z.Decode a BSON true/false to python True/False.r   r   Fr^   Tzinvalid boolean value: %rN)r.   )rs   r{   r   r   r   r   r   Zboolean_byter|   r|   r}   _get_boolean  s    r   )rs   r{   r   r   r   r   rt   c                 C   s   t t| |d ||d fS )z3Decode a BSON datetime to python datetime.datetime.r   r   )r*   _UNPACK_LONG_FROM)rs   r{   r   r   r   r   r|   r|   r}   	_get_date  s    r   c                 C   s"   t | |||||\}}t||fS )z%Decode a BSON code to bson.code.Code.)r   r!   )rs   r{   r   r   r   r   coder|   r|   r}   	_get_code  s    r   c           	      C   s`   |t | |d  }t| ||d |||\}}t| |||||\}}||krRtdt|||fS )z-Decode a BSON code_w_scope to bson.code.Code.r   r   z+scope outside of javascript code boundaries)r   r   r   r.   r!   )	rs   r{   r   r   r   r   Zcode_endr   scoper|   r|   r}   _get_code_w_scope  s    r   c           	      C   s6   t | |||\}}t | |||\}}t||}||fS )zCDecode a BSON regex to bson.regex.Regex or a python pattern object.)r   r8   )	rs   r{   r   r   r   r   patternZ
bson_flagsZbson_rer|   r|   r}   
_get_regex  s    
r   c                 C   s:   t | |||||\}}t| |||||\}}t|||fS )z7Decode (deprecated) BSON DBPointer to bson.dbref.DBRef.)r   r   r,   )rs   r{   r   r   r   r   
collectionoidr|   r|   r}   _get_ref  s    r   c                 C   s    t | |\}}t|||d fS )z4Decode a BSON timestamp to bson.timestamp.Timestamp.r   )_UNPACK_TIMESTAMP_FROMr;   )rs   r{   r   r   r   r   inc	timestampr|   r|   r}   _get_timestamp  s    r   c                 C   s   t t| |d |d fS )z(Decode a BSON int64 to bson.int64.Int64.r   r   )r1   r   r   r|   r|   r}   
_get_int64  s    r   c                 C   s   |d }t | || |fS )z7Decode a BSON decimal128 to bson.decimal128.Decimal128.   )r-   Zfrom_bidr   r|   r|   r}   _get_decimal128  s    r   c                 C   s   d |fS ru   r|   uvwxyzr|   r|   r}   <lambda>      r   c                 C   s   d |fS ru   r|   r   r|   r|   r}   r     r   c                 C   s
   t  |fS ru   r4   r   r|   r|   r}   r      r   c                 C   s
   t  |fS ru   r2   r   r|   r|   r}   r     r   .r   )rs   r{   r   r   r   	raw_arrayrt   c                 C   s   t | ||||S ru   )r>   _element_to_dict)rs   r{   r   r   r   r   r|   r|   r}   r     s    r   c                 C   s   | | }|d7 }t | |||\}}|r`|ttkr`t| |t| \}}	||||	d  |	d fS zt| | |||||\}
}W n tk
r   t|| Y nX |jj	r|jj	
t|
}|dk	r||
}
||
|fS )z Decode a single key, value pair.r   N)r   ordrB   r   lenr   r   r   r   r   r   r   )rs   r{   r   r   r   r   r~   r   _r   r   r   r|   r|   r}   r     s,    	     _T)bound)rs   r   r   r   r   r   rt   c              	   C   s"   t | \} }t| ||||||dS )Nr   )rT   r   )rs   r   r   r   r   r   r{   r|   r|   r}   _raw_to_dict4  s    r   )rs   r{   r   r   r   r   r   rt   c           
      C   sX   |dkr|  }|d }||k rDt| |||||d\}}	}|	||< q||krTtd|S )z#Decode a BSON document into result.Nr   r   bad object or element length)r   r   r.   )
rs   r{   r   r   r   r   r   r   keyr   r|   r|   r}   r   ;  s     
     
r   )rs   r   rt   c                 C   s   t | \} }z>t|jr&|| |W S t| dt| \}}t| |d||W S  tk
r`    Y n4 tk
r   t	 \}}}tt
||Y nX dS )z'Decode a BSON string to document_class.r   r   N)rT   r%   r   r   r   r   r.   	Exceptionsysexc_infor   with_traceback)rs   r   r{   r   r   	exc_valueexc_tbr|   r|   r}   _bson_to_dictR  s    
r   c                 c   s    | ]}t |d  dV  qdS ) utf8N)r   rV   ).0ir|   r|   r}   	<genexpr>k  s     r     )rt   c                  c   s2   t E dH  td} tt| d dV  qdS )zGenerate "keys" for encoded lists in the sequence
    b"0 ", b"1 ", b"2 ", ...

    The first 1000 keys are returned from a pre-built cache. All
    subsequent keys are generated on the fly.
    Nr   r   r   )_LIST_NAMES	itertoolscountr   nextrV   )counterr|   r|   r}   rU   n  s    

)stringrt   c                 C   sx   t | trTd| krtdzt| dd | d W S  tk
rP   td|  Y qtX n d| krdtdt| d d S dS )z8Make a 'C' string, checking for embedded NUL characters.r   z;BSON keys / regex patterns must not contain a NUL characterNT,strings in documents must be valid UTF-8: %rr   r   )rv   rw   r/   r   UnicodeErrorr0   _utf_8_encoder   r|   r|   r}   _make_c_string_check|  s    

r   c                 C   sX   t | trDzt| dd | d W S  tk
r@   td|  Y qTX nt| d d S dS )zMake a 'C' string.NTr   r   r   )rv   rw   r   r   r0   r   r   r|   r|   r}   _make_c_string  s    

r   c                 C   s    d| krt dt| d d S )z*Make a 'C' string suitable for a BSON key.r   z*BSON keys must not contain a NUL characterr   r   )r/   r   r   r|   r|   r}   
_make_name  s    r   )namer   r   r   rt   c                 C   s   d|  t | S )zEncode a float.r^   )_PACK_FLOATr   r   r   r   r|   r|   r}   _encode_float  s    r   c                 C   s   d|  t t| d | S )zEncode a python bytes.rb   r   )	_PACK_INTr   r   r|   r|   r}   _encode_bytes  s    r  )r   r   
check_keysr   rt   c                    sT   t |rd|  |j S d fdd| D }d|  tt|d  | d S )zEncode a mapping type.r`   r   c                    s   g | ]\}}t || qS r|   )_element_to_bson)r   r   valr  r   r|   r}   
<listcomp>  s     z#_encode_mapping.<locals>.<listcomp>r   r   )r%   rawjoinitemsr  r   r   r   r  r   rs   r|   r  r}   _encode_mapping  s    r  c                 C   s   t d|  d }t|d }|td|j||7 }|td|j||7 }|jdk	rb|td|j||7 }|j D ]\}}|t||||7 }ql|d7 }t	t|| |||d < t
|S )	zEncode bson.dbref.DBRef.r`   s       r   s   $ref s   $id Ns   $db r   )rx   r   _name_value_to_bsonr   idZdatabaseZ_DBRef__kwargsr
  r  r  rw   )r   r   r  r   bufbeginr   r  r|   r|   r}   _encode_dbref  s    
r  c                    sB   t  d fdd|D }d|  tt|d  | d S )zEncode a list/tuple.r   c                    s   g | ]}t t| qS r|   )r  r   )r   itemr  lnamer   r|   r}   r    s     z _encode_list.<locals>.<listcomp>ra   r   r   )rU   r	  r  r   r  r|   r  r}   _encode_list  s    r  c                 C   s,   t |d }d|  tt|d  | d S )zEncode a python str.r   r_   r   r   )r   r  r   )r   r   r   r   Zbvaluer|   r|   r}   _encode_text  s    r  c                 C   s8   |j }|dkrtt|| }d|  tt|| | S )zEncode bson.binary.Binary.r   rb   )r   r  r   _PACK_LENGTH_SUBTYPE)r   r   r   r   r   r|   r|   r}   _encode_binary  s    r  )r   r   r   r   rt   c                 C   s"   |j }tj||d}t| |||S )zEncode uuid.UUID.)r   )r   r   Z	from_uuidr  )r   r   r   r   r   Zbinvalr|   r|   r}   _encode_uuid  s    r  )r   r   r   r   rt   c                 C   s   d|  |j  S )zEncode bson.objectid.ObjectId.rd   )binary)r   r   r   r   r|   r|   r}   _encode_objectid  s    r  c                 C   s   d|  |rdpd S )z%Encode a python boolean (True/False).re   r^   r   r|   r   r|   r|   r}   _encode_bool  s    r  c                 C   s   t |}d|  t| S zEncode datetime.datetime.rf   )r)   
_PACK_LONGr   r   r   r   Zmillisr|   r|   r}   _encode_datetime  s    r   c                 C   s   t |}d|  t| S r  )intr  r  r|   r|   r}   _encode_datetime_ms  s    r"  )r   r   r   r   rt   c                 C   s   d|  S )zEncode python None.rg   r|   r   r   r   r   r|   r|   r}   _encode_none  s    r$  c                 C   s   |j }|tjkr&d|  t|j d S |dkrDd|  t|j d S d}|tj@ rZ|d7 }|tj@ rl|d7 }|tj@ r~|d7 }|tj@ r|d	7 }|tj@ r|d
7 }|tj	@ r|d7 }|d7 }d|  t|j | S dS )z*Encode a python regex or bson.regex.Regex.rh   s   u r   r   r      i   l   m   s   u   xN)
flagsreUNICODEr   r   
IGNORECASELOCALE	MULTILINEDOTALLVERBOSE)r   r   r   r   r+  Zsflagsr|   r|   r}   _encode_regex  s(    






r3  c                 C   sn   t |}t|}|jdkr.d|  t| | S t|jd|d}td| t| }d|  | t| | | S )zEncode bson.code.Code.Nrj   Fr   rl   )r   r   r   r  _dict_to_bson)r   r   r   r   ZcstringZcstrlenr   Zfull_lengthr|   r|   r}   _encode_code  s    
r5  c                 C   s^   d|  krdkr(n nd|  t | S zd|  t| W S  tjk
rX   tdY nX dS )zEncode a python int.i   irm   ro   &BSON can only handle up to 8-byte intsN)r  r  r   r   OverflowErrorr   r|   r|   r}   _encode_int  s    r8  c                 C   s   d|  t |j|j S )z Encode bson.timestamp.Timestamp.rn   )_PACK_TIMESTAMPr   timer   r|   r|   r}   _encode_timestamp*  s    r;  c                 C   s6   zd|  t | W S  tjk
r0   tdY nX dS )zEncode a bson.int64.Int64.ro   r6  N)r  r   r   r7  r   r|   r|   r}   _encode_long/  s    r<  c                 C   s   d|  |j  S )z"Encode bson.decimal128.Decimal128.rp   )bidr   r|   r|   r}   _encode_decimal1287  s    r>  c                 C   s   d|  S )zEncode bson.min_key.MinKey.rq   r|   r#  r|   r|   r}   _encode_minkey<  s    r?  c                 C   s   d|  S )zEncode bson.max_key.MaxKey.rr   r|   r#  r|   r|   r}   _encode_maxkeyA  s    r@  )	r                  d         c                 c   s   | ]
}|V  qd S ru   r|   )r   tr|   r|   r}   r   v  s     )r   r   r  r   in_custom_callin_fallback_callrt   c                 C   sf  d}zt t| | |||W S  tk
r0   Y n$ tk
rR   t|tsJ d}Y nX t|dd}t|tr|tkrt| }|t t|< || |||S |s|jj	r|jj	
t|}	|	dk	rt| |	|||ddS tD ]8}
|st||
rt |
 }|t t|< || |||  S q|jj}|s<|dk	r<t| ||||ddS |rJtdtd|d	t|dS )
z!Encode a single name, value pair.FTZ_type_markerN)rJ  )rK  r6  zcannot encode object: z, of type: )	_ENCODERSr   r   r7  rv   r!  getattr_MARKERSr   Z_encoder_mapr   r  _BUILT_IN_TYPESZ_fallback_encoderr/   )r   r   r  r   rJ  rK  Zwas_integer_overflowmarkerfuncZcustom_encoderbaseZfallback_encoderr|   r|   r}   r  y  sR    


        r  )r   r   r  r   rt   c                 C   sd   t | tstd| |rN| dr6td| dd| krNtd| dt| }t||||S )z Encode a single key, value pair.z.documents must have only string keys, key was $zkey z must not start with '$'.z must not contain '.')rv   r   r/   
startswithr   r  )r   r   r  r   r   r|   r|   r}   r    s    

r  )docr  r   	top_levelrt   c                 C   s   t | rtt| jS z^g }|r>d| kr>|td| d || |  D ](\}}|rZ|dkrF|t|||| qFW n" tk
r   t	d| Y nX d
|}tt|d | d S )zEncode a document to BSON.Z_ids   _id z)encoder expected a mapping type but got: r   r   r   )r%   r   rw   r  r   r  r
  r  AttributeError	TypeErrorr	  r  r   )rV  r  r   rW  elementsr   r   encodedr|   r|   r}   r4    s    
r4  z1codec_options must be an instance of CodecOptions)documentr  codec_optionsrt   c                 C   s   t |tstt| ||S )a  Encode a document to BSON.

    A document can be any mapping type (like :class:`dict`).

    Raises :class:`TypeError` if `document` is not a mapping type,
    or contains keys that are not instances of :class:`str`. Raises
    :class:`~bson.errors.InvalidDocument` if `document` cannot be
    converted to :class:`BSON`.

    :Parameters:
      - `document`: mapping type representing a document
      - `check_keys` (optional): check if keys start with '$' or
        contain '.', raising :class:`~bson.errors.InvalidDocument` in
        either case
      - `codec_options` (optional): An instance of
        :class:`~bson.codec_options.CodecOptions`.

    .. versionadded:: 3.9
    )rv   r#   _CODEC_OPTIONS_TYPE_ERRORr4  )r\  r  r]  r|   r|   r}   rV     s    
r=   )rs   r]  rt   c                 C   s   d S ru   r|   rs   r]  r|   r|   r}   rW     s    CodecOptions[_DocumentType]r<   c                 C   s   d S ru   r|   r_  r|   r|   r}   rW     s    z%Optional[CodecOptions[_DocumentType]]c                 C   s    |pt }t|tstt| |S )a  Decode BSON to a document.

    By default, returns a BSON document represented as a Python
    :class:`dict`. To use a different :class:`MutableMapping` class,
    configure a :class:`~bson.codec_options.CodecOptions`::

        >>> import collections  # From Python standard library.
        >>> import bson
        >>> from bson.codec_options import CodecOptions
        >>> data = bson.encode({'a': 1})
        >>> decoded_doc = bson.decode(data)
        <type 'dict'>
        >>> options = CodecOptions(document_class=collections.OrderedDict)
        >>> decoded_doc = bson.decode(data, codec_options=options)
        >>> type(decoded_doc)
        <class 'collections.OrderedDict'>

    :Parameters:
      - `data`: the BSON to decode. Any bytes-like object that implements
        the buffer protocol.
      - `codec_options` (optional): An instance of
        :class:`~bson.codec_options.CodecOptions`.

    .. versionadded:: 3.9
    )r"   rv   r#   r^  r   )rs   r]  r   r|   r|   r}   rW     s    
zList[_DocumentType]c                 C   s  t | \} }t| }g }d}|d }t|j}z||k rt| |d }|| |k rZtd|| d }	| |	 dkrztd|r||| ||	d  | n|t| ||d |	| ||7 }q0|W S  tk
r    Y n6 tk
r   t	
 \}
}}tt||Y nX dS )z)Decode a BSON data to multiple documents.r   r   zinvalid object sizer   r   N)rT   r   r%   r   r   r.   r   r   r   r   r   r   r   )rs   r   r{   Zdata_lenZdocsr   r   Zuse_rawr   r   r   r   r   r|   r|   r}   _decode_all1  s0    
 
ra  zList[Dict[str, Any]]c                 C   s   d S ru   r|   r_  r|   r|   r}   rX   U  s    c                 C   s   d S ru   r|   r_  r|   r|   r}   rX   Z  s    z0Union[List[Dict[str, Any]], List[_DocumentType]]c                 C   s*   |dkrt | tS t|ts tt | |S )aE  Decode BSON data to multiple documents.

    `data` must be a bytes-like object implementing the buffer protocol that
    provides concatenated, valid, BSON-encoded documents.

    :Parameters:
      - `data`: BSON data
      - `codec_options` (optional): An instance of
        :class:`~bson.codec_options.CodecOptions`.

    .. versionchanged:: 3.9
       Supports bytes-like objects that implement the buffer protocol.

    .. versionchanged:: 3.0
       Removed `compile_re` option: PyMongo now always represents BSON regular
       expressions as :class:`~bson.regex.Regex` objects. Use
       :meth:`~bson.regex.Regex.try_compile` to attempt to convert from a
       BSON regular expression to a Python regular expression object.

       Replaced `as_class`, `tz_aware`, and `uuid_subtype` options with
       `codec_options`.
    N)ra  r"   rv   r#   r^  r_  r|   r|   r}   rX   a  s
    

)rawdocfieldsr]  rt   c                 C   sv   t |jri }n| }|  D ]P\}}||krh|| dkrRt| j|| ||< qpt||| |||< q |||< q |S )Nr   )r%   r   r
  r   r  _decode_selective)rb  rc  r]  rV  r   r   r|   r|   r}   rd    s    

rd  )r{   rt   c                 C   s   d}t | |t| \}}|d7 }g }|j}||d k r| | dkrL|d7 }q6|d7 }t | ||\}}|| |||   ||7 }q*||krtdd|S )Nr   r   r   r   r   )r   r   r   r.   r	  )r{   r   r   r   buffersr   r   r|   r|   r}   _array_of_documents_to_buffer  s    

rf  )r\  rt   c                 C   sP   |  d}|sdS dD ]4}| |}|s*qt|}|rB|g||< qg ||< qdS )z=Convert raw array of documents to a stream of BSON documents.cursorN)Z
firstBatchZ	nextBatch)r   rf  )r\  rg  r   batchrs   r|   r|   r}   &_convert_raw_document_lists_to_streams  s    

ri  )rs   r]  rc  rt   c                 C   sZ   |j jst| |S |s(t| |jddS ddlm} |j|dd}t| |}t|||gS )a  Decode BSON data to a single document while using user-provided
    custom decoding logic.

    `data` must be a string representing a valid, BSON-encoded document.

    :Parameters:
      - `data`: BSON data
      - `codec_options`: An instance of
        :class:`~bson.codec_options.CodecOptions` with user-specified type
        decoders. If no decoders are found, this method is the same as
        ``decode_all``.
      - `fields`: Map of document namespaces where data that needs
        to be custom decoded lives or None. For example, to custom decode a
        list of objects in 'field1.subfield1', the specified value should be
        ``{'field1': {'subfield1': 1}}``. If ``fields``  is an empty map or
        None, this method is the same as ``decode_all``.

    :Returns:
      - `document_list`: Single-member list containing the decoded document.

    .. versionadded:: 3.8
    N)r   r   )RawBSONDocument)r   r   )r   r   rX   Zwith_optionsZbson.raw_bsonrj  r   rd  )rs   r]  rc  rj  Zinternal_codec_options_docr|   r|   r}   _decode_all_selective  s     
 
rl  zIterator[Dict[str, Any]]c                 C   s   d S ru   r|   r_  r|   r|   r}   rY     s    zIterator[_DocumentType]c                 C   s   d S ru   r|   r_  r|   r|   r}   rY     s    z8Union[Iterator[Dict[str, Any]], Iterator[_DocumentType]]c                 c   sf   |pt }t|tstd}t| d }||k rbt| |d }| |||  }||7 }t||V  q&dS )a  Decode BSON data to multiple documents as a generator.

    Works similarly to the decode_all function, but yields one document at a
    time.

    `data` must be a string of concatenated, valid, BSON-encoded
    documents.

    :Parameters:
      - `data`: BSON data
      - `codec_options` (optional): An instance of
        :class:`~bson.codec_options.CodecOptions`.

    .. versionchanged:: 3.0
       Replaced `as_class`, `tz_aware`, and `uuid_subtype` options with
       `codec_options`.

    .. versionadded:: 2.8
    r   r   N)r"   rv   r#   r^  r   r   r   )rs   r]  r   r   r   r   rZ  r|   r|   r}   rY     s    
)file_objr]  rt   c                 C   s   d S ru   r|   rm  r]  r|   r|   r}   rZ     s    c                 C   s   d S ru   r|   rn  r|   r|   r}   rZ   "  s    c                 c   sf   |pt }| d}|sqbnt|dkr.tdt|dd d }|| td| }t||V  qdS )a)  Decode bson data from a file to multiple documents as a generator.

    Works similarly to the decode_all function, but reads from the file object
    in chunks and parses bson in chunks, yielding one document at a time.

    :Parameters:
      - `file_obj`: A file object containing BSON data.
      - `codec_options` (optional): An instance of
        :class:`~bson.codec_options.CodecOptions`.

    .. versionchanged:: 3.0
       Replaced `as_class`, `tz_aware`, and `uuid_subtype` options with
       `codec_options`.

    .. versionadded:: 2.8
    r   zcut off in middle of objsizer   N)r"   readr   r.   r   maxr   )rm  r]  r   Z	size_datar   rZ  r|   r|   r}   rZ   )  s    
)bsonrt   c                 C   s>   t | tstdzt| t W dS  tk
r8   Y dS X dS )a  Check that the given string represents valid :class:`BSON` data.

    Raises :class:`TypeError` if `bson` is not an instance of
    :class:`bytes`. Returns ``True``
    if `bson` is valid :class:`BSON`, ``False`` otherwise.

    :Parameters:
      - `bson`: the data to be validated
    z4BSON data must be an instance of a subclass of bytesTFN)rv   rw   rY  r   r"   r   )rq  r|   r|   r}   r[   I  s    


c                   @   sP   e Zd ZdZedefed  eee	f e
ed dddZefdddd	d
ZdS )r\   zBSON (Binary JSON) data.

    .. warning:: Using this class to encode and decode BSON adds a performance
       cost. For better performance use the module level functions
       :func:`encode` and :func:`decode` instead.
    F)clsr\  r  r]  rt   c                 C   s   | t |||S )a@  Encode a document to a new :class:`BSON` instance.

        A document can be any mapping type (like :class:`dict`).

        Raises :class:`TypeError` if `document` is not a mapping type,
        or contains keys that are not instances of
        :class:`str'. Raises :class:`~bson.errors.InvalidDocument`
        if `document` cannot be converted to :class:`BSON`.

        :Parameters:
          - `document`: mapping type representing a document
          - `check_keys` (optional): check if keys start with '$' or
            contain '.', raising :class:`~bson.errors.InvalidDocument` in
            either case
          - `codec_options` (optional): An instance of
            :class:`~bson.codec_options.CodecOptions`.

        .. versionchanged:: 3.0
           Replaced `uuid_subtype` option with `codec_options`.
        )rV   )rr  r\  r  r]  r|   r|   r}   rV   e  s    zBSON.encoder`  r<   )r]  rt   c                 C   s
   t | |S )a3  Decode this BSON data.

        By default, returns a BSON document represented as a Python
        :class:`dict`. To use a different :class:`MutableMapping` class,
        configure a :class:`~bson.codec_options.CodecOptions`::

            >>> import collections  # From Python standard library.
            >>> import bson
            >>> from bson.codec_options import CodecOptions
            >>> data = bson.BSON.encode({'a': 1})
            >>> decoded_doc = bson.BSON(data).decode()
            <type 'dict'>
            >>> options = CodecOptions(document_class=collections.OrderedDict)
            >>> decoded_doc = bson.BSON(data).decode(codec_options=options)
            >>> type(decoded_doc)
            <class 'collections.OrderedDict'>

        :Parameters:
          - `codec_options` (optional): An instance of
            :class:`~bson.codec_options.CodecOptions`.

        .. versionchanged:: 3.0
           Removed `compile_re` option: PyMongo now always represents BSON
           regular expressions as :class:`~bson.regex.Regex` objects. Use
           :meth:`~bson.regex.Regex.try_compile` to attempt to convert from a
           BSON regular expression to a Python regular expression object.

           Replaced `as_class`, `tz_aware`, and `uuid_subtype` options with
           `codec_options`.
        )rW   )selfr]  r|   r|   r}   rW     s    zBSON.decodeN)__name__
__module____qualname____doc__classmethodr"   r   r   r   r   boolr#   rV   rW   r|   r|   r|   r}   r\   ]  s   
c                   C   s   t S )zIs the C extension installed?)_USE_Cr|   r|   r|   r}   r]     s    c                   C   s   t j rt j  dS )z!Releases the ObjectID lock child.N)r7   Z	_inc_locklockedreleaser|   r|   r|   r}   _after_fork  s    
r}  register_at_fork)after_in_child)F)F)F)NF)FF)T)N)N)N)N)N)N)N)N)rw  datetimer   osr,  r   r   uuidcodecsr   r   r   r   collectionsr   _abctypingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zbson.binaryr   r   r   r   r   r   r   r    Z	bson.coder!   Zbson.codec_optionsr"   r#   r$   r%   Zbson.datetime_msr&   r'   r(   r)   r*   r+   Z
bson.dbrefr,   Zbson.decimal128r-   Zbson.errorsr.   r/   r0   Z
bson.int64r1   Zbson.max_keyr3   Zbson.min_keyr5   Zbson.objectidr7   Z
bson.regexr8   Zbson.sonr9   r:   Zbson.timestampr;   Zbson.typingsr<   r=   rq  r>   rz  ImportError__all__r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   Structunpack_fromr   unpackZ_UNPACK_INTr   r   r   r   ry   rT   r!  r   r   r   r   floatr   r   r   r   r   UUIDr   r   ry  r   r   r   r   r   r   r   r   r   r   r   __annotations__r   r   r   r   r   packr   r  r  r  r9  tupleranger   rw   rU   r   r   r   r   r  r  r  r  r  r  r  r  r  r   r"  r$  r3  r5  r8  r;  r<  r>  r?  r@  dictlistr   rL  rN  rO  r  r  r4  rY  r^  rV   rW   ra  rX   rd  rf  ri  rl  rY   rZ   r[   r\   r]   r}  hasattrr~  r|   r|   r|   r}   <module>   s,  )X(
 
?	     
     
     
     
     
+     &     
	     
          
	     
     
     

     
	     
     
                     $                                D
"  $    ""-  $
 
 
  G