[Python-Dev] Best Python API for exposing posix_spawn (original) (raw)

Brett Cannon brett at python.org
Tue Jan 9 12:56:15 EST 2018


On Tue, 9 Jan 2018 at 02:42 Nick Coghlan <ncoghlan at gmail.com> wrote:

On 9 January 2018 at 20:01, Antoine Pitrou <solipsis at pitrou.net> wrote: > On Mon, 08 Jan 2018 09:11:38 +0000 > Pablo Galindo Salgado <pablogsal at gmail.com> wrote: >> Hi, >> >> I'm currently working on exposing posixspawn in the posix module (and by >> extension in the os module). You can find the initial implementation in >> this PR: >> >> https://github.com/python/cpython/pull/5109 >> >> As pointed out by Gregory P. Smith, some changes are needed in the way the >> fileactions arguments is passed from Python. For context, posixspawn has >> the following declaration: >> >> int posixspawn(pidt *pid, const char *path, >> const posixspawnfileactionst *fileactions, >> const posixspawnattrt *attrp, >> char *const argv[], char *const envp[]); >> >> Here, fileactions is an object that represents a list of file actions >> (open, close or dup2) that is populated using helper functions on the C API. >> >> The question is: what is the best way to deal with this argument? > > How about a list of tuples like: > [(os.SPAWNOPEN, 4, 'README.txt', os.ORDONLY, 0), > (os.SPAWNCLOSE, 5), > (os.SPAWNDUP2, 3, 6), > ] > > I don't expect this API to be invoked directly by user code so it > doesn't have to be extremely pretty.

I'll note that one advantage of this approach is that it ties in well with how the C API is going to have to deal with it anyway: a switch statement dispatching on the first value, and then passing the remaining arguments to the corresponding posixfileactions API.

Plus the posix module tends to stick reasonably close to the C API anyway since it's such a thin wrapper.

Wrapping it all up in a more Pythonic self-validating API would then be the responsibility of the subprocess module (in the standard library), or third party modules.

+1 from me on Antoine's suggestion. Might as well keep it simple. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20180109/4f894088/attachment.html>



More information about the Python-Dev mailing list