U
    
W[C                     @   s   d Z ddlmZmZ ddlZddlZddlmZmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZ dZervd	Zn
e sd
ZdddddddddddgZG dd dZG dd deZG dd dejZG dd deZdd Zee  dS ) z2
Tests for L{twisted.python._oldstyle._oldStyle}.
    )absolute_importdivisionN)namedAnyfullyQualifiedName)	getModule)_PY3_shouldEnableNewStyle)unittest)	_oldstylezNot relevant on Python 3.z#Not running with TWISTED_NEWSTYLE=1ztwisted._threadsztwisted.applicationztwisted.internetztwisted.loggerztwisted.pluginsztwisted.positioningztwisted.protocols.haproxyztwisted.pythonztwisted.scriptztwisted.tapztwisted.trialc                   @   s   e Zd ZdZdZdd ZdS )SomeOldStyleClassz
    I am a docstring!
    bazc                 C   s   dS )zX
        A function on an old style class.

        @return: "hi", for testing.
        hi )selfr   r   >/usr/lib/python3/dist-packages/twisted/test/test_nooldstyle.pyfunc1   s    zSomeOldStyleClass.funcN)__name__
__module____qualname____doc__barr   r   r   r   r   r   +   s   r   c                   @   s   e Zd ZdZdS )SomeNewStyleClassz
    Some new style class!
    N)r   r   r   r   r   r   r   r   r   ;   s   r   c                   @   sR   e Zd ZdZdd Zdd Zdd Zdd	 Zer6d
e_	e
rHe
e_	e
e_	nde_	dS )OldStyleDecoratorTestsz+
    Tests for L{_oldstyle._oldStyle}.
    c                 C   st   G dd dt }| t|tj t|}| t|t | |jt tf | | 	 d | | j
d dS )z
        L{_oldstyle._oldStyle} wraps an old-style class and returns a new-style
        class that has the same functions, attributes, etc.
        c                   @   s   e Zd ZdS )zLOldStyleDecoratorTests.test_makesNewStyle.<locals>.SomeClassThatUsesOldStyleN)r   r   r   r   r   r   r   SomeClassThatUsesOldStyleL   s   r   r   r   N)r   assertEqualtypetypes	ClassTyper
   	_oldStyle	__bases__objectr   r   )r   r   updatedClassr   r   r   test_makesNewStyleG   s    
z)OldStyleDecoratorTests.test_makesNewStylec                 C   s>   t t}| |jtj | |jtj | |jtj dS )z
        The class returned by L{_oldstyle._oldStyle} has the same C{__name__},
        C{__module__}, and docstring (C{__doc__}) attributes as the original.
        N)r
   r   r   r   r   r   r   r   r!   r   r   r   test_carriesAttributesW   s    
z-OldStyleDecoratorTests.test_carriesAttributesc              	   C   s8   |  t}tt W 5 Q R X | |jjd d dS )zp
        Using L{_oldstyle._oldStyle} on a new-style class on Python 2 will
        raise an exception.
        r   ztwisted.python._oldstyle._oldStyle is being used to decorate a new-style class (twisted.test.test_nooldstyle.SomeNewStyleClass). This should only be used to decorate old-style classes.N)ZassertRaises
ValueErrorr
   r   r   r   Z	exceptionargs)r   er   r   r   test_onlyOldStyleMayBeDecoratedc   s    
z6OldStyleDecoratorTests.test_onlyOldStyleMayBeDecoratedc                 C   s.   t t}| t|tt | |t dS )zw
        On Python 3 or on Py2 when C{TWISTED_NEWSTYLE} is not set,
        L{_oldStyle._oldStyle} is a no-op.
        N)r
   r   r   r   r   ZassertIsr#   r   r   r   test_noOpByDefaultr   s    
z)OldStyleDecoratorTests.test_noOpByDefaultzOnly relevant on Py2.z7Only relevant when not running under TWISTED_NEWSTYLE=1N)r   r   r   r   r"   r$   r(   r)   r   skip_skipr   r   r   r   r   B   s   	r   c                   @   s   e Zd ZdZeZdd ZdS )NewStyleOnlyz
    A base testclass that takes a module and tests if the classes defined
    in it are old-style.

    CAVEATS: This is maybe slightly dumb. It doesn't look inside functions, for
    classes defined there, or nested classes.
    c              
   C   s   zt | j}W n2 tk
r@ } ztd|W 5 d}~X Y nX g }t|D ]8\}}t|drP|j	| jkrPt
|tjrP|t| qP|rd| _tD ]}| j|rt| d qtdj| jd|ddS )	zJ
        Test that C{self.module} has no old-style classes in it.
        zNot importable: {}Nr   z2Not all classes are made new-style yet. See #8243.todoz$Old-style classes in {module}: {val}z, )moduleval)r   r.   ImportErrorr	   ZSkipTestformatinspectZ
getmembershasattrr   
isinstancer   r   appendr   r-   forbiddenModules
startswithdelattrZFailTestjoin)r   r.   r'   ZoldStyleClassesnamer/   xr   r   r   test_newStyleClassesOnly   s,    "

z%NewStyleOnly.test_newStyleClassesOnlyN)r   r   r   r   r+   r*   r<   r   r   r   r   r,      s   r,   c                    s   t d D ]t dddg} fdd|D }d|kr6qG  fdd	d	ttj} jd
d}||_t|drr||_	| 
||i qdS )z
    Build the test classes that use L{NewStyleOnly}, one class per module.

    @param _locals: The global C{locals()} dict.
    Ztwistedztwisted.test.reflect_helperztwisted.internet.test.process_ztwisted.test.process_c                    s   g | ]} j |qS r   )r:   r7   ).0Zignoredr;   r   r   
<listcomp>   s     z%_buildTestClasses.<locals>.<listcomp>Tc                       s   e Zd ZdZ jZdS )z_buildTestClasses.<locals>.Testz/
            @see: L{NewStyleOnly}
            N)r   r   r   r   r:   r.   r   r>   r   r   Test   s   r@   ._r   N)r   ZwalkModulesr,   r	   TestCaser:   replacer   r3   r   update)Z_localsZignoredModulesZ	isIgnoredr@   ZacceptableNamer   r>   r   _buildTestClasses   s    
rF   )r   Z
__future__r   r   r   r2   Ztwisted.python.reflectr   r   Ztwisted.python.modulesr   Ztwisted.python.compatr   r   Ztwisted.trialr	   Ztwisted.pythonr
   r+   r6   r   r    r   rC   r   r,   rF   localsr   r   r   r   <module>   s>   E*"