
    ph,              	      j   S r SSKJr  SSKrSSKrSSKJr  SSKJrJrJ	r	J
r
Jr  SSKJrJr  \R                  " S5      R                   r\R                  " S5      R$                  rSrS	rS
rSrSrSrSrSrSr\\-   S4r\S4r\\-   S4r\S4r \\-   S4r!\S4r"\\RF                  \\S/ \RH                  \RJ                  \RL                  /SS.r'\RP                  " S0 \'RS                  5       D6r*\\R
                  \+\,\	\-\\-   \-4   4   r. " S S\5      r/ " S S\5      r0SS jr1SS jr2 " S S5      r3g)zHTools for working with the BSON decimal128 type.

.. versionadded:: 3.4
    )annotationsNDecimal)AnySequenceTupleTypeUnion)TypeDecoderTypeEncoderz<Ql            i   i   i"   l          @ l          ` l          p l               )precroundingEminEmaxcapitalsflagstrapsclampc                  6    \ rS rSrSr\SS j5       rSS jrSrg)	DecimalEncoder@   zConverts Python :class:`decimal.Decimal` to BSON :class:`Decimal128`.

For example::
    opts = CodecOptions(type_registry=TypeRegistry([DecimalEncoder()]))
    bson.encode({"d": decimal.Decimal('1.0')}, codec_options=opts)

.. versionadded:: 4.15
c                    [         $ Nr   selfs    I/home/james-whalen/.local/lib/python3.13/site-packages/bson/decimal128.pypython_typeDecimalEncoder.python_typeJ   s        c                    [        U5      $ r   
Decimal128r   values     r   transform_pythonDecimalEncoder.transform_pythonN   s    %  r!    N)returnzType[Decimal])r&   r   r*   r$   )	__name__
__module____qualname____firstlineno____doc__propertyr   r'   __static_attributes__r)   r!   r   r   r   @   s       !r!   r   c                  6    \ rS rSrSr\SS j5       rSS jrSrg)	DecimalDecoderR   zConverts BSON :class:`Decimal128` to Python :class:`decimal.Decimal`.

For example::
    opts = CodecOptions(type_registry=TypeRegistry([DecimalDecoder()]))
    bson.decode(data, codec_options=opts)

.. versionadded:: 4.15
c                    [         $ r   r#   r   s    r   	bson_typeDecimalDecoder.bson_type\   s    r!   c                "    UR                  5       $ r   )
to_decimalr%   s     r   transform_bsonDecimalDecoder.transform_bson`   s    !!r!   r)   N)r*   Type[Decimal128])r&   r   r*   decimal.Decimal)	r+   r,   r-   r.   r/   r0   r6   r:   r1   r)   r!   r   r3   r3   R   s       "r!   r3   c                 `    [         R                  5       n / U S'   [        R                  " S0 U D6$ )z}Returns an instance of :class:`decimal.Context` appropriate
for working with IEEE-754 128-bit decimal floating point values.
r   r)   )_CTX_OPTIONScopydecimalContext)optss    r   create_decimal128_contextrD   d   s-     DDM??"T""r!   c           
         [         R                  " [        5       nUR                  U 5      n SSS5        U R	                  5       (       a!  U R                  5       (       a  [        $ [        $ U R                  5       u  p#nU R                  5       (       ai  U(       a  [        S5      eU R                  5       (       a!  U R                  5       (       a  [        $ [        $ U R                  5       (       a  [        $ [        $ [!        SR#                  U Vs/ s H  n[%        U5      PM     sn5      5      nUR'                  5       nSnSn	[)        [+        SU5      5       H  n
USU
-  -  (       d  M  U	SU
-  -  n	M     [)        SU5       H  n
USU
-  -  (       d  M  USU
S-
  -  -  nM     U[,        -   nUS-	  S:X  a  US-  nU[.        -  nXS	-  S
-  -  nOXS-  -  nU(       a	  U[0        -  nX4$ ! , (       d  f       GN= fs  snf )zgConverts a decimal.Decimal to BID (high bits, low bits).

:param value: An instance of decimal.Decimal
Nz'NaN with debug payload is not supported r   r   r   1   l    i?  /   )rA   localcontext_DEC128_CTXcreate_decimalis_infinite	is_signed_NINF_PINFas_tupleis_nan
ValueErroris_snan_NSNAN_PSNAN_NNAN_PNANintjoinstr
bit_lengthrangemin_EXPONENT_BIAS_EXPONENT_MASK_SIGN)r&   ctxsigndigitsexponentdigitsignificandr[   highlowibiased_exponents               r   _decimal_to_128rk   m   s   
 
		k	*c""5) 
+ ))u4u4"^^-D(||~~FGG==??"__..6:F:))u4u4bggv>ves5zv>?@K'')JD
C3r:&'!q&!!16MC ( 2z"!q&!!A!b&M!D # /OrzQn$6)b002%%9Q 
+	* ?s   G)G;)
G8c                      \ rS rSrSrSrSrSS jrSS jr\	SS j5       r
\SS j5       rSS	 jrSS
 jrSS jrSS jrSS jrSS jrSrg)r$      a  BSON Decimal128 type::

  >>> Decimal128(Decimal("0.0005"))
  Decimal128('0.0005')
  >>> Decimal128("0.0005")
  Decimal128('0.0005')
  >>> Decimal128((3474527112516337664, 5))
  Decimal128('0.0005')

:param value: An instance of :class:`decimal.Decimal`, string, or tuple of
    (high bits, low bits) from Binary Integer Decimal (BID) format.

.. note:: :class:`~Decimal128` uses an instance of :class:`decimal.Context`
  configured for IEEE-754 Decimal128 when validating parameters.
  Signals like :class:`decimal.InvalidOperation`, :class:`decimal.Inexact`,
  and :class:`decimal.Overflow` are trapped and raised as exceptions::

    >>> Decimal128(".13.1")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ...
    decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
    >>>
    >>> Decimal128("1E-6177")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ...
    decimal.Inexact: [<class 'decimal.Inexact'>]
    >>>
    >>> Decimal128("1E6145")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ...
    decimal.Overflow: [<class 'decimal.Overflow'>, <class 'decimal.Rounded'>]

  To ensure the result of a calculation can always be stored as BSON
  Decimal128 use the context returned by
  :func:`create_decimal128_context`::

    >>> import decimal
    >>> decimal128_ctx = create_decimal128_context()
    >>> with decimal.localcontext(decimal128_ctx) as ctx:
    ...     Decimal128(ctx.create_decimal(".13.3"))
    ...
    Decimal128('NaN')
    >>>
    >>> with decimal.localcontext(decimal128_ctx) as ctx:
    ...     Decimal128(ctx.create_decimal("1E-6177"))
    ...
    Decimal128('0E-6176')
    >>>
    >>> with decimal.localcontext(DECIMAL128_CTX) as ctx:
    ...     Decimal128(ctx.create_decimal("1E6145"))
    ...
    Decimal128('Infinity')

  To match the behavior of MongoDB's Decimal128 implementation
  str(Decimal(value)) may not match str(Decimal128(value)) for NaN values::

    >>> Decimal128(Decimal('NaN'))
    Decimal128('NaN')
    >>> Decimal128(Decimal('-NaN'))
    Decimal128('NaN')
    >>> Decimal128(Decimal('sNaN'))
    Decimal128('NaN')
    >>> Decimal128(Decimal('-sNaN'))
    Decimal128('NaN')

  However, :meth:`~Decimal128.to_decimal` will return the exact value::

    >>> Decimal128(Decimal('NaN')).to_decimal()
    Decimal('NaN')
    >>> Decimal128(Decimal('-NaN')).to_decimal()
    Decimal('-NaN')
    >>> Decimal128(Decimal('sNaN')).to_decimal()
    Decimal('sNaN')
    >>> Decimal128(Decimal('-sNaN')).to_decimal()
    Decimal('-sNaN')

  Two instances of :class:`Decimal128` compare equal if their Binary
  Integer Decimal encodings are equal::

    >>> Decimal128('NaN') == Decimal128('NaN')
    True
    >>> Decimal128('NaN').bid == Decimal128('NaN').bid
    True

  This differs from :class:`decimal.Decimal` comparisons for NaN::

    >>> Decimal('NaN') == Decimal('NaN')
    False
)__high__low   c                (   [        U[        [        R                  45      (       a  [	        U5      u  U l        U l        g [        U[        [        45      (       a*  [        U5      S:w  a  [        S5      eUu  U l        U l        g [        SU< S35      e)N   zYInvalid size for creation of Decimal128 from list or tuple. Must have exactly 2 elements.zCannot convert z to Decimal128)
isinstancerZ   rA   r   rk   _Decimal128__high_Decimal128__lowlisttuplelenrR   	TypeErrorr%   s     r   __init__Decimal128.__init__   s}    ec7??344&5e&<#DKe}--5zQ   
 ',#DKoeYnEFFr!   c           	        U R                   nU R                  nU[        -  (       a  SOSnU[        -  [        :X  a  [        R
                  " USS45      $ U[        -  [        :X  a  [        R
                  " USS45      $ U[        -  [        :X  a  [        R
                  " USS45      $ U[        -  [        :X  a(  US-  S-	  [        -
  n[        R
                  " US	U45      $ US
-  S-	  [        -
  n[        S5      nSn[        SSS5       H  nX&-  SU-
  S-  -	  XW'   US-  nM     Sn[        SSS5       H  nX-  SU-
  S-  -	  XW'   US-  nM     SnX-  S-	  US'   [        S [        [        R                  US5      5       5       5      n[        R                   " ["        5       n	U	R%                  X8U45      sSSS5        $ ! , (       d  f       g= f)zNReturns an instance of :class:`decimal.Decimal` for this
:class:`Decimal128`.
r   r   r)   NnFl          rH   )r   l          rG                     l          0   c              3  8   #    U  H  n[        U5      v   M     g 7fr   )rX   ).0re   s     r   	<genexpr>(Decimal128.to_decimal.<locals>.<genexpr>1  s     O/Nes5zz/Ns   bigN)rt   ru   r`   _SNANrA   r   _NAN_INFr_   r^   	bytearrayr\   rw   rZ   rX   
from_bytesrI   rJ   rK   )
r   rg   rh   rb   rd   arrmaskri   rc   ra   s
             r   r9   Decimal128.to_decimal  s    {{jjE\q5LU"??D"c?33Tkd"??D"c?33Tkd"??D"c?33>!n4 22r9^KH??D$#9:: 22r9^KHm!r1b!AjrAv!m4CF19D " "q!RAkA!|4CF19D ! "+"$A Os3>>#u3M/NOO!!+.#%%tX&>? /..s   %G
Gc                    [        U[        5      (       d  [        S[        U5       35      e[	        U5      S:w  a  [        S5      eU " [        USS 5      S   [        USS 5      S   45      $ )zCreate an instance of :class:`Decimal128` from Binary Integer
Decimal string.

:param value: 16 byte string (128-bit IEEE 754-2008 decimal floating
    point in Binary Integer Decimal (BID) format).
z(value must be an instance of bytes, not    zvalue must be exactly 16 bytesr   Nr   )rs   bytesry   typerx   rR   
_UNPACK_64)clsr&   s     r   from_bidDecimal128.from_bid6  sp     %''FtE{mTUUu:=>>JuQRy)!,jr.CA.FGHHr!   c                X    [        U R                  5      [        U R                  5      -   $ )z;The Binary Integer Decimal (BID) encoding of this instance.)_PACK_64ru   rt   r   s    r   bidDecimal128.bidD  s!     

#ht{{&;;;r!   c                d    U R                  5       nUR                  5       (       a  g[        U5      $ )NNaN)r9   rQ   rZ   )r   decs     r   __str__Decimal128.__str__I  s%    oo::<<3xr!   c                    SU < S3$ )NzDecimal128('z')r)   r   s    r   __repr__Decimal128.__repr__P  s    dXR((r!   c                "    Uu  U l         U l        g r   rt   ru   r%   s     r   __setstate__Decimal128.__setstate__S  s    "'TZr!   c                2    U R                   U R                  4$ r   r   r   s    r   __getstate__Decimal128.__getstate__V  s    {{DJJ&&r!   c                j    [        U[        5      (       a  U R                  UR                  :H  $ [        $ r   )rs   r$   r   NotImplementedr   others     r   __eq__Decimal128.__eq__Y  s(    eZ((88uyy((r!   c                    X:X  + $ r   r)   r   s     r   __ne__Decimal128.__ne__^  s      r!   N)r&   _VALUE_OPTIONSr*   None)r*   r=   )r   r<   r&   r   r*   r$   )r*   r   )r*   rZ   )r&   Tuple[int, int]r*   r   )r*   r   )r   r   r*   bool)r+   r,   r-   r.   r/   	__slots___type_markerrz   r9   classmethodr   r0   r   r   r   r   r   r   r   r1   r)   r!   r   r$   r$      sj    [z $ILG'@R I I < <)('
!r!   r$   r)   )r*   zdecimal.Context)r&   r   r*   r   )4r/   
__future__r   rA   structr   typingr   r   r   r	   r
   bson.codec_optionsr   r   Structpackr   unpackr   r_   r^   _EXPONENT_MAX_EXPONENT_MIN_MAX_DIGITSr   r   r   r`   rN   rO   rV   rW   rT   rU   ROUND_HALF_EVENInvalidOperationOverflowInexactr?   rB   r@   rJ   floatrZ   rX   r   r   r3   rD   rk   r$   r)   r!   r   <module>r      sk   #    4 4 7==##]]4 ''
	q	q		q	q	
%-	
 ''&&(8(8'//J	 oo4 1 1 34wsE#x}c:Q4RRS![ !$"[ "$#-`B! B!r!   