python+ncurses: I can't display accents

F

Fabrice DELENTE

Hello.

I'm trying to display french characters (è -- that's e grave -- or à --
agrave) in python 2.5, with the ncurses wrapper that comes it, and I can't.
My locale is set correctly (fr_FR.iso885915), and my terminal (rxvt-unicode)
is able to display those chars.

What am I missing?

Thanks.
 
N

Neil Cerutti

I'm trying to display french characters (è -- that's e grave --
or à -- agrave) in python 2.5, with the ncurses wrapper that
comes it, and I can't. My locale is set correctly
(fr_FR.iso885915), and my terminal (rxvt-unicode) is able to
display those chars.

What have you tried?
 
F

Fabrice DELENTE

What have you tried?

I've tried

stdscr.addstr(0,0,"aéïoù")

or

stdscr.addstr(0,0,"leçon")

The ASCII chars show correctly, but the accented characters don't, so I see
'ao' or 'leon' on the screen.

The term in which I display is 8-bit-able, so the problem is either on
ncurses side, or on python side.

I have

#!/usr/local/bin/python
#coding: iso8859-15

at the top of my python file.
 
N

Neil Cerutti

I've tried

stdscr.addstr(0,0,"aéïoù")

or

stdscr.addstr(0,0,"leçon")

The ASCII chars show correctly, but the accented characters
don't, so I see 'ao' or 'leon' on the screen.

The term in which I display is 8-bit-able, so the problem is
either on ncurses side, or on python side.

What happens when you try this?

stdscr.addstr(0,0, u"leçon".encode('iso8859-15'))

I don't really expect it to work, but if anything will, that is
it. Curses supports only ASCII and a some special symbol codes
defined by curses.
I have

#!/usr/local/bin/python
#coding: iso8859-15

Be sure to write your non-ASCII strings as unicode literals, and
then encode them just before displaying or storing them
somewhere.
 
F

Fabrice DELENTE

What happens when you try this?
stdscr.addstr(0,0, u"leçon".encode('iso8859-15'))
I don't really expect it to work

And it doesn't...

As support for 8-bit (and even unicode) is important for my script, is there
any hope? Should I switch to slang instead of curses?
 
T

Thomas Dickey

Neil Cerutti said:
I don't really expect it to work, but if anything will, that is
it. Curses supports only ASCII and a some special symbol codes
defined by curses.

un - no. Curses supports whatever the flavor of curses you have does.
Often that's the 8-bit flavor of ncurses, but the limitation is definitely
in the python configuration.
 
F

Fabrice DELENTE

My system is Linux, and the distribution is Slackware 10.1.

I have

/lib/libncurses.so.5.4
/lib/libncursesw.so.5.4

so I even have the wide-chars version available. Any hint on the python
configuration? I didn't find any function that would allow the unrestricted
display of 8-bit chars.
 
N

Neil Cerutti

un - no. Curses supports whatever the flavor of curses you
have does. Often that's the 8-bit flavor of ncurses, but the
limitation is definitely in the python configuration.

Thanks for the clarification. I was going by the some Python
documentation, but I did notice contradictory information in
other discussion. The 8-bit ncurses is supposed to support
iso-8859-1 through iso-8859-15, i.e., all the byle encodings. I
don't know why Python's bindings don't work.
 
F

Fabrice DELENTE

To really be sure that the problem is when I use python, I tried in C:

#include <stdio.h>
#include <ncurses.h>

int main(void)
{
initscr(); /* Start curses mode */
// printw("àéïoù"); /* Print Hello World */
addstr("àéïoù"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */

return(0);
}

and both my tries (with printw, or with addstr) showed the 8-bot chars
correctly.
 
F

Fabrice DELENTE

Incidentally, I noticed something about the environment: in my script, I use
the LINES and COLUMNS environment vars that are set in my shell:

columns=int(os.environ.get("COLUMNS"))
lines=int(os.environ.get("LINES"))

In the shell, I get

$ echo $LINES $COLUMNS
89 199

but python doesn't get these values. I have to start the script with

$ LINES=$LINES COLUMNS=$COLUMNS ./sort_entries.py

How come?
 
C

Carsten Haese

On 27 Jan 2007 07:43:59 GMT, Fabrice DELENTE wrote
Incidentally, I noticed something about the environment: in my
script, I use the LINES and COLUMNS environment vars that are set in
my shell:

columns=int(os.environ.get("COLUMNS"))
lines=int(os.environ.get("LINES"))

In the shell, I get

$ echo $LINES $COLUMNS
89 199

but python doesn't get these values. I have to start the script with

$ LINES=$LINES COLUMNS=$COLUMNS ./sort_entries.py

How come?

There is a distinction between shell variables and environment variables. In
all likelihood, LINES and COLUMNS are shell variables, not environment
variables. Try "export LINES COLUMNS" to set them as environment variables.

HTH,

Carsten.
 
F

Fabrice DELENTE

Try "export LINES COLUMNS" to set them as environment variables.

Thanks, it works. Didn't know that.
 
F

Fabrice DELENTE

It's solved, it was a locale problem: I put

import locale

locale.setlocale(locale.LC_ALL,"fr_FR.iso8859-1")

at the beginning of the script, and now the 8-bit-chars show up correctly.

Thanks all for your help.
 
T

Thomas Dickey

Fabrice DELENTE said:
My system is Linux, and the distribution is Slackware 10.1.
I have

so I even have the wide-chars version available. Any hint on the python
configuration? I didn't find any function that would allow the unrestricted
display of 8-bit chars.

It's more complicated than that: python's loading "ncurses"
dynamically.

It doesn't matter much (to python) which one it loads, but it's
specifying the library name explicitly. At the same time, some other
packages (such as readline) are _separately_ loading the ncurses library
- and they also specify a library name. Rather than abstracting that
stuff out to another (more easily changed) level, it's embedded in the code.

If the initialization script is changed to load "ncursesw" then python
can use ncursesw without further change. There are a few patches to the
configuration that I've seen mentioned in the bug reports to enable
python to do this. Those are patches to python of course...

(this was a topic of discussion on this newsgroup about a year ago).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top