
    	h                         S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r	  Sr
 " S S\	5      r\S:X  a  \R                  " S5        gg)	a  Testing pasing object between multiple COM threads

Uses standard COM marshalling to pass objects between threads.  Even
though Python generally seems to work when you just pass COM objects
between threads, it shouldn't.

This shows the "correct" way to do it.

It shows that although we create new threads to use the Python.Interpreter,
COM marshalls back all calls to that object to the main Python thread,
which must be running a message loop (as this sample does).

When this test is run in "free threaded" mode (at this stage, you must
manually mark the COM objects as "ThreadingModel=Free", or run from a
service which has marked itself as free-threaded), then no marshalling
is done, and the Python.Interpreter object start doing the "expected" thing
- ie, it reports being on the same thread as its caller!

Python.exe needs a good way to mark itself as FreeThreaded - at the moment
this is a pain in the but!

    N   )
InterpCasec                   B    \ rS rSrS rS rS rS rSS jrS r	S r
S	rg
)ThreadInterpCase%   c                      U R                  U5        [        R                  " U5        g ! [        R                  " U5        f = fN)_doTestInThread
win32eventSetEvent)self	stopEventinterps      lC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\win32com/test/testMarshal.py_testInterpInThread$ThreadInterpCase._testInterpInThread&   s2    	+  (	*J	*s	   * Ac                 B   [         R                  " 5         [        R                  " 5       n[        (       aD  [         R
                  " U[         R                  5      n[        R                  R                  U5      nUR                  S5        [         R                  " 5         g )Nzimport win32api)	pythoncomCoInitializewin32apiGetCurrentThreadIdfreeThreadedCoGetInterfaceAndReleaseStreamIID_IDispatchwin32comclientDispatchExecCoUninitialize)r   r   myThreads      r   r
    ThreadInterpCase._doTestInThread,   sk     ..0<==	//F __--f5F%&  "    c                    [         R                  R                  S5      n/ n/ n[        U5       H  n[        R
                  " SSSS5      nUR                  U5        [        R                  " [        R                  UR                  5      n[        R                  " U R                  Xg4SS9nUR                  5         UR                  U5        M     SnXC4$ )zCreates multiple threads using simple (but slower) marshalling.

Single interpreter object, but a new stream is created per thread.

Returns the handles the threads will set when complete.
Python.InterpreterNr   Ttargetargsdaemon)r   r   r   ranger   CreateEventappendr   %CoMarshalInterThreadInterfaceInStreamr   _oleobj_	threadingThreadr   start)	r   
numThreadsr   eventsthreadsihEventinterpStreamts	            r   BeginThreadsSimpleMarshal*ThreadInterpCase.BeginThreadsSimpleMarshal:   s     ))*>?z"A++D!Q=FMM&!$JJ''L   //+A
 GGINN1 # r"   c                    [         R                  R                  S5      n[        (       a/  [        R
                  " [        R                  UR                  5      n/ n/ n[        U5       Ho  n[        R                  " SSSS5      n[        R                  " U R                  Xb4SS9nUR                  5         UR                  U5        UR                  U5        Mq     XC4$ )zCreates multiple threads using fast (but complex) marshalling.

The marshal stream is created once, and each thread uses the same stream

Returns the handles the threads will set when complete.
r$   Nr   Tr%   )r   r   r   r   r   r,   r   r-   r)   r   r*   r.   r/   r   r0   r+   )r   r1   r   r2   r3   r4   r5   r7   s           r   BeginThreadsFastMarshal(ThreadInterpCase.BeginThreadsFastMarshalX   s     ))*>?<DD''F z"A++D!Q=F  //%A
 GGIMM&!NN1 # r"   c                    U" S5      u  p4Sn  U(       a  [         R                  " SSU5      nO'[        R                  " USS[        R                  5      nU[        R
                  :  a6  U[        R
                  [        U5      -   :  a  US-  nU[        U5      :  a  OqOmU[        R
                  [        U5      -   :X  a  [         R                  " 5         O7[        S[         R                  " 5       [         R                  " 5       4-  5         GM  U H4  nUR                  S5        U R                  UR                  5       S5        M6     S ng ! [         a     MJ  f = f)N   r   r   i  z:Waiting for thread to stop with interfaces=%d, gateways=%dzthread failed to stop!?)r   CoWaitForMultipleHandlesr   MsgWaitForMultipleObjectsQS_ALLINPUTWAIT_OBJECT_0lenPumpWaitingMessagesprint_GetInterfaceCount_GetGatewayCountKeyboardInterruptjoinassertFalseis_alive)r   fnbCoWaitr3   r2   numFinishedrcr7   s           r   _DoTestMarshalThreadInterpCase._DoTestMarshalr   s3   Q%";;AtVLB $==4)?)?B *222Z55FCC1$K"c&k1 2:33c&kAA113T$7799;U;U;WXY+ 6 AFF1IQZZ\+DE   % s   BE  5E 6E 
EEc                 :    U R                  U R                  5        g r	   rP   r8   r   s    r   testSimpleMarshal"ThreadInterpCase.testSimpleMarshal   s    D::;r"   c                 <    U R                  U R                  S5        g )Nr   rS   rT   s    r   testSimpleMarshalCoWait(ThreadInterpCase.testSimpleMarshalCoWait   s    D::A>r"    N)r   )__name__
__module____qualname____firstlineno__r   r
   r8   r;   rP   rU   rX   __static_attributes__rZ   r"   r   r   r   %   s&    +#<4"N<?r"   r   __main__testMarshal)__doc__r.   unittestr   r   win32com.clientr   r   testServersr   r   r   r[   mainrZ   r"   r   <module>rg      sP   .       #x?z x?| zMM-  r"   