2323import subprocess
2424import sys
2525import time
26+ import warnings
27+
2628
2729# The maximum amount of time to wait for the intermediate subprocess.
2830_WAIT_TIMEOUT = 10
2931_THIS_FILE = os .path .realpath (__file__ )
3032
33+
3134if sys .version_info [0 ] < 3 :
3235 def _popen_wait (popen , timeout ):
3336 """Implement wait timeout support for Python 2."""
@@ -66,7 +69,9 @@ def _silence_resource_warning(popen):
6669 # "ResourceWarning: subprocess XXX is still running".
6770 # See https://bugs.python.org/issue38890 and
6871 # https://bugs.python.org/issue26741.
69- popen .returncode = 0
72+ # popen is None when mongocryptd spawning fails
73+ if popen is not None :
74+ popen .returncode = 0
7075
7176
7277if sys .platform == 'win32' :
@@ -75,12 +80,17 @@ def _silence_resource_warning(popen):
7580
7681 def _spawn_daemon (args ):
7782 """Spawn a daemon process (Windows)."""
78- with open (os .devnull , 'r+b' ) as devnull :
79- popen = subprocess .Popen (
80- args ,
81- creationflags = _DETACHED_PROCESS ,
82- stdin = devnull , stderr = devnull , stdout = devnull )
83- _silence_resource_warning (popen )
83+ try :
84+ with open (os .devnull , 'r+b' ) as devnull :
85+ popen = subprocess .Popen (
86+ args ,
87+ creationflags = _DETACHED_PROCESS ,
88+ stdin = devnull , stderr = devnull , stdout = devnull )
89+ _silence_resource_warning (popen )
90+ except FileNotFoundError as exc :
91+ warnings .warn ('Failed to start %s: is it on your $PATH?\n '
92+ 'Original exception: %s' % (args [0 ], exc ),
93+ RuntimeWarning , stacklevel = 2 )
8494else :
8595 # On Unix we spawn the daemon process with a double Popen.
8696 # 1) The first Popen runs this file as a Python script using the current
@@ -95,12 +105,16 @@ def _spawn_daemon(args):
95105 # we spawn the mongocryptd daemon process.
96106 def _spawn (args ):
97107 """Spawn the process and silence stdout/stderr."""
98- with open (os .devnull , 'r+b' ) as devnull :
99- return subprocess .Popen (
100- args ,
101- close_fds = True ,
102- stdin = devnull , stderr = devnull , stdout = devnull )
103-
108+ try :
109+ with open (os .devnull , 'r+b' ) as devnull :
110+ return subprocess .Popen (
111+ args ,
112+ close_fds = True ,
113+ stdin = devnull , stderr = devnull , stdout = devnull )
114+ except FileNotFoundError as exc :
115+ warnings .warn ('Failed to start %s: is it on your $PATH?\n '
116+ 'Original exception: %s' % (args [0 ], exc ),
117+ RuntimeWarning , stacklevel = 2 )
104118
105119 def _spawn_daemon_double_popen (args ):
106120 """Spawn a daemon process using a double subprocess.Popen."""
0 commit comments