From d368971749e1dd5cfa08e4859b5df579b7aff633 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 10 Mar 2019 21:27:15 +0000 Subject: [PATCH] core: introduce mitogen.core.pipe() It's used in later commit. This is an os.pipe() wrapper that traps the file descriptors in a file object, to ensure leaked objects will eventually be collected, and a central place exists to track open/closed status. --- mitogen/core.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mitogen/core.py b/mitogen/core.py index 76baf4d3..4e7b347b 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -615,6 +615,20 @@ def import_module(modname): return __import__(modname, None, None, ['']) +def pipe(): + """ + Create a UNIX pipe pair using :func:`os.pipe`, wrapping the returned + descriptors in Python file objects in order to manage their lifetime and + ensure they are closed when their last reference is discarded and they have + not been closed explicitly. + """ + rfd, wfd = os.pipe() + return ( + os.fdopen(rfd, 'rb', 0), + os.fdopen(wfd, 'wb', 0) + ) + + def iter_split(buf, delim, func): """ Invoke `func(s)` for each `delim`-delimited chunk in the potentially large