util¶
-
class
invoke.util.ExceptionHandlingThread(**kwargs)¶ Thread handler making it easier for parent to handle thread exceptions.
Based in part on Fabric 1’s ThreadHandler. See also Fabric GH issue #204.
When used directly, can be used in place of a regular
threading.Thread. If subclassed, the subclass must do one of:- supply
targetto__init__ - define
_run()instead ofrun()
This is because this thread’s entire point is to wrap behavior around the thread’s execution; subclasses could not redefine
run()without breaking that functionality.-
__init__(**kwargs)¶ Create a new exception-handling thread instance.
Takes all regular
threading.Threadkeyword arguments, via**kwargsfor easier display of thread identity when raising captured exceptions.
-
exception()¶ If an exception occurred, return an
ExceptionWrapperaround it.Returns: An ExceptionWrappermanaging the result ofsys.exc_info, if an exception was raised during thread execution. If no exception occurred, returnsNoneinstead.
-
is_dead¶ Returns
Trueif not alive and has a stored exception.Used to detect threads that have excepted & shut down.
- supply
-
invoke.util.encode_output(string, encoding)¶ Transform string-like object
stringinto bytes viaencoding.Returns: A byte-string ( stron Python 2,byteson Python 3.)
-
invoke.util.has_fileno(stream)¶ Cleanly determine whether
streamhas a useful.fileno().Note
This function helps determine if a given file-like object can be used with various terminal-oriented modules and functions such as
select,termios, andtty. For most of those, a fileno is all that is required; they’ll function even ifstream.isatty()isFalse.Parameters: stream – A file-like object. Returns: Trueifstream.fileno()returns an integer,Falseotherwise (this includes whenstreamlacks afilenomethod).
-
invoke.util.isatty(stream)¶ Cleanly determine whether
streamis a TTY.Specifically, first try calling
stream.isatty(), and if that fails (e.g. due to lacking the method entirely) fallback toos.isatty.Note
Most of the time, we don’t actually care about true TTY-ness, but merely whether the stream seems to have a fileno (per
has_fileno). However, in some cases (notably the use ofpty.forkto present a local pseudoterminal) we need to tell if a given stream has a valid fileno but isn’t tied to an actual terminal. Thus, this function.Parameters: stream – A file-like object. Returns: A boolean depending on the result of calling .isatty()and/oros.isatty.
-
invoke.util.sort_names(names)¶ Sort task
namesby nesting depth & then as regular strings.
-
class
invoke.util.ExceptionWrapper(kwargs, type, value, traceback)¶ A namedtuple wrapping a thread-borne exception & that thread’s arguments. Mostly used as an intermediate between
ExceptionHandlingThread(which preserves initial exceptions) andThreadException(which holds 1..N such exceptions, as typically multiple threads are involved.)