[Python-Dev] getch() in msvcrt does not accept extended characters.

2005-06-29 Thread Darryl Dixon
Hi all,

Please CC: me on any replies as I am not subscribed to this list.  I
am the lead maintainer for the ntlmaps project
(http://ntlmaps.sourceforge.net).  Recently a bugreport logged against
ntlmaps has brought an issue to my attention (see:
https://sourceforge.net/tracker/index.php?func=detail&aid=1224877&group_id=69259&atid=523935).
  The getpass() function in module getpass does not accept extended characters 
on Windows.  On Windows, the getpass() source uses the msvcrt module to capture 
one character at a time from stdin via the _getch() function defined in 
conio.h.  Microsoft support capturing extended characters via _getch (see: 
http://support.microsoft.com/default.aspx?scid=kb;en-us;57888), but it requires 
making a second call to getch() if one of the 'magic' returns is encountered in 
the first call (0x00 or 0xE0).

The relevant chunk of code in Python that would probably need to be
changed to support this appears to be in msvcrtmodule.c:

static PyObject *
msvcrt_getch(PyObject *self, PyObject *args)
{
int ch;
char s[1];

if (!PyArg_ParseTuple(args, ":getch"))
return NULL;

Py_BEGIN_ALLOW_THREADS
ch = _getch();
Py_END_ALLOW_THREADS
s[0] = ch;
return PyString_FromStringAndSize(s, 1);
}

It would seem that it could be made to support extended characters by changing 
it to:

static PyObject *
msvcrt_getch(PyObject *self, PyObject *args)
{
int ch;
char s[1];

if (!PyArg_ParseTuple(args, ":getch"))
return NULL;

Py_BEGIN_ALLOW_THREADS
ch = _getch();
+if (ch == 0x00 || ch == 0XE0)
+ch = _getch();
Py_END_ALLOW_THREADS
s[0] = ch;
return PyString_FromStringAndSize(s, 1);
}

I haven't yet read the code for PyString_FromStringAndSize(), but presumably it 
can coerce unicode/extended results out of the int (or could be made to)?

What are the chances of modifying msvcrt_getch() to support extended chars?

The unix_getpass() seems to already work OK with extended characters, so this 
would ~seem~ to be a logical extension to the Windows version.

Thoughts?



-- 
Darryl Dixon <[EMAIL PROTECTED]>

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] getch() in msvcrt does not accept extended characters.

2005-06-30 Thread Darryl Dixon
Hi,

I'm sorry, I don't seem to have done a very good job at explaining
the situation.  I'll try again:
'getch()' is a low-level function provided on Windows to capture a
single character of input from a user, /without echoing it to the
screen/.  As far as I can tell there's no other way of doing this with
Python on Windows.  The Python interface to this function is in the C
code in msvcrtmodule.c, which has a (very thin) wrapper around the raw
OS system call.  Microsoft provide a way of accepting both normal ASCII
codes, and extended characters via this system call.  Currently, the
Python wrapper in msvcrtmodule.c only supports the semantics of getting
the bare ASCII codes, and not the extended characters.  I would strongly
suggest that it should support both.

So, I guess in answer to the questions raised below;

1) I can't do it in Python code without getch() (hence the original
email)

2) I would argue that in fact getch() is 'broken' in its current Python
implementation, as it doesn't support what the OS implies /should/ be
supported (and, indeed, if I input an extended character in response to
a 'getch()' call, all I get back currently is an empty string).

Finally, I've dug a little deeper, and it looks as though if (ch >
UCHAR_MAX) then it would have to call PyUnicode_FromUnicode(s, 1)
instead.

Is it worth sending in a patch to the sourceforge patch-tracker
implementing this?  Is it OK for msvcrt_getch to return Unicode when
appropriate?

Thoughts?

Hope this helps,
D


Fredrik wrote:
>Darryl Dixon wrote:
>
>> Microsoft support capturing extended characters via _getch, but it
requires making a
>> second call to getch() if one of the 'magic' returns is encountered
in the first call (0x00
>> or 0xE0).
>
>so why not do that in your python code?
>
>> The relevant chunk of code in Python that would probably need to be
>> changed to support this appears to be in msvcrtmodule.c:
>
>if you change msvcrt, you'll break all the programs that uses getch()
in
>the prescribed way...
>
> 
-- 
Darryl Dixon <[EMAIL PROTECTED]>

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] getch() in msvcrt does not accept extended characters.

2005-07-05 Thread Darryl Dixon
Hi,

I'm sorry, I don't seem to have done a very good job at explaining
the situation.  I'll try again:
'getch()' is a low-level function provided on Windows to capture a
single character of input from a user, /without echoing it to the
screen/.  As far as I can tell there's no other way of doing this with
Python on Windows.  The Python interface to this function is in the C
code in msvcrtmodule.c, which has a (very thin) wrapper around the raw
OS system call.  Microsoft provide a way of accepting both normal ASCII
codes, and extended characters via this system call.  Currently, the
Python wrapper in msvcrtmodule.c only supports the semantics of getting
the bare ASCII codes, and not the extended characters.  I would strongly
suggest that it should support both.

So, I guess in answer to the questions raised below;

1) I can't do it in Python code without getch() (hence the original
email)

2) I would argue that in fact getch() is 'broken' in its current Python
implementation, as it doesn't support what the OS implies /should/ be
supported (and, indeed, if I input an extended character in response to
a 'getch()' call, all I get back currently is an empty string).

Hope this helps,
D


Fredrik wrote:
>Darryl Dixon wrote:
>
>> Microsoft support capturing extended characters via _getch, but it requires 
>> making a
>> second call to getch() if one of the 'magic' returns is encountered in the 
>> first call (0x00
>> or 0xE0).
>
>so why not do that in your python code?
>
>> The relevant chunk of code in Python that would probably need to be
>> changed to support this appears to be in msvcrtmodule.c:
>
>if you change msvcrt, you'll break all the programs that uses getch() in
>the prescribed way...
>
> 
-- 
Darryl Dixon <[EMAIL PROTECTED]>

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] getch() in msvcrt does not accept extended characters.

2005-07-05 Thread Darryl Dixon
Sorry all,

It seems that the list mods have finally released a duplicate
message that I wrote last week when I wasn't subscribed to the list.
Please ignore the message below if you have read the previous copy
already.

D


On Thu, 2005-06-30 at 09:43 +1200, Darryl Dixon wrote:
> Hi,
> 
> I'm sorry, I don't seem to have done a very good job at explaining
> the situation.  I'll try again:
> 'getch()' is a low-level function provided on Windows to capture a
> single character of input from a user, /without echoing it to the
> screen/.  As far as I can tell there's no other way of doing this with
> Python on Windows.  The Python interface to this function is in the C
> code in msvcrtmodule.c, which has a (very thin) wrapper around the raw
> OS system call.  Microsoft provide a way of accepting both normal ASCII
> codes, and extended characters via this system call.  Currently, the
> Python wrapper in msvcrtmodule.c only supports the semantics of getting
> the bare ASCII codes, and not the extended characters.  I would strongly
> suggest that it should support both.
> 
> So, I guess in answer to the questions raised below;
> 
> 1) I can't do it in Python code without getch() (hence the original
> email)
> 
> 2) I would argue that in fact getch() is 'broken' in its current Python
> implementation, as it doesn't support what the OS implies /should/ be
> supported (and, indeed, if I input an extended character in response to
> a 'getch()' call, all I get back currently is an empty string).
> 
> Hope this helps,
> D
> 
> 
> Fredrik wrote:
> >Darryl Dixon wrote:
> >
> >> Microsoft support capturing extended characters via _getch, but it 
> >> requires making a
> >> second call to getch() if one of the 'magic' returns is encountered in the 
> >> first call (0x00
> >> or 0xE0).
> >
> >so why not do that in your python code?
> >
> >> The relevant chunk of code in Python that would probably need to be
> >> changed to support this appears to be in msvcrtmodule.c:
> >
> >if you change msvcrt, you'll break all the programs that uses getch() in
> >the prescribed way...
> >
> > 
-- 
Darryl Dixon <[EMAIL PROTECTED]>

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com