comparison mercurial/ui.py @ 31956:c13ff31818b0

ui: add special-purpose atexit functionality In spite of its longstanding use, Python's built-in atexit code is not suitable for Mercurial's purposes, for several reasons: * Handlers run after application code has finished. * Because of this, the code that runs handlers swallows exceptions (since there's no possible stacktrace to associate errors with). If we're lucky, we'll get something spat out to stderr (if stderr still works), which of course isn't any use in a big deployment where it's important that exceptions get logged and aggregated. * Mercurial's current atexit handlers make unfortunate assumptions about process state (specifically stdio) that, coupled with the above problems, make it impossible to deal with certain categories of error (try "hg status > /dev/full" on a Linux box). * In Python 3, the atexit implementation is completely hidden, so we can't hijack the platform's atexit code to run handlers at a time of our choosing. As a result, here's a perfectly cromulent atexit-like implementation over which we have control. This lets us decide exactly when the handlers run (after each request has completed), and control what the process state is when that occurs (and afterwards).
author Bryan O'Sullivan <bryano@fb.com>
date Tue, 11 Apr 2017 14:54:12 -0700
parents e518192d6bac
children de5c9d0e02ea
comparison
equal deleted inserted replaced
31955:4c2c30bc38b4 31956:c13ff31818b0
137 In most cases, you should use ui.copy() to create a copy of an existing 137 In most cases, you should use ui.copy() to create a copy of an existing
138 ui object. 138 ui object.
139 """ 139 """
140 # _buffers: used for temporary capture of output 140 # _buffers: used for temporary capture of output
141 self._buffers = [] 141 self._buffers = []
142 # _exithandlers: callbacks run at the end of a request
143 self._exithandlers = []
142 # 3-tuple describing how each buffer in the stack behaves. 144 # 3-tuple describing how each buffer in the stack behaves.
143 # Values are (capture stderr, capture subprocesses, apply labels). 145 # Values are (capture stderr, capture subprocesses, apply labels).
144 self._bufferstates = [] 146 self._bufferstates = []
145 # When a buffer is active, defines whether we are expanding labels. 147 # When a buffer is active, defines whether we are expanding labels.
146 # This exists to prevent an extra list lookup. 148 # This exists to prevent an extra list lookup.
161 self._colormode = None 163 self._colormode = None
162 self._terminfoparams = {} 164 self._terminfoparams = {}
163 self._styles = {} 165 self._styles = {}
164 166
165 if src: 167 if src:
168 self._exithandlers = src._exithandlers
166 self.fout = src.fout 169 self.fout = src.fout
167 self.ferr = src.ferr 170 self.ferr = src.ferr
168 self.fin = src.fin 171 self.fin = src.fin
169 self.pageractive = src.pageractive 172 self.pageractive = src.pageractive
170 self._disablepager = src._disablepager 173 self._disablepager = src._disablepager
944 pager.stdin.close() 947 pager.stdin.close()
945 pager.wait() 948 pager.wait()
946 949
947 return True 950 return True
948 951
952 def atexit(self, func, *args, **kwargs):
953 '''register a function to run after dispatching a request
954
955 Handlers do not stay registered across request boundaries.'''
956 self._exithandlers.append((func, args, kwargs))
957 return func
958
949 def interface(self, feature): 959 def interface(self, feature):
950 """what interface to use for interactive console features? 960 """what interface to use for interactive console features?
951 961
952 The interface is controlled by the value of `ui.interface` but also by 962 The interface is controlled by the value of `ui.interface` but also by
953 the value of feature-specific configuration. For example: 963 the value of feature-specific configuration. For example: