Skip to content

Commit 06819ac

Browse files
committed
No-op refactor
_receive_until_checkpoint_or_timeout() into a function. This contains code that doesn't work on Windows, and this function will need a tweak for whoever wants to fix it.
1 parent 6512c7d commit 06819ac

File tree

1 file changed

+46
-41
lines changed

1 file changed

+46
-41
lines changed

gnuplotlib.py

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,51 @@ def _printGnuplotPipe(self, string):
16631663
format(len(string), string))
16641664

16651665

1666+
def _receive_until_checkpoint_or_timeout(self, checkpoint, waitforever):
1667+
1668+
fromerr = ''
1669+
while not fromerr.endswith(checkpoint):
1670+
# if no data received in 5 seconds, the gnuplot process is stuck. This
1671+
# usually happens if the gnuplot process is not in a command mode, but in
1672+
# a data-receiving mode. I'm careful to avoid this situation, but bugs in
1673+
# this module and/or in gnuplot itself can make this happen
1674+
1675+
self._logEvent("Trying to read byte from gnuplot")
1676+
1677+
rlist,wlist,xlist = select.select([self.gnuplotProcess.stderr],[], [],
1678+
None if waitforever else 15)
1679+
1680+
if not rlist:
1681+
self._logEvent("Gnuplot read timed out")
1682+
self.checkpoint_stuck = True
1683+
1684+
raise GnuplotlibError(
1685+
r'''Gnuplot process no longer responding. This shouldn't happen... Is your X connection working?''')
1686+
1687+
# read a byte. I'd like to read "as many bytes as are
1688+
# available", but I don't know how to this in a very portable
1689+
# way (I just know there will be windows users complaining if I
1690+
# simply do a non-blocking read). Very little data will be
1691+
# coming in anyway, so doing this a byte at a time is an
1692+
# irrelevant inefficiency
1693+
byte = self.gnuplotProcess.stderr.read(1)
1694+
if len(byte) == 0:
1695+
# Did the child process die?
1696+
returncode = self.gnuplotProcess.poll()
1697+
if returncode is not None:
1698+
# Yep. It died.
1699+
raise Exception(f"gnuplot child died. returncode = {returncode}")
1700+
self._logEvent("read() returned no data")
1701+
continue
1702+
1703+
byte = byte.decode()
1704+
fromerr += byte
1705+
self._logEvent("Read byte '{}' ({}) from gnuplot child process".format(byte,
1706+
hex(ord(byte))))
1707+
1708+
self._logEvent(f"Read string from gnuplot: '{fromerr}'")
1709+
return fromerr
1710+
16661711
# syncronizes the child and parent processes. After _checkpoint() returns, I
16671712
# know that I've read all the data from the child. Extra data that represents
16681713
# errors is returned. Warnings are explicitly stripped out
@@ -1698,47 +1743,7 @@ def _checkpoint(self, flags=''):
16981743
if not self.gnuplotProcess or not self.gnuplotProcess.stderr:
16991744
return '',[]
17001745

1701-
fromerr = ''
1702-
while not fromerr.endswith(checkpoint):
1703-
# if no data received in 5 seconds, the gnuplot process is stuck. This
1704-
# usually happens if the gnuplot process is not in a command mode, but in
1705-
# a data-receiving mode. I'm careful to avoid this situation, but bugs in
1706-
# this module and/or in gnuplot itself can make this happen
1707-
1708-
self._logEvent("Trying to read byte from gnuplot")
1709-
1710-
rlist,wlist,xlist = select.select([self.gnuplotProcess.stderr],[], [],
1711-
None if waitforever else 15)
1712-
1713-
if rlist:
1714-
# read a byte. I'd like to read "as many bytes as are
1715-
# available", but I don't know how to this in a very portable
1716-
# way (I just know there will be windows users complaining if I
1717-
# simply do a non-blocking read). Very little data will be
1718-
# coming in anyway, so doing this a byte at a time is an
1719-
# irrelevant inefficiency
1720-
byte = self.gnuplotProcess.stderr.read(1)
1721-
if len(byte) == 0:
1722-
# Did the child process die?
1723-
returncode = self.gnuplotProcess.poll()
1724-
if returncode is not None:
1725-
# Yep. It died.
1726-
raise Exception(f"gnuplot child died. returncode = {returncode}")
1727-
byte = byte.decode()
1728-
fromerr += byte
1729-
if byte is not None and len(byte):
1730-
self._logEvent("Read byte '{}' ({}) from gnuplot child process".format(byte,
1731-
hex(ord(byte))))
1732-
else:
1733-
self._logEvent("read() returned no data")
1734-
else:
1735-
self._logEvent("Gnuplot read timed out")
1736-
self.checkpoint_stuck = True
1737-
1738-
raise GnuplotlibError(
1739-
r'''Gnuplot process no longer responding. This shouldn't happen... Is your X connection working?''')
1740-
1741-
self._logEvent(f"Read string from gnuplot: '{fromerr}'")
1746+
fromerr = self._receive_until_checkpoint_or_timeout(checkpoint, waitforever)
17421747

17431748
m = re.search(rf'\s*(.*?)\s*{checkpoint}$', fromerr, re.M + re.S)
17441749
if m is None:

0 commit comments

Comments
 (0)