root/trunk/twisted/internet/_baseprocess.py

Revision 25366, 1.8 KB (checked in by exarkun, 22 months ago)

Merge processexited-failure-3521

Author: exarkun
Reviewer: radix
Fixes: #3521

Fix the implementation of IReactorProcess so that it passes a Failure to
IProcessProtocol.processExited instead of passing an exception.

Line 
1# -*- test-case-name: twisted.test.test_process -*-
2# Copyright (c) 2008 Twisted Matrix Laboratories.
3# See LICENSE for details.
4
5"""
6Cross-platform process-related functionality used by different
7L{IReactorProcess} implementations.
8"""
9
10from twisted.python.reflect import qual
11from twisted.python.deprecate import getWarningMethod
12from twisted.python.failure import Failure
13from twisted.python.log import err
14from twisted.persisted.styles import Ephemeral
15
16_missingProcessExited = ("Since Twisted 8.2, IProcessProtocol.processExited "
17                         "is required.  %s must implement it.")
18
19class BaseProcess(Ephemeral):
20    pid = None
21    status = None
22    lostProcess = 0
23    proto = None
24
25    def __init__(self, protocol):
26        self.proto = protocol
27
28
29    def _callProcessExited(self, reason):
30        default = object()
31        processExited = getattr(self.proto, 'processExited', default)
32        if processExited is default:
33            getWarningMethod()(
34                _missingProcessExited % (qual(self.proto.__class__),),
35                DeprecationWarning, stacklevel=0)
36        else:
37            processExited(Failure(reason))
38
39
40    def processEnded(self, status):
41        """
42        This is called when the child terminates.
43        """
44        self.status = status
45        self.lostProcess += 1
46        self.pid = None
47        self._callProcessExited(self._getReason(status))
48        self.maybeCallProcessEnded()
49
50
51    def maybeCallProcessEnded(self):
52        """
53        Call processEnded on protocol after final cleanup.
54        """
55        if self.proto is not None:
56            reason = self._getReason(self.status)
57            proto = self.proto
58            self.proto = None
59            try:
60                proto.processEnded(Failure(reason))
61            except:
62                err(None, "unexpected error in processEnded")
Note: See TracBrowser for help on using the browser.