Interactive scripts (back on topic for once) [was Re: The "loop and a half"]

Grant Edwards grant.b.edwards at gmail.com
Fri Oct 6 13:54:02 EDT 2017


On 2017-10-06, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, Oct 7, 2017 at 4:05 AM, Grant Edwards <grant.b.edwards at gmail.com> wrote:
>> On 2017-10-06, Thomas Jollans <tjol at tjol.eu> wrote:
>>
>>> Seriously? sys.stdin can be None? That's terrifying.
>>
>> Why?
>>
>> Unix daemons usually run with no stdin, stderr, or stdout.
>>
>> And yes, people do write Unix daemons in Python.
>
> Hmm, but usually I would expect them still to HAVE those streams,
> they're just connected to /dev/null or something. I don't think they
> would actually fail to exist, would they?

That's a good point.  The basic approach is to fork and then just
close all file descriptors.  Since what is normally the
std{in,out,err} descriptors can be re-used, you could end up with some
naive code (e.g. something in a library) writing to a file/socket that
it shouldn't be writing to.

The defensive approach is to open /dev/null and use dup2() to make fd
0 1 2 refer to that.  In that case, they do exist, but stdin always
reads empty and stdout stderr write data is discarded.

That's probably the more common approach.

-- 
Grant Edwards               grant.b.edwards        Yow! Could I have a drug
                                  at               overdose?
                              gmail.com            




More information about the Python-list mailing list