
    pjl
                     T    d dl Z e j                  d        Ze j                  d        Zy)    Nc               #   R  K   ddl } ddl}| j                         \  }}	 t        j                  |t        j                  | j
                         |j                  j                          | j                  d      }| j                  |d       	 d | j                  |d       | j                  |d       |j                  j                  | j                  |d      dd        | j                  |       | j                  |       | j                  |       y#  Y xY w# | j                  |d       | j                  |d       |j                  j                  | j                  |d      dd        | j                  |       | j                  |       | j                  |       w xY ww)a  
  
  There a various tricks in Python to capture redirect / capture sys.stdout.
  However, the C++ code is not aware of the Python sys.stdout
  
  This contextmanager captures the C++ output in memory and dumps it on the Python sys.stdout
  
  Caution:
    All C++ output is dumped to a pipe and only passed to stdout at the end of the call.
    This means that e.g. NLP iterates will not show up interactively.
    
    This could in theory be overcome by spawning a sister thread that periodically reads from the buffer and dumps to the Python stdout
    
    
  Usage:
  
  from casadi.tools import *

  x = SX.sym("x")

  with capture_stdout() as out:
    with nice_stdout():
      print "foo"
      x.sparsity().spy()
      
  r   N   xi   )ossyspipefcntlF_SETFL
O_NONBLOCKstdoutflushdupdup2writereadclose)r   r   rwbackups        @/DATA/.local/lib/python3.12/site-packages/casadi/tools/in_out.pynice_stdoutr      s9    8 
'')%1Q		KK5=="--0 **66!9&''!Q-ggfahhqo	jjrwwq',-hhqkhhqkhhv	 	ggfahhqo	jjrwwq',-hhqkhhqkhhvs6   F'/D =F'D BF'DF'BF$$F'c               #     K   dd l } 	 ddlm} | j                  | j
                  }}	  |        |       g}|\  | _        | _        | ||c| _        | _        |d   j                         |d<   |d   j                         |d<   y #  ddlm} Y xY w# ||c| _        | _        d   j                         |d<   |d   j                         |d<   w xY ww)Nr   )StringIOr   )r   	cStringIOr   ior   stderrgetvalue)r   r   oldoutolderrouts        r   capture_stdoutr"   J   s     $ JJ

6F#Z$ #
3:	 &
3:Q"AQ"A !'
3:Q"AQ"As2   CB C!B <CBC=CC)
contextlibcontextmanagerr   r"        r   <module>r'      s=   0 , ,^ # #r&   