B
    u9a                 @   s   d dl Z d dl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G dd de jZG dd dZG dd dee jZG d	d
 d
ee jZedkre   dS )    N)TESTFNunlinkunloadrmtreescript_helpercaptured_stdoutc               @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )MiscSourceEncodingTestc             C   s(   |  ddd |  ddd d S )Nu
   Питонzutf-8s
   Питонu   \Пs   \П)assertEqualencode)self r   */usr/lib/python3.7/test_source_encoding.pytest_pep263   s    z"MiscSourceEncodingTest.test_pep263c             C   s.   t ddd}i }t|| | |d d d S )Ns   
# coding: utf-8
u = 'ó'
dummyexecu   ó)compiler   r	   )r   cdr   r   r   test_compilestring   s    
z)MiscSourceEncodingTest.test_compilestringc          
   C   sN   yt ddd W n0 tk
r@ } z| |jd W d d }~X Y n
X |   d S )Ns   # coding: cp932
print 'N'r   r   u   print '年'
)r   SyntaxErrorr	   textZfail)r   vr   r   r   test_issue2301   s
     z%MiscSourceEncodingTest.test_issue2301c             C   s.   t ddd}i }t|| | |d d d S )Nu   # coding=latin-1
Æ = 'Æ'r   r      Æ)r   r   r	   )r   r   r   r   r   r   test_issue4626&   s    
z%MiscSourceEncodingTest.test_issue4626c             C   sj   t ddd}i }t|| | |d |d  | t|d t|d  | t|d t|d  d S )Nu   a, b = '𐄏', '\U0001010F'r   r   ab)r   r   r	   lenascii)r   r   r   r   r   r   test_issue3297,   s    
z%MiscSourceEncodingTest.test_issue3297c             C   s    |  ttd |  ttd d S )Ns    s    )assertRaisesr   eval)r   r   r   r   test_issue78204   s    z%MiscSourceEncodingTest.test_issue7820c             C   sR   t jtjtjtjtdgt j	d}|
 d }| |jd | d| d S )Nzcoding20731.py)stderr   r   s   SyntaxError)
subprocessPopensys
executableospathjoindirname__file__PIPEZcommunicater	   
returncodeZassertNotIn)r   suberrr   r   r   
test_20731?   s    
z!MiscSourceEncodingTest.test_20731c          	   C   s   t ddd t ddd t ddd | td t ddd W d Q R X | td t d	dd W d Q R X | td
 t d	dd W d Q R X | td t ddd W d Q R X | td
 t ddd W d Q R X d S )Ns   # -*- coding: iso-8859-15 -*-
r   r   s   ﻿
s   ﻿# -*- coding: utf-8 -*-
Zfakes   # -*- coding: fake -*-
ziso-8859-15s!   ﻿# -*- coding: iso-8859-15 -*-
BOMs   ﻿# -*- coding: fake -*-
)r   ZassertRaisesRegexr   )r   r   r   r   test_error_messageH   s    z)MiscSourceEncodingTest.test_error_messagec             C   s   d}|  | d S )NZ
bad_coding)verify_bad_module)r   module_namer   r   r   test_bad_codingY   s    z&MiscSourceEncodingTest.test_bad_codingc             C   s   d}|  | d S )NZbad_coding2)r7   )r   r8   r   r   r   test_bad_coding2]   s    z'MiscSourceEncodingTest.test_bad_coding2c          	   C   sd   |  ttd|  tjt}tj||d }t|d}|	 }W d Q R X |  tt
||d d S )Nztest.z.pyrbr   )r"   r   
__import__r+   r,   r.   r/   r-   openreadr   )r   r8   r,   filenamefpbytesr   r   r   r7   a   s    z(MiscSourceEncodingTest.verify_bad_modulec             C   s"   i }t d| | |d d d S )Ns   # coding: cp949
a = ""
r   u   ぇ)r   r	   )r   r   r   r   r   test_exec_valid_codingj   s    
z-MiscSourceEncodingTest.test_exec_valid_codingc          	   C   s   t t td }t|ddd}tjdtj zP|2 |d |d |d |d	d
  W d Q R X t	
  tt W d tjd= t| t|d  t|d  t t td X d S )Nz.pywcp1252)encodingr   z# -*- coding: cp1252 -*-
z'''A short string
z'''
z'A very long string %s'
A  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXr   o__pycache__)r   r   r=   r)   r,   insertr+   curdirwrite	importlibinvalidate_cachesr<   r   r   )r   r?   fr   r   r   test_file_parseo   s$    


z&MiscSourceEncodingTest.test_file_parsec          	   C   sX   d d}| t}t|dd W d Q R X d}| j|jjd ||jjd d d S )Nu   # coding: ascii
☃zutf-8z<string>r   zN'ascii' codec can't decode byte 0xe2 in position 16: ordinal not in range(128)r   )msg)r
   r"   r   r   Z
assertTrueZ	exceptionargs
startswith)r   inputr   expectedr   r   r   test_error_from_string   s    
z-MiscSourceEncodingTest.test_error_from_stringN)__name__
__module____qualname__r   r   r   r   r!   r$   r4   r6   r9   r:   r7   rB   rN   rT   r   r   r   r   r      s   
		r   c               @   s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )AbstractSourceEncodingTestc             C   s   d}|  |d d S )Ns   print(ascii("ä"))
s   '\xe4')check_script_output)r   srcr   r   r   test_default_coding   s    z.AbstractSourceEncodingTest.test_default_codingc             C   s   d}|  |d d S )Ns&   #coding:iso8859-15
print(ascii("ä"))
s   '\xc3\u20ac')rY   )r   rZ   r   r   r   test_first_coding_line   s    z1AbstractSourceEncodingTest.test_first_coding_linec             C   s   d}|  |d d S )Ns(   #
#coding:iso8859-15
print(ascii("ä"))
s   '\xc3\u20ac')rY   )r   rZ   r   r   r   test_second_coding_line   s    z2AbstractSourceEncodingTest.test_second_coding_linec             C   s   d}|  |d d S )Ns*   #
#
#coding:iso8859-15
print(ascii("ä"))
s   '\xe4')rY   )r   rZ   r   r   r   test_third_coding_line   s    z1AbstractSourceEncodingTest.test_third_coding_linec             C   s   d}|  |d d S )Ns5   #coding:iso8859-15
#coding:latin1
print(ascii("ä"))
s   '\xc3\u20ac')rY   )r   rZ   r   r   r   test_double_coding_line   s    z2AbstractSourceEncodingTest.test_double_coding_linec             C   s   d}|  |d d S )Ns4   #coding:iso8859-15 coding:latin1
print(ascii("ä"))
s   '\xc3\u20ac')rY   )r   rZ   r   r   r   test_double_coding_same_line   s    z7AbstractSourceEncodingTest.test_double_coding_same_linec             C   s   d}|  |d d S )Ns)   #coding:iso-8859-15 
print(ascii("ä"))
s   '\xc3\u20ac')rY   )r   rZ   r   r   r   test_first_non_utf8_coding_line   s    z:AbstractSourceEncodingTest.test_first_non_utf8_coding_linec             C   s   d}|  |d d S )Ns*   
#coding:iso-8859-15 
print(ascii("ä"))
s   '\xc3\u20ac')rY   )r   rZ   r   r   r    test_second_non_utf8_coding_line   s    z;AbstractSourceEncodingTest.test_second_non_utf8_coding_linec             C   s   d}|  |d d S )Ns   ﻿print(ascii("ä"))
s   '\xe4')rY   )r   rZ   r   r   r   test_utf8_bom   s    z(AbstractSourceEncodingTest.test_utf8_bomc             C   s   d}|  |d d S )Ns$   ﻿#coding:utf-8
print(ascii("ä"))
s   '\xe4')rY   )r   rZ   r   r   r   "test_utf8_bom_and_utf8_coding_line   s    z=AbstractSourceEncodingTest.test_utf8_bom_and_utf8_coding_lineN)rU   rV   rW   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   r   r   r   r   rX      s   rX   c               @   s   e Zd Zdd ZdS )BytesSourceEncodingTestc          	   C   s<   t  }t| W d Q R X | d}| | | d S )Nlatin1)r   r   getvaluer
   r	   rstrip)r   rZ   rS   stdoutoutr   r   r   rY      s    z+BytesSourceEncodingTest.check_script_outputN)rU   rV   rW   rY   r   r   r   r   re      s   re   c               @   s   e Zd Zdd ZdS )FileSourceEncodingTestc          
   C   sb   t  >}tj|d}t|d}|| W d Q R X t|}W d Q R X | 	|j
 | d S )Nztest.pywb)tempfileZTemporaryDirectoryr+   r,   r-   r=   rJ   r   Zassert_python_okr	   rj   rh   )r   rZ   rS   Ztmpdfnr@   Zresr   r   r   rY      s    
z*FileSourceEncodingTest.check_script_outputN)rU   rV   rW   rY   r   r   r   r   rk      s   rk   __main__)ZunittestZtest.supportr   r   r   r   r   r   rK   r+   r)   r'   rm   ZTestCaser   rX   re   rk   rU   mainr   r   r   r   <module>   s     :	