Skip to content

Commit 94f8450

Browse files
committed
Test rs485_sync working on Windows
1 parent a0bd179 commit 94f8450

File tree

3 files changed

+137
-72
lines changed

3 files changed

+137
-72
lines changed

modbus_test.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,14 @@ def __init__( self, command, stderr=subprocess.STDOUT, stdin=None, bufsize=0, bl
7979
# binary data from the target process and decode it as received. This retains
8080
# consistency with Python2, and also is necessary to support non-blocking sockets --
8181
# which defeat the built-in Python codecs, which do *not* offer non-blocking support.
82-
self.process = subprocess.Popen(
83-
command, stdout=subprocess.PIPE, stderr=stderr, stdin=stdin,
84-
bufsize=bufsize, preexec_fn=os.setsid, shell=shell )
82+
if sys.platform == 'win32':
83+
self.process = subprocess.Popen(
84+
command, stdout=subprocess.PIPE, stderr=stderr, stdin=stdin,
85+
bufsize=bufsize, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP, shell=shell )
86+
else:
87+
self.process = subprocess.Popen(
88+
command, stdout=subprocess.PIPE, stderr=stderr, stdin=stdin,
89+
bufsize=bufsize, preexec_fn=os.setsid, shell=shell )
8590
log.normal( 'Started Server PID [%d]: %s', self.process.pid, self.command )
8691
if not blocking:
8792
self.non_blocking()
@@ -90,8 +95,26 @@ def __init__( self, command, stderr=subprocess.STDOUT, stdin=None, bufsize=0, bl
9095

9196
def non_blocking( self ):
9297
fd = self.process.stdout.fileno()
93-
fl = fcntl.fcntl( fd, fcntl.F_GETFL )
94-
fcntl.fcntl( fd, fcntl.F_SETFL, fl | os.O_NONBLOCK )
98+
try:
99+
# Windows' Python 3 doesn't appear to have the capacity to
100+
# stream data from subprocess in a non-blocking fashion
101+
# if sys.platform == "win32":
102+
# import win32pipe
103+
# import msvcrt
104+
# handle = msvcrt.get_osfhandle(self.process.stdout.fileno())
105+
# # Set the pipe to non-blocking mode
106+
# # PIPE_NOWAIT is the Windows equivalent of O_NONBLOCK
107+
# win32pipe.SetNamedPipeHandleState(
108+
# handle,
109+
# win32pipe.PIPE_NOWAIT,
110+
# None,
111+
# None
112+
# )
113+
# else:
114+
fl = fcntl.fcntl( fd, fcntl.F_GETFL )
115+
fcntl.fcntl( fd, fcntl.F_SETFL, fl | os.O_NONBLOCK )
116+
except Exception as exc:
117+
log.warning( "Unable to set O_NONBLOCK on subprocess stdout; tests may not work correctly: {exc}".format( exc=exc ))
95118

96119
@property
97120
def stdout( self ):
@@ -191,14 +214,15 @@ def start_simulator( simulator, *options, **kwds ):
191214
return command,control.address
192215

193216

194-
def start_modbus_simulator( *options ):
217+
def start_modbus_simulator( *options, **kwds ):
195218
"""Start bin/modbus_sim.py; assumes it flushes stdout when printing bindings so we can parse it
196219
here.
197220
198221
"""
199222
return start_simulator(
200223
os.path.join( os.path.dirname( os.path.abspath( __file__ )), 'bin', 'modbus_sim.py' ),
201-
*options
224+
*options,
225+
**kwds
202226
)
203227

204228

requirements-tests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
flake8
22
pylint
33
pytest >=4.6
4+
#pywin32 ; sys_platform == "win32"

0 commit comments

Comments
 (0)