tesseract v5.3.3.20231005
gtest_test_utils.Subprocess Class Reference

Public Member Functions

def __init__ (self, command, working_dir=None, capture_stderr=True, env=None)
 

Public Attributes

 output
 
 terminated_by_signal
 
 exited
 
 exit_code
 

Detailed Description

Definition at line 202 of file gtest_test_utils.py.

Constructor & Destructor Documentation

◆ __init__()

def gtest_test_utils.Subprocess.__init__ (   self,
  command,
  working_dir = None,
  capture_stderr = True,
  env = None 
)
Changes into a specified directory, if provided, and executes a command.

Restores the old directory afterwards.

Args:
  command:        The command to run, in the form of sys.argv.
  working_dir:    The directory to change into.
  capture_stderr: Determines whether to capture stderr in the output member
                  or to discard it.
  env:            Dictionary with environment to pass to the subprocess.

Returns:
  An object that represents outcome of the executed process. It has the
  following attributes:
    terminated_by_signal   True if and only if the child process has been
                           terminated by a signal.
    exited                 True if and only if the child process exited
                           normally.
    exit_code              The code with which the child process exited.
    output                 Child process's stdout and stderr output
                           combined in a string.

Definition at line 203 of file gtest_test_utils.py.

203 def __init__(self, command, working_dir=None, capture_stderr=True, env=None):
204 """Changes into a specified directory, if provided, and executes a command.
205
206 Restores the old directory afterwards.
207
208 Args:
209 command: The command to run, in the form of sys.argv.
210 working_dir: The directory to change into.
211 capture_stderr: Determines whether to capture stderr in the output member
212 or to discard it.
213 env: Dictionary with environment to pass to the subprocess.
214
215 Returns:
216 An object that represents outcome of the executed process. It has the
217 following attributes:
218 terminated_by_signal True if and only if the child process has been
219 terminated by a signal.
220 exited True if and only if the child process exited
221 normally.
222 exit_code The code with which the child process exited.
223 output Child process's stdout and stderr output
224 combined in a string.
225 """
226
227 # The subprocess module is the preferrable way of running programs
228 # since it is available and behaves consistently on all platforms,
229 # including Windows. But it is only available starting in python 2.4.
230 # In earlier python versions, we revert to the popen2 module, which is
231 # available in python 2.0 and later but doesn't provide required
232 # functionality (Popen4) under Windows. This allows us to support Mac
233 # OS X 10.4 Tiger, which has python 2.3 installed.
234 if _SUBPROCESS_MODULE_AVAILABLE:
235 if capture_stderr:
236 stderr = subprocess.STDOUT
237 else:
238 stderr = subprocess.PIPE
239
240 p = subprocess.Popen(command,
241 stdout=subprocess.PIPE, stderr=stderr,
242 cwd=working_dir, universal_newlines=True, env=env)
243 # communicate returns a tuple with the file object for the child's
244 # output.
245 self.output = p.communicate()[0]
246 self._return_code = p.returncode
247 else:
248 old_dir = os.getcwd()
249
250 def _ReplaceEnvDict(dest, src):
251 # Changes made by os.environ.clear are not inheritable by child
252 # processes until Python 2.6. To produce inheritable changes we have
253 # to delete environment items with the del statement.
254 for key in dest.keys():
255 del dest[key]
256 dest.update(src)
257
258 # When 'env' is not None, backup the environment variables and replace
259 # them with the passed 'env'. When 'env' is None, we simply use the
260 # current 'os.environ' for compatibility with the subprocess.Popen
261 # semantics used above.
262 if env is not None:
263 old_environ = os.environ.copy()
264 _ReplaceEnvDict(os.environ, env)
265
266 try:
267 if working_dir is not None:
268 os.chdir(working_dir)
269 if capture_stderr:
270 p = popen2.Popen4(command)
271 else:
272 p = popen2.Popen3(command)
273 p.tochild.close()
274 self.output = p.fromchild.read()
275 ret_code = p.wait()
276 finally:
277 os.chdir(old_dir)
278
279 # Restore the old environment variables
280 # if they were replaced.
281 if env is not None:
282 _ReplaceEnvDict(os.environ, old_environ)
283
284 # Converts ret_code to match the semantics of
285 # subprocess.Popen.returncode.
286 if os.WIFSIGNALED(ret_code):
287 self._return_code = -os.WTERMSIG(ret_code)
288 else: # os.WIFEXITED(ret_code) should return True here.
289 self._return_code = os.WEXITSTATUS(ret_code)
290
291 if bool(self._return_code & 0x80000000):
292 self.terminated_by_signal = True
293 self.exited = False
294 else:
295 self.terminated_by_signal = False
296 self.exited = True
297 self.exit_code = self._return_code
298
299

Member Data Documentation

◆ exit_code

gtest_test_utils.Subprocess.exit_code

Definition at line 297 of file gtest_test_utils.py.

◆ exited

gtest_test_utils.Subprocess.exited

Definition at line 293 of file gtest_test_utils.py.

◆ output

gtest_test_utils.Subprocess.output

Definition at line 245 of file gtest_test_utils.py.

◆ terminated_by_signal

gtest_test_utils.Subprocess.terminated_by_signal

Definition at line 292 of file gtest_test_utils.py.


The documentation for this class was generated from the following file: