Drekin / win-unicode-console

A Python package to enable Unicode support when running Python from Windows console.
MIT License
103 stars 12 forks source link

ValueError: Procedure probably called with too many arguments (12 bytes in excess) #5

Closed anthrotype closed 9 years ago

anthrotype commented 9 years ago

Hello,

I installed the package via "pip install win_unicode_console". When I enable it inside an interactive session, I get this error below and the python interpreter crashes. I am using the vanilla python 3.4.2 distribution for win32, downloaded from the Python.org, from within the Windows Command Prompt (on Windows 7). I did not change the cmd.exe codepage through "chcp" command, it's stays the default "cp850" on my machine. Here is the output of my interactive session followed by traceback:

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win_unicode_console
>>> win_unicode_console
<module 'win_unicode_console' from 'C:\\Python34\\lib\\site-packages\\win_unicode_console\\__init__.py'>
>>> win_unicode_console.enable()
>>> print('hello')
Intenal win_unicode_console error
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\win_unicode_console\readline_hook.py", line 52, in readline_wrapper
    return new_zero_terminated_string(line.encode(sys.stdin.encoding))
  File "C:\Python34\lib\site-packages\win_unicode_console\readline_hook.py", line 20, in new_zero_terminated_string
    strncpy(cast(p, c_char_p), b, len(b) + 1)
ValueError: Procedure probably called with too many arguments (12 bytes in excess)
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\win_unicode_console\readline_hook.py", line 52, in readline_wrapper
    return new_zero_terminated_string(line.encode(sys.stdin.encoding))
  File "C:\Python34\lib\site-packages\win_unicode_console\readline_hook.py", line 20, in new_zero_terminated_string
    strncpy(cast(p, c_char_p), b, len(b) + 1)
ValueError: Procedure probably called with too many arguments (12 bytes in excess)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 277, in 'calling callback function'
  File "C:\Python34\lib\site-packages\win_unicode_console\readline_hook.py", line 57, in readline_wrapper
    return new_zero_terminated_string(b"\n")
  File "C:\Python34\lib\site-packages\win_unicode_console\readline_hook.py", line 20, in new_zero_terminated_string
    strncpy(cast(p, c_char_p), b, len(b) + 1)
ValueError: Procedure probably called with too many arguments (12 bytes in excess)

Thanks for your support.

Cosimo

Drekin commented 9 years ago

Hello,

it seems that there is a problem with the ctypes call. I'll investigate.

Drekin commented 9 years ago

Fixed.

anthrotype commented 9 years ago

Thanks a lot! It's working now when using the win_unicode_console.enable() method.

However, I still don't understand why the transcoding from UTF-16-LE to UTF-8 is set to False when using the custom REPL. If I run the custom REPL with

python -i -m run

because the input/output are not transcoded, I get unicode decode/encode errors. If I change line 9 of run.py to:

streams.enable(transcode=True)

then the custom REPL works.

Thanks again for your support.

Cosimo

Drekin commented 9 years ago

The custom REPL is from times when installing a custom readline hook wasn't supported. Now there is no need to use run.py.

Nevertheless, the transcoding is set to False when using the custom REPL because it shouldn't be needed. Trancoding is used only because certain Python internals cannot handle utf-16. I thought that the problem is only with the tokenizer, but it turns out that PyOS_Readline has also this problem. When isatty() and fileno() weren't set on the custom stream objects, input() used directly the stream objects, but now it goes via PyOS_Readline and that's why the problem occurs.

So I will eventually set transcode=True in run.py, but the point is that there is no need to use run.