[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-ideas
Subject:    Re: [Python-ideas] allow overriding files used for the input builtin
From:       Wren Turkal <w00t () fb ! com>
Date:       2017-09-29 18:20:31
Message-ID: BN6PR15MB1859400E9030AEE5DD52C871B27E0 () BN6PR15MB1859 ! namprd15 ! prod ! outlook ! com
[Download RAW message or body]

[Attachment #2 (text/plain)]

So, I was just thinking, maybe we don't want an errfile arg, but an arg that is a \
sequence of file objects that need to be flushed before showing the prompt. That's \
certainly more complicated, but it seems more general if we want to cover this case.


Thoughts?


wt

________________________________
From: Python-ideas <python-ideas-bounces+w00t=fb.com@python.org> on behalf of Wren \
                Turkal <w00t@fb.com>
Sent: Friday, September 29, 2017 9:53:01 AM
To: Amit Green
Cc: python-ideas@python.org
Subject: [Potential Spoof] Re: [Python-ideas] allow overriding files used for the \
input builtin


Oh, I thought that stderr was unbuffered. Like the following C program:


#include <stdio.h>

int main() {
  fprintf(stdout, "stdout0");
  fprintf(stderr, "stderr0");
  fprintf(stdout, "stdout1");
  return 0;
}


This outputs:

stderr0stdout0stdout1


Turns out fprintf in glibc will also implicitly flush at newlines also. So, the \
following program:

#include <stdio.h>

int main() {
  fprintf(stdout, "stdout0\nstdout12");
  fprintf(stderr, "stderr0");
  fprintf(stdout, "stdout1");
  return 0;
}


outputs:

stdout0
stderr0stdout12stdout1


This is in line with python. The following program:

import sys

sys.stdout.write('stdout0\nstdout01')
sys.stderr.write('stderr0')
sys.stdout.write('stdout1')


outputs:

stdout0
stderr0stdout01stdout1

While the following program:

import sys

sys.stdout.write('stdout0')
sys.stderr.write('stderr0')
sys.stdout.write('stdout1')


outputs:

stderr0stdout0stdout1


So, I guess that the errfile is explicitly flushed due to the fact that stdout may or \
may not be flushed during output.


So, I have 2 questions: (1) do we need to keep the errfile param to make this useful \
(I think we do), and if #1 is yes, (2) what is an appropriate way to doc this?


wt

________________________________
From: Amit Green <amit.mixie@gmail.com>
Sent: Friday, September 29, 2017 9:17:39 AM
To: Wren Turkal
Cc: Steven D'Aprano; python-ideas@python.org
Subject: Re: [Python-ideas] allow overriding files used for the input builtin

Hmm, very good point.

Flushing standard error is essential:


  *   This is because output of standard output & standard error are often redirected \
to the same file -- frequently the terminal -- and its important to flush one of them \
before output to the other (since these are typically line buffered you only see this \
if you output partial lines).

  *   Here is an example of not flushing & the mess it causes:

> > > import sys
> > > sys.stdout.write('hi'); sys.stderr.write(' HMM '); sys.stdout.write('there\n')
 HMM hithere

  *   Here is the same example with flushing:

> > > sys.stdout.write('hi'); sys.stdout.flush(); sys.stderr.write(' HMM '); \
> > > sys.stderr.flush(); sys.stdout.write('there\n')
hi HMM there

In fact, I think you need to improve your interface and documentation:

  *   If either of the parameters 'outfile', or 'errfile' are passed in & not the \
                'none' value then the following happens:
     *   Both sys.stdout & sys.stderr are flushed (in that order) before being \
                redirected.  (NOTE: Both are flushed, even if only one is \
                redirected).
     *   Before restoring sys.stdout & sys.stderr; then outfile & errfile are flushed \
(if both have been used then they are flushed in that order).

You would of course need to write the documentation clearer than I did above (writing \
documentation well is not my skill) -- I wrote it to convey exactly what has to \
happen.

On Fri, Sep 29, 2017 at 12:01 PM, Wren Turkal <w00t@fb.com<mailto:w00t@fb.com>> \
wrote:

Steven and Amit,


I originally configured the mailing list for digest delivery and can't reply directly \
to his message. However, I now seen it, I will update the PR with the suggested name \
changes as soon as my "make test" finishes. FWIW, I've changed to direct message \
delivery.


With regard to ferr (the corresponding variable in the builtin_input c function \
before the change), I saw this code:


    /* First of all, flush stderr */
    tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
    if (tmp == NULL)
        PyErr_Clear();
    else
        Py_DECREF(tmp);


And I assumed that it was important to be able to override a stderr as a result.


I think there are few options here to resolve that:

  1.  Remove the errfile parameter and just do what happened before.
  2.  Remove the errfile and the above code (FWIW, I am not sure I understand the \
importance of flushing stderr before taking input).  3.  Document the explicit \
purpose of the errfile. Amit, you'd responded with something about this (again digest \
reply, sorry). I am not sure how to concisely do that given Amit's descriptions. 😊

I am honestly leaning toward 2 unless I can figure out why the flushing of stderr is \
actually needed.


wt


________________________________
From: Amit Green <amit.mixie@gmail.com<mailto:amit.mixie@gmail.com>>
Sent: Friday, September 29, 2017 8:13:21 AM

To: Wren Turkal
Cc: python-ideas@python.org<mailto:python-ideas@python.org>
Subject: Re: [Python-ideas] allow overriding files used for the input builtin

Yes, infile, outfile & errfile would be consistent with python naming convention \
(also mentioned by Steven D'Aprano above)

One of python's greatest strength is its library, the consistency of the library, and \
how well documented the library is (in fact, I think the library is a greater \
strength than even the very nice syntax of python in general).

By "consistency of the library" I mean: functions do pretty much what you expect, \
they use consistent error mechanism & the documentation pretty much accurately \
documents what the function does -- especially as to showing its results & how it \
handles errors.

Regarding this though, this then brings up the question (above from Steven D'Aprano) \
-- what would the the "errfile" parameter do?

  *   As a general principle of consistency python library functions, and python \
                itself, do not output to errfile, but instead throw errors.
  *   (There are very minor exceptions such as exceptions thrown in __del__ \
functions; which are caught by python & then printed to standard error).

I would thus think you don't want the errfile parameter -- unless it would be for \
catching these __del__ method that get triggered by input failing (for example your \
'infile' parameter when called, allocated an object, which gets deallocated & throws \
an exception inside of its __del__ method).

If this is the purpose, then (back to 'how well documented the library is') -- it \
should be documented this is the purpose of the "errfile" parameter ;-)

[A secondary reason you might want to redirect "errfile" is that the passed in input \
or output file's, themselves do output to standard error ...]


On Fri, Sep 29, 2017 at 10:50 AM, Wren Turkal <w00t@fb.com<mailto:w00t@fb.com>> \
wrote:

I am happy to rename the args. What do you think about infile, outfile, and errfile?


FWIW, I did consider "in", "out", and "err", but "in" is a keyword, and I didn't \
think those quite captured the full meaning.


wt

________________________________
From: Amit Green <amit.mixie@gmail.com<mailto:amit.mixie@gmail.com>>
Sent: Thursday, September 28, 2017 11:18:18 PM
To: Wren Turkal
Cc: python-ideas@python.org<mailto:python-ideas@python.org>
Subject: Re: [Python-ideas] allow overriding files used for the input builtin

I'm fine with the idea in general of extra keyword parameters to the input function.

A few points:

Your example code, needs try/catch to match what the input with parameters does -- \
and yes, its way nicer to be able to use it the example you have shown than play \
games with try/catch (Personally I also refuse to ever change sys.stdin, or \
sys.stdout, as I consider that a bad coding style).

Mostly though I would like to ask, please do not name keyword arguments with names \
like 'fin' & 'fout'.  This is almost unreadable and make's code almost indecipherable \
to others the first time they see the function & its keyword arguments (First \
impressions are very important).

Both a function name & its keyword parameters need to be as understandable as \
possible when a user encounters them for the first time.

On Fri, Sep 29, 2017 at 1:53 AM, Wren Turkal <w00t@fb.com<mailto:w00t@fb.com>> wrote:

Hi there,


I have posted an idea for improvement with a PR of an implementation to \
https://bugs.python.org/issue31603<https://urldefense.proofpoint.com/v2/url?u=https-3A \
__bugs.python.org_issue31603&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=OAN5uLR4JWXbIgcvx315Z \
w&m=K1-OD0dhslOiAqYQdEyr0Oppl8TaroBPvXr8h_Z8XxM&s=sjDmBcI00MbWPPjuLMPlBnZHoFZOHoTxaCo2KCYlEd4&e=>.



The basic idea is to add fin, fout, and ferr file object parameters and default to \
using what is used today when the args are not specified. I believe this would be \
useful to allow captures input and send output to specific files when using input. \
The input builtin has some logic to use readline if it's available. It would be nice \
to be able to use this same logic no matter what files are being used for \
input/output.


This is meant to turn code like the following:

orig_stdin = sys.stdin

orig_stdout = sys.stdout

with open('/dev/tty', 'r+') as f:

    sys.stdin = f

    sys.stdout = f

    name = input('Name? ')

sys.stdin = orig_stdin

sys.stdout = orig_stdout

print(name)


into something more like this:

with open('/dev/tty', 'r+') as f:

    name = input('Name? ', fin=f, fout=f)

print(name)


It's nice that it makes the assignment to a global variable to change the file used \
for input/output to no longer be needed.


I had this idea the other day, and I realized that it would be super easy to \
implement, so I went ahead the threw up a PR also.


Would love to see if anyone else is interested in this. I think it's pretty cool that \
the core logic really didn't need to be changed other than plumbing in the new args.


FWIW, this change introduces no regressions and adds a few more tests to test the new \
functionality. Honestly, I think this functionality could probably be used to \
simplify some of the other tests as well, but I wanted to gauge what folks thought of \
the change before going farther.


Wren Turkal

Existential Production Engineer of the Ages

Facebook, Inc.

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org<mailto:Python-ideas@python.org>
https://mail.python.org/mailman/listinfo/python-ideas<https://urldefense.proofpoint.co \
m/v2/url?u=https-3A__mail.python.org_mailman_listinfo_python-2Dideas&d=DwMFaQ&c=5VD0RT \
tNlTh3ycd41b3MUw&r=OAN5uLR4JWXbIgcvx315Zw&m=K1-OD0dhslOiAqYQdEyr0Oppl8TaroBPvXr8h_Z8XxM&s=dvbtuL0kPe5UxakqosCnQQCIxlKprMP6JTqx4ZLXx4g&e=>
 Code of Conduct: http://python.org/psf/codeofconduct/<https://urldefense.proofpoint.c \
om/v2/url?u=http-3A__python.org_psf_codeofconduct_&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r \
=OAN5uLR4JWXbIgcvx315Zw&m=K1-OD0dhslOiAqYQdEyr0Oppl8TaroBPvXr8h_Z8XxM&s=px67QeYtdnaejiTP9VcY5IuSiJ4pk3XEXcbDzZWbsnI&e=>



[Attachment #3 (text/html)]

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} \
--></style> <div id="divtagdefaultwrapper" \
style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" \
dir="ltr"> <p>So, I was just thinking, maybe we don't want an errfile arg, but an arg \
that is a sequence of file objects that need to be flushed before showing the prompt. \
That's certainly more complicated, but it seems more general if we want to cover this \
case.</p> <p><br>
</p>
<p>Thoughts?</p>
<p><br>
</p>
<p>wt</p>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" \
style="font-size:11pt" color="#000000"><b>From:</b> Python-ideas \
&lt;python-ideas-bounces&#43;w00t=fb.com@python.org&gt; on behalf of Wren Turkal \
&lt;w00t@fb.com&gt;<br> <b>Sent:</b> Friday, September 29, 2017 9:53:01 AM<br>
<b>To:</b> Amit Green<br>
<b>Cc:</b> python-ideas@python.org<br>
<b>Subject:</b> [Potential Spoof] Re: [Python-ideas] allow overriding files used for \
the input builtin</font> <div>&nbsp;</div>
</div>
<div><style type="text/css" style="display:none;"><!-- P \
{margin-top:0;margin-bottom:0;} --></style> <div id="divtagdefaultwrapper" \
style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" \
dir="ltr"> <p>Oh, I thought that stderr was unbuffered. Like the following C \
program:</p> <p><br>
</p>
<p></p>
<div><span style="font-family: Consolas, Courier, monospace;">#include \
&lt;stdio.h&gt;</span></div> <div><br>
</div>
<div><span style="font-family: Consolas, Courier, monospace;">int main() \
{</span></div> <div><span style="font-family: Consolas, Courier, monospace;">&nbsp; \
fprintf(stdout, &quot;stdout0&quot;);</span></div> <div><span style="font-family: \
Consolas, Courier, monospace;">&nbsp; fprintf(stderr, \
&quot;stderr0&quot;);</span></div> <div><span style="font-family: Consolas, Courier, \
monospace;">&nbsp; fprintf(stdout, &quot;stdout1&quot;);</span></div> <div><span \
style="font-family: Consolas, Courier, monospace;">&nbsp; return 0;</span></div> \
<div><span style="font-family: Consolas, Courier, monospace;">}</span></div> <br>
<p></p>
<p>This outputs:</p>
<p><span style="font-family: Consolas, Courier, \
monospace;">stderr0stdout0stdout1</span></p> <p><br>
</p>
<p>Turns out fprintf in glibc&nbsp;will also implicitly flush&nbsp;at newlines also. \
So, the following program:</p> <p></p>
<div><span style="font-family: Consolas, Courier, monospace;">#include \
&lt;stdio.h&gt;</span></div> <div><br>
</div>
<div><span style="font-family: Consolas, Courier, monospace;">int main() \
{</span></div> <div><span style="font-family: Consolas, Courier, monospace;">&nbsp; \
fprintf(stdout, &quot;stdout0\nstdout12&quot;);</span></div> <div><span \
style="font-family: Consolas, Courier, monospace;">&nbsp; fprintf(stderr, \
&quot;stderr0&quot;);</span></div> <div><span style="font-family: Consolas, Courier, \
monospace;">&nbsp; fprintf(stdout, &quot;stdout1&quot;);</span></div> <div><span \
style="font-family: Consolas, Courier, monospace;">&nbsp; return 0;</span></div> \
<div><span style="font-family: Consolas, Courier, monospace;">}</span></div> <br>
<p></p>
<p>outputs:</p>
<p></p>
<div><span style="font-family: Consolas, Courier, monospace;">stdout0</span></div>
<div><span style="font-family: Consolas, Courier, \
monospace;">stderr0stdout12stdout1</span></div> <br>
<p></p>
<p>This is in line with python. The following program:</p>
<p></p>
<div><span style="font-family: Consolas, Courier, monospace;">import sys</span></div>
<div><br>
</div>
<div><span style="font-family: Consolas, Courier, \
monospace;">sys.stdout.write('stdout0\nstdout01')</span></div> <div><span \
style="font-family: Consolas, Courier, \
monospace;">sys.stderr.write('stderr0')</span></div> <div><span style="font-family: \
Consolas, Courier, monospace;">sys.stdout.write('stdout1')</span></div> <br>
<p></p>
<p>outputs:</p>
<p></p>
<div style="font-family: Calibri, Helvetica, sans-serif, Helvetica, EmojiFont, \
&quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, NotoColorEmoji, \
&quot;Segoe UI Symbol&quot;, &quot;Android Emoji&quot;, EmojiSymbols; font-size: \
16px;"> <span style="font-family: Consolas, Courier, monospace;">stdout0</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif, Helvetica, EmojiFont, \
&quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, NotoColorEmoji, \
&quot;Segoe UI Symbol&quot;, &quot;Android Emoji&quot;, EmojiSymbols; font-size: \
16px;"> <span style="font-family: Consolas, Courier, \
monospace;">stderr0stdout01stdout1</span></div> <div><span style="font-family: \
Consolas, Courier, monospace;"><br> </span></div>
While the following program:
<p></p>
<p></p>
<div>import sys</div>
<div><br>
</div>
<div>sys.stdout.write('stdout0')</div>
<div>sys.stderr.write('stderr0')</div>
<div>sys.stdout.write('stdout1')</div>
<br>
<p></p>
<p>outputs:</p>
<p><span>stderr0stdout0stdout1</span><br>
</p>
<p><span><br>
</span></p>
<p><span>So, I guess that the errfile is explicitly flushed due to the fact that \
stdout may or may not be flushed during output.</span></p> <p><span><br>
</span></p>
<p><span>So, I have 2 questions:&nbsp;(1)&nbsp;do we&nbsp;need to keep the errfile \
param to make this useful (I think we do), and if #1 is yes, (2) what is an \
appropriate way to doc this?</span></p> <p></p>
<p><br>
</p>
<p>wt</p>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" \
style="font-size:11pt" color="#000000"><b>From:</b> Amit Green \
&lt;amit.mixie@gmail.com&gt;<br> <b>Sent:</b> Friday, September 29, 2017 9:17:39 \
AM<br> <b>To:</b> Wren Turkal<br>
<b>Cc:</b> Steven D'Aprano; python-ideas@python.org<br>
<b>Subject:</b> Re: [Python-ideas] allow overriding files used for the input \
builtin</font> <div>&nbsp;</div>
</div>
<div>
<div dir="ltr">
<div>Hmm, very good point.<br>
<br>
</div>
Flushing standard error is essential:<br>
<br>
<ul>
<li>This is because output of standard output &amp; standard error are often \
redirected to the same file -- frequently the terminal -- and its important to flush \
one of them before output to the other (since these are typically line buffered you \
only see this if  you output partial lines).</li></ul>
<ul>
<li>Here is an example of not flushing &amp; the mess it causes:</li></ul>
<p style="margin-left:80px">&gt;&gt;&gt; import sys<br>
&gt;&gt;&gt; sys.stdout.write('hi'); sys.stderr.write(' HMM '); \
sys.stdout.write('there\n')<br> &nbsp;HMM hithere<br>
</p>
<ul>
<li>Here is the same example with flushing:</li></ul>
<p style="margin-left:80px">&gt;&gt;&gt; sys.stdout.write('hi'); sys.stdout.flush(); \
sys.stderr.write(' HMM '); sys.stderr.flush(); sys.stdout.write('there\n')<br> hi HMM \
there<br> </p>
<p>In fact, I think you need to improve your interface and documentation:</p>
<ul>
<li>If either of the parameters 'outfile', or 'errfile' are passed in &amp; not the \
'none' value then the following happens: <ul>
<li>Both sys.stdout &amp; sys.stderr are flushed (in that order) before being \
redirected.&nbsp; (NOTE: Both are flushed, even if only one is redirected).<br> \
</li><li>Before restoring sys.stdout &amp; sys.stderr; then outfile &amp; errfile are \
flushed (if both have been used then they are flushed in that order).</li></ul> \
</li></ul> <p>You would of course need to write the documentation clearer than I did \
above (writing documentation well is not my skill) -- I wrote it to convey exactly \
what has to happen.<br> </p>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Sep 29, 2017 at 12:01 PM, Wren Turkal <span \
dir="ltr"> &lt;<a href="mailto:w00t@fb.com" \
target="_blank">w00t@fb.com</a>&gt;</span> wrote:<br> <blockquote class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div \
dir="ltr"> <div id="m_-5979384650938487007divtagdefaultwrapper" dir="ltr">
<div id="m_-5979384650938487007divtagdefaultwrapper" dir="ltr">
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> Steven and \
Amit,</p> <p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <br>
</p>
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> I \
originally&nbsp;configured the mailing list for digest delivery and can't reply \
directly to his message.&nbsp;However, I now seen it, I will update the PR with the \
suggested name changes as soon as my &quot;make test&quot; finishes. FWIW,<span \
style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple Color \
Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:16px">&nbsp;I've  \
changed to direct message delivery.</span></p> <p \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <span \
style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple Color \
Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:16px"><br> </span></p>
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> With regard to \
ferr (the corresponding variable in the&nbsp;builtin_input c function before the \
change), I saw this code:</p> <p \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <br>
</p>
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> </p>
<div style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <span \
style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; /* First of all, flush \
stderr */</span></div> <div \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <span \
style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; tmp = \
_PyObject_CallMethodId(ferr, &amp;PyId_flush, NULL);</span></div> <div \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <span \
style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; if (tmp == \
NULL)</span></div> <div \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <span \
style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; &nbsp; &nbsp; \
PyErr_Clear();</span></div> <div \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <span \
style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; else</span></div> <div \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <span \
style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; &nbsp; &nbsp; \
Py_DECREF(tmp);</span></div> <br>
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> </p>
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> And&nbsp;I \
assumed that it was important to be able to override a stderr as a result.</p> <p \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <br>
</p>
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> I think there \
are few options here to resolve that:</p> <p></p>
<ol style="margin-bottom:0px;margin-top:0px">
<li>Remove the errfile parameter and just do what happened before.</li><li>Remove the \
errfile and the above code (FWIW, I am not sure I understand the importance of \
flushing stderr before taking input).</li><li>Document the explicit purpose of the \
errfile. Amit, you'd responded with something about this (again digest reply, \
sorry).&nbsp;I am not sure how to concisely do that given Amit's descriptions. \
<span>😊</span></li></ol> <div><br>
</div>
<div>I am honestly leaning toward 2 unless I can figure out why the&nbsp;flushing of \
stderr is actually needed.</div> <p></p>
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <br>
</p>
<p style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <span \
style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple Color \
Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:16px">wt</span></p> <p \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <span \
style="font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple Color \
Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:16px"><br> </span></p>
</div>
<hr style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android \
Emoji&quot;,EmojiSymbols;font-size:12pt;display:inline-block;width:98%"> <div \
id="m_-5979384650938487007divRplyFwdMsg" dir="ltr" \
style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <font \
style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> Amit \
Green &lt;<a href="mailto:amit.mixie@gmail.com" \
target="_blank">amit.mixie@gmail.com</a>&gt;<br> <b>Sent:</b> Friday, September 29, \
2017 8:13:21 AM <div>
<div class="h5"><br>
<b>To:</b> Wren Turkal<br>
<b>Cc:</b> <a href="mailto:python-ideas@python.org" \
target="_blank">python-ideas@python.org</a><br> <b>Subject:</b> Re: [Python-ideas] \
allow overriding files used for the input builtin</div> </div>
</font>
<div>&nbsp;</div>
</div>
<div>
<div class="h5">
<div style="color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols;font-size:12pt"> <div dir="ltr">
<div>
<div>
<div>Yes, infile, outfile &amp; errfile would be consistent with python naming \
convention (also mentioned by Steven D'Aprano above)<br> <br>
</div>
One of python's greatest strength is its library, the consistency of the library, and \
how well documented the library is (in fact, I think the library is a greater \
strength than even the very nice syntax of python in general).<br> <br>
</div>
By &quot;consistency of the library&quot; I mean: functions do pretty much what you \
expect, they use consistent error mechanism &amp; the documentation pretty much \
accurately documents what the function does -- especially as to showing its results \
&amp; how it handles errors.<br> <br>
</div>
Regarding this though, this then brings up the question (above from Steven D'Aprano) \
-- what would the the &quot;errfile&quot; parameter do?<br> <ul>
<li>As a general principle of consistency python library functions, and python \
itself, do not output to errfile, but instead throw errors.</li><li>(There are very \
minor exceptions such as exceptions thrown in __del__ functions; which are caught by \
python &amp; then printed to standard error).</li></ul> <p>I would thus think you \
don't want the errfile parameter -- unless it would be for catching these __del__ \
method that get triggered by input failing (for example your 'infile' parameter when \
called, allocated an object, which gets deallocated &amp; throws an  exception inside \
of its __del__ method).</p> <p>If this is the purpose, then (back to 'how well \
documented the library is') -- it should be documented this is the purpose of the \
&quot;errfile&quot; parameter ;-)</p> <p>[A secondary reason you might want to \
redirect &quot;errfile&quot; is that the passed in input or output file's, themselves \
do output to standard error ...]<br> </p>
<p><br>
</p>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Sep 29, 2017 at 10:50 AM, Wren Turkal <span \
dir="ltr"> &lt;<a href="mailto:w00t@fb.com" \
target="_blank">w00t@fb.com</a>&gt;</span> wrote:<br> <blockquote class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div>
<div id="m_-5979384650938487007m_-348553151314392222divtagdefaultwrapper" \
style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols" dir="ltr"> <p>I am happy to \
rename the args. What do you think about infile, outfile, and errfile?</p> <p><br>
</p>
<p>FWIW, I did consider &quot;in&quot;, &quot;out&quot;, and &quot;err&quot;, but \
&quot;in&quot; is a keyword, and I didn't think those quite captured the full \
meaning.</p> <p><br>
</p>
<p>wt</p>
</div>
<hr style="display:inline-block;width:98%">
<div id="m_-5979384650938487007m_-348553151314392222divRplyFwdMsg" dir="ltr"><font \
style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> Amit \
Green &lt;<a href="mailto:amit.mixie@gmail.com" \
target="_blank">amit.mixie@gmail.com</a>&gt;<br> <b>Sent:</b> Thursday, September 28, \
2017 11:18:18 PM<br> <b>To:</b> Wren Turkal<br>
<b>Cc:</b> <a href="mailto:python-ideas@python.org" \
target="_blank">python-ideas@python.org</a><br> <b>Subject:</b> Re: [Python-ideas] \
allow overriding files used for the input builtin</font> <div>&nbsp;</div>
</div>
<div>
<div class="m_-5979384650938487007h5">
<div>
<div dir="ltr">
<div>
<div>
<div>
<div>
<div>I'm fine with the idea in general of extra keyword parameters to the input \
function.<br> <br>
</div>
A few points:<br>
<br>
</div>
Your example code, needs try/catch to match what the input with parameters does -- \
and yes, its way nicer to be able to use it the example you have shown than play \
games with try/catch (Personally I also refuse to ever change sys.stdin, or \
sys.stdout, as I  consider that a bad coding style).<br>
<br>
</div>
Mostly though I would like to ask, please do not name keyword arguments with names \
like 'fin' &amp; 'fout'.&nbsp; This is almost unreadable and make's code almost \
indecipherable to others the first time they see the function &amp; its keyword \
arguments (First impressions  are very important).<br>
<br>
</div>
Both a function name &amp; its keyword parameters need to be as understandable as \
possible when a user encounters them for the first time.<br> </div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Sep 29, 2017 at 1:53 AM, Wren Turkal <span \
dir="ltr"> &lt;<a href="mailto:w00t@fb.com" \
target="_blank">w00t@fb.com</a>&gt;</span> wrote:<br> <blockquote class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div \
dir="ltr"> <div id="m_-5979384650938487007m_-348553151314392222m_-7469814740547824775divtagdefaultwrapper" \
style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols" dir="ltr"> <p>Hi there,</p>
<p><br>
</p>
<p>I have posted an idea for improvement with a PR of an implementation to&nbsp;<a \
href="https://urldefense.proofpoint.com/v2/url?u=https-3A__bugs.python.org_issue31603& \
amp;d=DwMFaQ&amp;c=5VD0RTtNlTh3ycd41b3MUw&amp;r=OAN5uLR4JWXbIgcvx315Zw&amp;m=K1-OD0dhs \
lOiAqYQdEyr0Oppl8TaroBPvXr8h_Z8XxM&amp;s=sjDmBcI00MbWPPjuLMPlBnZHoFZOHoTxaCo2KCYlEd4&amp;e=" \
class="m_-5979384650938487007m_-348553151314392222m_-7469814740547824775OWAAutoLink" \
id="m_-5979384650938487007m_-348553151314392222m_-7469814740547824775LPlnk789029" \
target="_blank">https://bugs.python.org/iss<wbr>ue31603</a>.</p> <p><br>
</p>
<p>The basic idea is to add fin, fout, and ferr file object parameters and default to \
using what is used today when the args are not specified. I believe this would be \
useful to allow captures input and send output to specific files when using input. \
The input  builtin has some&nbsp;logic to use readline if it's available. It would be \
nice to be able to use this same logic no matter what files are being used for \
input/output.</p> <p><br>
</p>
<p>This is meant to turn code like the following:</p>
<p><span style="font-family:Consolas,Courier,monospace">orig_stdin&nbsp;</span><span \
style="font-family:Consolas,Courier,monospace">= sys.stdin</span></p> <p><span \
style="font-family:Consolas,Courier,monospace">orig_stdout = sys.stdout</span></p> \
<p><span style="font-family:Consolas,Courier,monospace">with open('/dev/tty', \
'r&#43;') as f:</span></p> <p><span \
style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; sys.stdin = f</span></p> \
<p><span style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; sys.stdout = \
f</span></p> <p><span style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; \
name =&nbsp;</span><span style="font-family:Consolas,Courier,monospace">input('Name? \
')</span></p> <p><span style="font-family:Consolas,Courier,monospace">sys.stdin = \
orig_stdin</span></p> <p><span \
style="font-family:Consolas,Courier,monospace">sys.stdout = orig_stdout</span></p> \
<p><span style="font-family:Consolas,Courier,monospace">print(name)</span></p> \
<p><br> </p>
<p>into something more like this:</p>
<p><span style="font-family:Consolas,Courier,monospace">with open('/dev/tty', \
'r&#43;') as f:</span></p> <p><span \
style="font-family:Consolas,Courier,monospace">&nbsp; &nbsp; name = input('Name? ', \
fin=f, fout=f)</span></p> <p><span \
style="font-family:Consolas,Courier,monospace">print(name)</span></p> <p><br>
</p>
<p>It's nice that it makes the assignment to a global variable to change the file \
used for input/output to no longer be needed.</p> <p><br>
</p>
<p>I had this idea the other day, and I realized that it would be super easy to \
implement, so I went ahead the threw up a PR also.</p> <p><br>
</p>
<p>Would love to see if anyone else is interested in this. I think it's pretty cool \
that the core logic really didn't need to be changed other than plumbing in the new \
args.</p> <p><br>
</p>
<p>FWIW, this change introduces no regressions and adds a few more tests to test the \
new functionality. Honestly, I think this functionality could probably be used to \
simplify some of the other tests as well, but I wanted to gauge what folks thought of \
the  change before going farther.</p>
<p><br>
</p>
<div id="m_-5979384650938487007m_-348553151314392222m_-7469814740547824775Signature">
<div id="m_-5979384650938487007m_-348553151314392222m_-7469814740547824775divtagdefaultwrapper" \
dir="ltr" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif,Helvetica,EmojiFont,&quot;Apple \
Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,NotoColorEmoji,&quot;Segoe UI \
Symbol&quot;,&quot;Android Emoji&quot;,EmojiSymbols"> <p>Wren Turkal</p>
<p>Existential Production Engineer of the Ages</p>
<p>Facebook, Inc.</p>
</div>
</div>
</div>
</div>
<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" \
target="_blank">Python-ideas@python.org</a><br> <a \
href="https://urldefense.proofpoint.com/v2/url?u=https-3A__mail.python.org_mailman_lis \
tinfo_python-2Dideas&amp;d=DwMFaQ&amp;c=5VD0RTtNlTh3ycd41b3MUw&amp;r=OAN5uLR4JWXbIgcvx \
315Zw&amp;m=K1-OD0dhslOiAqYQdEyr0Oppl8TaroBPvXr8h_Z8XxM&amp;s=dvbtuL0kPe5UxakqosCnQQCIxlKprMP6JTqx4ZLXx4g&amp;e=" \
rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
 Code of Conduct: <a \
href="https://urldefense.proofpoint.com/v2/url?u=http-3A__python.org_psf_codeofconduct \
_&amp;d=DwMFaQ&amp;c=5VD0RTtNlTh3ycd41b3MUw&amp;r=OAN5uLR4JWXbIgcvx315Zw&amp;m=K1-OD0d \
hslOiAqYQdEyr0Oppl8TaroBPvXr8h_Z8XxM&amp;s=px67QeYtdnaejiTP9VcY5IuSiJ4pk3XEXcbDzZWbsnI&amp;e=" \
rel="noreferrer" target="_blank"> http://python.org/psf/codeofco<wbr>nduct/</a><br>
<br>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</body>
</html>



_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

--===============5074600633372857194==--

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic