o
    8Va)<                     @   s   d Z ddlmZ ddlmZ dZdZdZdd
dZdd Z	dd Z
dd Zd	g ddfddZdd Zd	dd	d	d	dddd	d	d	g fddZd	S )z+Tools for setting up interactive sessions.     )version_tuple)init_printingzfrom __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing()
zjThese commands were executed:
%(source)s
Documentation can be found at https://docs.sympy.org/%(version)s
zCouldn't locate IPython. Having IPython installed is greatly recommended.
See http://ipython.scipy.org for more details. If you use Debian/Ubuntu,
just install the 'ipython' package and start isympy again.
TFNc                 C   s>  ddl m} ddlm} ddlm} ddl m} ddl}ddl}|r$dS d|j	dd	  }	| r2d
}
nd}
d| g}|
d}|durM| dkrM|d |rT|d |
||	|d|f}d| }|du rht}d}|ddd D ]}|s||d7 }qs|d| d 7 }qs|}d|v rd}nd| }|dt||d  7 }|S )z,Create a banner for an interactive session. r   )__version__)GROUND_TYPES)ARCH)SYMPY_DEBUGN z%d.%d.%d   IPythonPythonzground types: %sZSYMPY_USE_CACHEnoz
cache: offzdebugging: onz, z,%s console for SymPy %s (Python %s-%s) (%s)

z>>> Zdevz%s/)sourceversion)Zsympyr   Zsympy.external.gmpyr   Zsympy.utilities.miscr   r   sysosversion_infogetenvlowerappendjoinpreexec_sourcesplitverbose_message)ipythonquietr   Zsympy_versionr   r   r   r   r   Zpython_versionZ
shell_nameinfocacheargsmessageZ_sourcelineZdoc_version r"   ;/usr/lib/python3/dist-packages/sympy/interactive/session.py_make_message   sF   




r$   c                 C   s   ddl m}m}m}m}m} ddlm} dd }g }||| j}	|	D ](\}
}}}}|
|krD||rD|	|df|df||f|dfg q#|
|
|f q#||S )	a  
    Wrap integer literals with Integer.

    This is based on the decistmt example from
    http://docs.python.org/library/tokenize.html.

    Only integer literals are converted.  Float literals are left alone.

    Examples
    ========

    >>> from __future__ import division
    >>> from sympy import Integer # noqa: F401
    >>> from sympy.interactive.session import int_to_Integer
    >>> s = '1.2 + 1/2 - 0x12 + a1'
    >>> int_to_Integer(s)
    '1.2 +Integer (1 )/Integer (2 )-Integer (0x12 )+a1 '
    >>> s = 'print (1/2)'
    >>> int_to_Integer(s)
    'print (Integer (1 )/Integer (2 ))'
    >>> exec(s)
    0.5
    >>> exec(int_to_Integer(s))
    1/2
    r   )generate_tokens
untokenizeNUMBERNAMEOP)StringIOc                 S   s(   d| v sd|   v sd|   v rdS dS )z]
        Returns true if string value num (with token NUMBER) represents an integer.
        .jeFT)r   )Znumr"   r"   r#   _is_intr   s    zint_to_Integer.<locals>._is_intZInteger())tokenizer%   r&   r'   r(   r)   ior*   readlineextendr   )sr%   r&   r'   r(   r)   r*   r.   resultgtoknumtokval_r"   r"   r#   int_to_IntegerU   s   	r;   c                    s&   ddl  | j fdd}|| _dS )zM
    Allow IPython to automatically convert integer literals to Integer.
    r   Nc                    sD   z  |  W n	 ty   Y nw t| } | g|R i | d S N)parseSyntaxErrorr;   )Zcellr   kwargsastZold_run_cellr"   r#   my_run_cell   s   z7enable_automatic_int_sympification.<locals>.my_run_cell)rA   run_cell)shellrB   r"   r@   r#   "enable_automatic_int_sympification   s   
rE   c                    s2   ddl }|d d fdd	}| tf| dS )z?Allow IPython to automatically create symbols (``isympy -a``). r   Nz8name '(?P<symbol>[A-Za-z_][A-Za-z0-9_]*)' is not definedc                    s   |t u rk|jrk|jjsk t|}|durk| jdd|di dd z9z	| jd d }W n ttfy9   Y nw | j|dd W | jd|d dd dS W | jd|d dd n| jd|d dd w | j	j
||||d	}| ||| dS )
zJHandle :exc:`NameError` exception and allow injection of missing symbols. Nz!%(symbol)s = Symbol('%(symbol)s')symbolF)Zstore_historyZInr   zdel %s)	tb_offset)	NameErrortb_nextmatchstrrC   groupZuser_nsKeyError
IndexErrorZInteractiveTBZstructured_tracebackZ_showtraceback)selfetypevaluetbrG   rJ   codeZstbZre_nameerrorr"   r#   _handler   s8   
z*enable_automatic_symbols.<locals>._handlerr<   )recompileZset_custom_excrH   )rD   rV   rU   r"   rT   r#   enable_automatic_symbols   s   'rX   c                 C   s   ddl }t|jtdkrB| s4t|jtdkrddlm} nddlm} | }d|_|| |j	} |r:t
|  |r@t|  | S ddlm} ||S )zConstruct new IPython session. r   N0.11z1.0)ipappF)make_IPython)r
   r   r   ZIPython.terminalrZ   ZIPython.frontend.terminalZTerminalIPythonAppZdisplay_bannerZ
initializerD   rX   rE   ZIPython.Shellr[   )rD   argvauto_symbolsauto_int_to_Integerr
   rZ   Zappr[   r"   r"   r#   init_ipython_session   s"   
r_   c                     s&   ddl m  G  fddd } |  S )zConstruct new Python session. r   InteractiveConsolec                       s   e Zd ZdZ fddZdS )z)init_python_session.<locals>.SymPyConsolez.An interactive console with readline support. c                    s   t  } j| |d z
dd l}dd l}W n
 ty   Y d S w dd l}dd l}|||j	 |
d t|drZ|jd}z|| W n	 tyP   Y nw ||j| d S d S )N)localsr   ztab: completeread_history_filez~/.sympy-history)dict__init__rlcompleterr3   ImportErrorr   atexitZset_completerZ	CompleterZcompleteparse_and_bindhasattrpath
expanduserrc   OSErrorregisterwrite_history_file)rO   Z	ns_localsrf   r3   r   rh   historyr`   r"   r#   re     s*   

z2init_python_session.<locals>.SymPyConsole.__init__N)__name__
__module____qualname____doc__re   r"   r`   r"   r#   SymPyConsole  s    ru   )rS   ra   )ru   r"   r`   r#   init_python_session
  s   rv   c              
      s  ddl }d}| durEzddl}W n ty#   | du rtdd Y nw zddlm} |  W n ty:   d Y nw t }| du rE|} | du rPt   j}n0t |||d t	|j
t	dkr{d fd
d	 _z jdd W n	 tyz   Y nw |s j}|r| rt	|j
t	dk rtd|r| rt	|j
t	dk rtdt} j|d	d t|||| ||	|
d t| ||}|st| |  |d dS t| ddl}|dd  dS )u  
    Initialize an embedded IPython or Python session. The IPython session is
    initiated with the --pylab option, without the numpy imports, so that
    matplotlib plotting can be interactive.

    Parameters
    ==========

    pretty_print: boolean
        If True, use pretty_print to stringify;
        if False, use sstrrepr to stringify.
    order: string or None
        There are a few different settings for this parameter:
        lex (default), which is lexographic order;
        grlex, which is graded lexographic order;
        grevlex, which is reversed graded lexographic order;
        old, which is used for compatibility reasons and for long expressions;
        None, which sets it to lex.
    use_unicode: boolean or None
        If True, use unicode characters;
        if False, do not use unicode characters.
    use_latex: boolean or None
        If True, use latex rendering if IPython GUI's;
        if False, do not use latex rendering.
    quiet: boolean
        If True, init_session will not print messages regarding its status;
        if False, init_session will print messages regarding its status.
    auto_symbols: boolean
        If True, IPython will automatically create symbols for you.
        If False, it will not.
        The default is False.
    auto_int_to_Integer: boolean
        If True, IPython will automatically wrap int literals with Integer, so
        that things like 1/2 give Rational(1, 2).
        If False, it will not.
        The default is False.
    ipython: boolean or None
        If True, printing will initialize for an IPython console;
        if False, printing will initialize for a normal console;
        The default is None, which automatically determines whether we are in
        an ipython instance or not.
    str_printer: function, optional, default=None
        A custom string printer function. This should mimic
        sympy.printing.sstrrepr().
    pretty_printer: function, optional, default=None
        A custom pretty printer. This should mimic sympy.printing.pretty().
    latex_printer: function, optional, default=None
        A custom LaTeX printer. This should mimic sympy.printing.latex()
        This should mimic sympy.printing.latex().
    argv: list of arguments for IPython
        See sympy.bin.isympy for options that can be used to initialize IPython.

    See Also
    ========

    sympy.interactive.printing.init_printing: for examples and the rest of the parameters.


    Examples
    ========

    >>> from sympy import init_session, Symbol, sin, sqrt
    >>> sin(x) #doctest: +SKIP
    NameError: name 'x' is not defined
    >>> init_session() #doctest: +SKIP
    >>> sin(x) #doctest: +SKIP
    sin(x)
    >>> sqrt(5) #doctest: +SKIP
      ___
    \/ 5
    >>> init_session(pretty_print=False) #doctest: +SKIP
    >>> sqrt(5) #doctest: +SKIP
    sqrt(5)
    >>> y + x + y**2 + x**2 #doctest: +SKIP
    x**2 + x + y**2 + y
    >>> init_session(order='grlex') #doctest: +SKIP
    >>> y + x + y**2 + x**2 #doctest: +SKIP
    x**2 + y**2 + x + y
    >>> init_session(order='grevlex') #doctest: +SKIP
    >>> y * x**2 + x * y**2 #doctest: +SKIP
    x**2*y + x*y**2
    >>> init_session(order='old') #doctest: +SKIP
    >>> x**2 + y**2 + x + y #doctest: +SKIP
    x + y + x**2 + y**2
    >>> theta = Symbol('theta') #doctest: +SKIP
    >>> theta #doctest: +SKIP
    theta
    >>> init_session(use_unicode=True) #doctest: +SKIP
    >>> theta # doctest: +SKIP
    θ
    r   NFTz'IPython is not available on this system)get_ipython)r\   r]   r^   rY   execc                    s     | dS )NF)rC   )srcrF   ipr"   r#   <lambda>  s    zinit_session.<locals>.<lambda>)Z
import_allzKautomatic construction of symbols is possible only in IPython 0.11 or abovezQautomatic int to Integer transformation is possible only in IPython 0.11 or above)rF   )pretty_printorderuse_unicode	use_latexr{   str_printerpretty_printerlatex_printerzExiting ...c                   S   s   t dS )NzExiting ...
)printr"   r"   r"   r#   r|     s    )rx   )r   r
   rg   RuntimeErrorrw   boolrv   Zinteractr_   r   r   Z	runsourceZenable_pylab	Exceptionmainloopr   r   r$   r   exitrh   rn   )r   r}   r~   r   r   r   r]   r^   r   r   r   r\   r   Z
in_ipythonr
   rw   r   Z_preexec_sourcer    rh   r"   rz   r#   init_session-  sj   _
r   )TFN)rt   Zsympy.external.importtoolsr   Zsympy.interactive.printingr   r   r   Z
no_ipythonr$   r;   rE   rX   r_   rv   r   r"   r"   r"   r#   <module>   s"    	
85G!#