33import pty
44import threading
55
6- from serial import Serial
6+ import pytest
7+
8+ try :
9+ from serial import Serial
10+ except ImportError :
11+ Serial = None
712
813from .api import RANDOM_BYTES , accountgroups
914from .generator import chacha20poly1305 , accountgroups_output , accountgroups_input
@@ -25,6 +30,8 @@ def listener(port):
2530 os .write (port , b"I don't understand\r \n " )
2631
2732
33+ @pytest .mark .skipif ( not Serial ,
34+ reason = "Serial testing needs pyserial" )
2835def test_serial ():
2936 """Start the testing"""
3037 master ,slave = pty .openpty () # open the pseudoterminal
@@ -77,20 +84,8 @@ def generator( password, cryptopaths, fd ):
7784 )
7885
7986
80- class SerialEOF ( Serial ):
81- """Convert any SerialException to an EOFError, for compatibility with PTY. In real serial ports,
82- we'll handle detection of counterparty readiness with DTR/DSR, and flow control with RTS/CTS."""
83- def read ( self , size = 1 ):
84- while True :
85- try :
86- return super ( SerialEOF , self ).read ( size = size )
87- except Exception as exc : # SerialError as exc:
88- # if "readiness" in str(exc):
89- # time.sleep( .1 )
90- # continue
91- raise EOFError ( str ( exc ))
92-
93-
87+ @pytest .mark .skipif ( not Serial ,
88+ reason = "Serial testing needs pyserial" )
9489def test_groups_pty ():
9590 password = "password"
9691 master ,slave = pty .openpty ()
@@ -107,6 +102,22 @@ def test_groups_pty():
107102 gen .daemon = True
108103 gen .start ()
109104
105+ class SerialEOF ( Serial ):
106+ """Convert any SerialException to an EOFError, for compatibility with PTY. In real serial
107+ ports, we'll handle detection of counterparty readiness with DTR/DSR, and flow control with
108+ RTS/CTS.
109+
110+ """
111+ def read ( self , size = 1 ):
112+ while True :
113+ try :
114+ return super ( SerialEOF , self ).read ( size = size )
115+ except Exception as exc : # SerialError as exc:
116+ # if "readiness" in str(exc):
117+ # time.sleep( .1 )
118+ # continue
119+ raise EOFError ( str ( exc ))
120+
110121 ser = SerialEOF ( slave_name , timeout = 1 )
111122 for group in accountgroups_input (
112123 cipher = chacha20poly1305 ( password = password ),
0 commit comments