
     lf#                     ,   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d dlm	Z	 d dl
Z
d dlZd dlmZ d dlmZ d dlmZ d dlmZ dZddZ ed	      d
        Z ed	      d        Ze
j0                  j3                   ej4                         dk7  d      e
j0                  j7                  d       ed	      d                      Ze
j0                  j;                  dd      e
j0                  j7                  d       ed 	      d                      Ze
j0                  j;                  dd       ed 	      d               Z ed	      d        Z e
j0                  j;                  dd       ed	      d               Z! ed	      d        Z"y)    N)Image)_c_internal_utils)ToolToggleBase)subprocess_run_helper<   c                     t        j                  t               S dt        j                  v rS t
        j                  j                  t        j                  j                  d       d      t
        j                  j                  t        j                  dk(  xr t        j                          d      t        j                         fd                     }|S )z
    A decorator to run *func* in a subprocess and assert that it prints
    "success" *success_count* times and nothing on stderr.

    TkAgg tests seem to have interactions between tests, so isolate each test
    in a subprocess. See GH#18261
    MPL_TEST_ESCAPE_HATCHtkinterzmissing tkinterreasonlinuxz'$DISPLAY and $WAYLAND_DISPLAY are unsetc                  &   t        j                  d       	 t        t        t	        dd            } g d}| j
                  j                         D cg c]  t        fd|D              r c}rJ | j                  j                  d      k(  sJ y c c}w # t        j                  $ r t        j                  d	       Y y t        j                  $ r5}t        j                  d
t        |j
                        z          Y d }~y d }~ww xY w)Nr
   TkAgg1)
MPLBACKENDr	   )timeout	extra_env)OpenGLz!CFMessagePort: bootstrap_registerz%/usr/include/servers/bootstrap_defs.hc              3   &   K   | ]  }|v 
 y wN ).0msglines     B/usr/lib/python3/dist-packages/matplotlib/tests/test_backend_tk.py	<genexpr>z7_isolated_tk_test.<locals>.test_func.<locals>.<genexpr>G   s     H3s$Hs   successzSubprocess timed outz,Subprocess failed to test intended behavior
)pytestimportorskipr   _test_timeoutdictstderr
splitlinesallstdoutcount
subprocessTimeoutExpiredfailCalledProcessErrorstr)procignored_linesr   efuncsuccess_counts     ` r   	test_funcz$_isolated_tk_test.<locals>.test_func(   s     	I&	A(mt&c8CDDFM)-)?)?)A JH-HH  J J J;;$$Y/=@@@J (( 	0KK./,, 	)KKGahh-( ) )	)s#   B B(D	D+DD)	functoolspartial_isolated_tk_testosenvironr   markskipif	importlibutil	find_specsysplatformr   display_is_validwraps)r0   r/   r1   s   `` r   r4   r4      s     |  !2MBB"**,[[NN$$Y//    [[L(9(J(J(L$L8   __TA 		A2        )r0   c            	         dd l m}  dd l}dd l}ddlm}  | j                         \  }}|j                  j                  }|j                  d      }|j                  d d \  }}	||	|j                  j                  f}
d}|D ]:  }	 |j                  |j                  j                         t!        |      |
dd|       < y # t"        $ r t%        d       Y Tw xY w)Nr   )_tkagg)   rD   rD      ))rE   r   rE   )rE   r   r   rE   )   rA   r   rE   )r   rE   rF   rE   )r   rE   rE   r   )r   rE   rG   rA   )r   rG   rE      r   )matplotlib.pyplotpyplotnumpy!matplotlib.backends.backend_tkaggmatplotlib.backendsrC   subplotscanvas_tkphotoonesshapectypesdatablittk
interpaddrr+   
ValueErrorprint)pltnp
matplotlibrC   figax
photoimagerT   heightwidthdataptr	bad_boxesbad_boxs                r   	test_blitre   M   s    #,*cllnGC$$J779DJJrNMFEudkk../GI  	KK((*C
OWag'
  	)	s   7B==CCrG   c                      dd l } dd lm g fd}fd| j                         j	                  d|       j                          rt        d       y y )Nr   c                       j                            j                  ddgddg        j                          j                  d        y )NrG   rE   rH      r   )figureplotcloseafter)legitimate_quitrZ   roots   r   do_plotz;test_figuremanager_preserves_host_mainloop.<locals>.do_ploto   s?    

!Q!Q 		

1o&r@   c                  H     j                          j                  d       y NT)quitappend)rn   r   s   r   rm   zCtest_figuremanager_preserves_host_mainloop.<locals>.legitimate_quitu   s    		tr@   r   )r
   rI   rJ   Tkrl   mainlooprY   )r
   ro   rm   rZ   rn   r   s     @@@@r   *test_figuremanager_preserves_host_mainlooprv   i   sL    #G' ::<DJJq'MMOi r@   CPythonzZPyPy does not support Tkinter threading: https://foss.heptapod.net/pypy/pypy/-/issues/1929r   rH   )rerunsc                  f   dd l } dd ldd lm dd l}ddlm | j                         } j                  g dg d       fd}|j                         |j                  |d      }|j                           j                  d       j                          |j                          y )	Nr   )"_get_running_interactive_frameworkrG   rE   rH   )rG   rE   rh   c                      d         k(  sj                  d       d         k(  s j                          j                         rt        d       y y )NrV   g{Gz?r   )sleeprk   waitrY   )rz   rZ   show_finished_eventtimes   r   targetz6test_figuremanager_cleans_own_mainloop.<locals>.target   sL    <>>JJsO <>>		##%) &r@   T)r   daemonblock)r
   r   rI   rJ   	threadingmatplotlib.cbookrz   rt   rj   EventThreadstartshowsetjoin)	r
   r   rn   r   threadrz   rZ   r   r   s	        @@@@r   &test_figuremanager_cleans_own_mainloopr      s     #C::<DCHHY	" $//+VD9F
LLNCHH4
KKMr@   r   T)skip_on_importerrorc                     dd l } | j                  `| j                  `dd lm}  |j                         } |j                  d        |j                          |j                  j                  j                          |j                  j                         j                  d|j                  |        |j                  d       y )Nr   Fr   d   T)r
   Miscupdateupdate_idletasksrI   rJ   ri   r   drawrO   toolbarconfigure_subplotsget_tk_widgetrl   rk   )r
   rZ   r]   s      r   test_never_updater      s     %#
#**,CCHH5CHHJJJ))+JJ$$S#))S9 CHH4r@   c                     t        j                         5  t        j                  dt               dt        j
                  d<   d d d        t        j                         5 } t        j                  dd      }t        j                  j                  | d      |j                          G fddt              }t        j                          }|j"                  j$                  j&                  }|j"                  j$                  j(                  }|j+                  d	|       |j+                  d	d
       d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nignoretoolmanagerr   LA)   r   ztest_la_icon.pngc                       e Zd ZW  Zy)4test_toolbar_button_la_mode_icon.<locals>.CustomToolN)__name__
__module____qualname__image)tmp_img_paths   r   
CustomToolr      s     Er@   r   testgroup)warningscatch_warningssimplefilterUserWarningr\   rcParamstempfileTemporaryDirectoryr   newr5   pathr   saver   rZ   ri   rO   managerr   r   add_tool)tempdirimgr   r]   r   r   r   s         @r    test_toolbar_button_la_mode_iconr      s    
	 	 	" 7h4)6
I&7
 
	$	$	& *'iih'ww||G-?@	! 	! jjljj((44**$$,,VZ0)* *7 7
* *s   .D: CE:EErE   c                      dd l m}  ddlm  G fdd      } | j                         }t        d        ||j                  |j                  j                  j                         t        d       y )Nr   )NavigationToolbar2Tkc                   b    e Zd ZW j                  D  cg c]  }|d   dv r| c}} Zyc c}} w ))test_missing_back_button.<locals>.Toolbarr   )HomePanZoomN)r   r   r   	toolitems)r   tr   s   00r   Toolbarr      s2     4 > > 61qT44  6	 6s   +r   r   )	rI   rJ   rL   r   ri   rY   rO   r   window)rZ   r   r]   r   s      @r   test_missing_back_buttonr      sS    #F6& 6
 #**,C	)CJJ

**112	)r@   c                  6   dd l } dd lm g fd}| j                          j                          j
                  g d       j                  dj                         j                  d|       j                          rt        d       y y )Nr   c                     j                   j                         } | j                         s| j                          | j	                         | k(  rj                  d        j                          j                          y rq   )rO   r   winfo_viewablewait_visibilityfocus_lastforrs   rk   destroy)tkcanvasr]   rZ   rn   r   s    r   check_focusz&test_canvas_focus.<locals>.check_focus   s`    ::++-&&($$& !!#x/NN4 		r@   r{   r   r   )
r
   rI   rJ   rt   ri   rj   rl   r   ru   rY   )rV   r   r]   rZ   rn   r   s     @@@@r   test_canvas_focusr      sv     #G
 557D
#**,CCHHYJJq#((JJsK MMOi r@   c                      dd l } ddlmm ddlm ddlm | j                         }fd} ||       t        d       |j                  ddd	
        ||       t        d       y )Nr   )FigureCanvasTkAggr   )key_press_handler)Figurec                            }|j                         }|j                  g d        ||       }|j                          |j                  d       |j	                         j                  dd        || d      }|j                  dd	       |j	                         j                          |j                          y )
Nr{   )masterkey_press_eventTboth)expandfillF)pack_toolbarx)add_subplotrj   r   mpl_connectr   packforget)	r   r]   r^   rO   r   r   r   r   r   s	        r   test_figurez#test_embedding.<locals>.test_figure
  s    h__
	"3v6,.?@##4f#=&vvEJDs+%%'r@   r   zsky bluezmidnight bluewhite)
backgroundselectColor
foreground)r
   rL   r   r   matplotlib.backend_basesr   matplotlib.figurer   rt   rY   tk_setPalette)rV   rn   r   r   r   r   r   s      @@@@r   test_embeddingr      s_    1:(557D  	)
 	*/")  +	)r@   r   )#r2   r9   r5   r=   r'   r<   r   r   PILr   r   r\   r   matplotlib.backend_toolsr   matplotlib.testingr   rI   rJ   rZ   r    r4   re   rv   r7   r8   python_implementationflakyr   backendr   r   r   r   r   r   r@   r   <module>r      s     	   
      ( 3 4  2j # $6 # $. 2H224	AO  P !# $ P
2 W$7!# $  8, W$7#* $ 8*0 # $ W$7# $ 88 #" $"r@   