a
    V$c                     @   s   d dl 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mZmZmZm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Ze pdZdd Zdd Zd	d
 Zdd Z dddZ!dS )    )divisionabsolute_importwith_statementprint_functionunicode_literals)PY2
basestringbchrbordchropenpystrrangeroundstrtobytesunicodeNutf-8c                 C   sn   g }|D ]F\}}}}t |tr(|t}t |tr<|d}|||||f qt|D ]}| | qZdS )z@
    Given the traceback list, writes it to out as unicode.
    r   N)
isinstancebytesdecode
FSENCODINGappend	tracebackformat_listwrite)outlulfilenamelinewhattextt r$   renpy/error.pywrite_traceback_list*   s    



r&   c              	   C   s   g }| r| j }| j}|j}|j}|j}| j} d|jv rtjj	s|jd }| du }z&|
||}	|	durt||	 W qW n ty   Y n0 ||||df qg }
|D ]N\}}}}|du rzt||}W n ty   d}Y n0 |
||||f q|
S )z`
    Given `tb`, returns a list of (filename, line_number, function, line_text)
    tuples.
    selfN )tb_frame	tb_linenof_codeco_filenameco_nametb_nextf_localsrenpyconfigraw_tracebacksreport_tracebackextend	Exceptionr   	linecachegetline)tbr   frameline_numbercoder   nameobjlastreportrvr    r$   r$   r%   traceback_listE   s8    


rA   c                 C   s@   g }| D ]2}|d }| dr|ddds|| q|S )z
    Returns the subset of `tl` that originates in creator-written files, as
    opposed to those portions that come from Ren'Py itself.
    r   z.rpy\/zcommon/)endswithreplace
startswithr   )tlr@   r#   r   r$   r$   r%   filter_traceback_listt   s    rH   c                 C   s   z&t jtjj| }t||}||fW S  ty8   Y n0 zt| |}|| fW S  ty`   Y n0 ddl}t j|	 d|  }t|||fS )z_
    Opens an error/log/file. Returns the open file, and the filename that
    was opened.
    r   Nzrenpy-)
ospathjoinr0   r1   logdirr   r5   tempfile
gettempdir)fnmodenew_fnfrM   r$   r$   r%   open_error_file   s    



rS   Tc                 C   s  ddl }t \}}}t }t }t|}t|}	ttt	j
j|d t||	 t|jd d|d tt| |d td|d t|| t|jd d|d tt| |d z<tjd tj|  tjd tj|  W n ty   Y n0 td	|d z`ttt tt |d tt	j|d tt	jjd t	jj |d ttt |d W n ty   Y n0 | }| }ztd
d\}
}|
j |
d td|
d td	|
d |
| td	|
d td|
d td	|
d |
| W d   n1 s0    Y  zt	j| W n tyL   Y n0 z:|rt	j
jjdksrt	j
jjrt	jj |gddd W n ty   Y n0 W n& ty   t!j"#t	jj$d
}Y n0 |||fS )aA  
    Reports an exception by writing it to standard error and
    traceback.txt. If `editor` is True, opens the traceback
    up in a text editor.

    Returns a three-item tuple, with the first item being
    a simplified traceback, the second being a full traceback,
    and the third being the traceback filename.
    r   N)file: )endrT   zFull traceback:
r(   ztraceback.txtwu   ﻿z.I'm sorry, but an uncaught exception occurred.zN-- Full Traceback ------------------------------------------------------------run   T)	transient)%codecssysexc_infoioStringIOrA   rH   printr   r0   gameexception_infor&   __name__stdoutr   getvaluer5   platformmachineversionr1   r<   timectimerS   utilexpose_fileargscommanderrors_in_editorexportslaunch_editorrI   rJ   rK   basedir)eeditorr]   type_valuer8   simplefullfull_tl	simple_tlrR   traceback_fnr$   r$   r%   report_exception   sl    



*"
r~   )T)"
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   r   r^   r`   rh   r6   rk   rI   r0   getfilesystemencodingr   r&   rA   rH   rS   r~   r$   r$   r$   r%   <module>   s   8/