Exploring terminfo

Grant Edwards grant.b.edwards at gmail.com
Fri Jan 15 12:31:42 EST 2021


On 2021-01-15, Alan Gauld via Python-list <python-list at python.org> wrote:
> On 14/01/2021 16:12, Alan Gauld via Python-list wrote:
>
>> #################
>> import curses as cur
>> cur.setupterm()
>> 
>> bold = cur.tigetstr('bold')
>> cls = cur.tigetstr('clear')
>> 
>> cur.putp(cls)
>> name = input("Hello, what's your name? ")
>> 
>> cur.putp(bold)
>> print("Nice to meet you ", name)
>> 
>> input("\nHit enter to exit")
>> ###############
>> 
>> I've tried variations on flush both inside and outside
>> the print() - both before and after.
>> I've tried writing to stdout instead of print, but nothing is working.
>> The "Hit enter..." message is in bold but the "Nice..." line isn't.
>
> A bit more exploration and I've got a C version of the same program
> to work (I'll spare you the #includes!):
>
> int main(void){
>     char line[20];
>     int err = 0;
>     char *clr;
>     char *bold;
>
>     setupterm("xterm",1, &err);
>     clr = tigetstr("clear");
>     bold = tigetstr("bold");
>
>     putp(clr);
>     putp(bold);
>     printf("Enter a name: ");
>     fgets(line, sizeof(line),stdin);
>
>     printf("Hello %s\n", line);
>     exit(0);
> }

> So the native C functions work as expected.
> Why does the Python wrapper not?

One difference is that the name prompt is being written to stdout in
the C version and stderr in the Python version. But I don't see why
that would matter.

I suspect that the problem is that putp is writing to the libc
"stdout" FILE stream that's declaredin <stdio.h>. That stream
layer/object has buffering that is invisible to Python. Python's
sys.stdout.flush() is flushing the Python file object's output buffers
to the standard output file descriptor(fd 2). It is not flushing the
the libc FILE stream's output buffers. That apparently doesn't happen
until input() is called (not sure why input() does that). If you omit
the second input() call, and just write to sys.stdout, then the bold
escape sequence doesn't get output until the program terminates.

Now the question: is there a way to tell the curses module to flush
its stdout FILE stream?

--
Grant







More information about the Python-list mailing list