ó
ÔŒ\c           @  s¶   d  d l  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 j
 ƒ  p d Z d „  Z d „  Z d „  Z d „  Z e d „ Z d S(	   iÿÿÿÿ(   t   print_functionNs   utf-8c         C  s»   g  } x{ | D]s \ } } } } t  | t ƒ sC t | t d ƒ } n  t  | t ƒ rg | j d d ƒ } n  | j | | | | f ƒ q Wx0 t j | ƒ D] } |  j | j	 d d ƒ ƒ q” Wd S(   s@   
    Given the traceback list l, writes it to out as utf-8.
    t   replaces   utf-8N(
   t
   isinstancet   unicodet
   FSENCODINGt   strt   decodet   appendt	   tracebackt   format_listt   writet   encode(   t   outt   lt   ult   filenamet   linet   whatt   textt   t(    (    s   renpy/error.pyt   write_utf8_traceback_list&   s    c         C  sN  g  } xÐ |  rØ |  j  } |  j } | j } | j } | j } |  j }  d | j k r¼ t j j	 r¼ | j d } |  d k } y5 | j | | ƒ }	 |	 d k	 r® | j |	 ƒ w	 n  Wq¼ q¼ Xn  | j | | | d f ƒ q	 Wg  }
 xh | D]` \ } } } } | d k r-y t j | | ƒ } Wq-d } q-Xn  |
 j | | | | f ƒ qæ W|
 S(   s`   
    Given `tb`, returns a list of (filename, line_number, function, line_text)
    tuples.
    t   selft    N(   t   tb_framet	   tb_linenot   f_codet   co_filenamet   co_namet   tb_nextt   f_localst   renpyt   configt   raw_tracebackst   Nonet   report_tracebackt   extendR   t	   linecachet   getline(   t   tbR   t   framet   line_numbert   codeR   t   namet   objt   lastt   reportt   rvR   (    (    s   renpy/error.pyt   traceback_listA   s8    							
c         C  s`   g  } xS |  D]K } | d } | j  d ƒ r | j d d ƒ j d ƒ r | j | ƒ q q W| S(   s   
    Returns the subset of `tl` that originates in creator-written files, as
    opposed to those portions that come from Ren'Py itself.
    i    s   .rpys   \t   /s   common/(   t   endswithR   t
   startswithR   (   t   tlR.   R   R   (    (    s   renpy/error.pyt   filter_traceback_listp   s    
+c         C  s§   y8 t  j j t j j |  ƒ } t | | ƒ } | | f SWn n Xy t |  | ƒ } | |  f SWn n Xd d l } t  j j | j ƒ  d |  ƒ } t | | ƒ | f S(   s_   
    Opens an error/log/file. Returns the open file, and the filename that
    was opened.
    iÿÿÿÿNs   renpy-(	   t   ost   patht   joinR   R   t   logdirt   filet   tempfilet
   gettempdir(   t   fnt   modet   new_fnt   fR:   (    (    s   renpy/error.pyt   open_error_file€   s    c         C  s  d d l  } t j ƒ  \ } } } d „  } t j ƒ  } t j ƒ  } t | ƒ }	 t |	 ƒ }
 t t j	 j
 d | ƒt | |
 ƒ t | j d d d d | ƒt | |  ƒ d | ƒt d d | ƒt | |	 ƒ t | j d d d d | ƒt | |  ƒ d | ƒyP t j j d	 ƒ t j j | j ƒ  ƒ t j j d	 ƒ t j j | j ƒ  ƒ Wn n Xt d | ƒ ym t t j ƒ  d | ƒt t j d | ƒt | t j j d t j j ƒ d | ƒt t j ƒ  d | ƒWn n X| j ƒ  } | j ƒ  } yÜ t d
 d ƒ \ } } | j | j ƒ t d d | ƒt d | ƒ | j | ƒ t d | ƒ t d d | ƒt d | ƒ | j | ƒ | j ƒ  y> | rÆt j	 j j d k rÆt j j | g d d d ƒn  Wn n XWn n X| j d d ƒ | j d d ƒ | f S(   s  
    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 two-unicode tuple, with the first item being
    a simple message, and the second being a full traceback.
    iÿÿÿÿNc         S  sÄ   y t  |  ƒ } WnŠ yY t |  j ƒ d k r7 d } n7 t |  j ƒ d k r\ |  j d } n d j |  j ƒ } Wq y t |  ƒ } Wq™ d } q™ Xq Xn Xt | t  ƒ r¼ | j d d ƒ S| Sd  S(   Ni    R   i   t    s   <Could not encode exception.>s   utf-8R   (   R   t   lent   argsR7   t   reprR   R   (   t   et   m(    (    s   renpy/error.pyt	   safe_utf8ª   s"    	R9   t   :t   endRA   s   Full traceback:s   
s   traceback.txtt   ws.   I'm sorry, but an uncaught exception occurred.sN   -- Full Traceback ------------------------------------------------------------t   runi   t	   transients   utf-8R   (   t   codecst   syst   exc_infot	   cStringIOt   StringIOR/   R4   t   printR   t   gamet   exception_infoR   t   __name__t   stdoutR
   t   getvaluet   platformt   versionR   R*   t   timet   ctimeR@   t   BOM_UTF8t   closeRC   t   commandt   exportst   launch_editorR   (   RE   t   editorRM   t   typet   _valueR&   RG   t   simplet   fullt   full_tlt	   simple_tlR?   t   traceback_fn(    (    s   renpy/error.pyt   report_exception™   sf    	*
#(   t
   __future__R    R   RN   RP   RX   R$   RZ   R5   R   t   getfilesystemencodingR   R   R/   R4   R@   t   TrueRi   (    (    (    s   renpy/error.pyt   <module>   s   		/		