Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79805 > unrolled thread

different behavior from idle/pycharm and shell

Started bykiuhnm03@yahoo.it
First post2014-10-27 15:38 -0700
Last post2014-10-27 16:14 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  different behavior from idle/pycharm and shell kiuhnm03@yahoo.it - 2014-10-27 15:38 -0700
    Re: different behavior from idle/pycharm and shell sohcahtoa82@gmail.com - 2014-10-27 15:49 -0700
    Re: different behavior from idle/pycharm and shell MRAB <python@mrabarnett.plus.com> - 2014-10-27 22:55 +0000
      Re: different behavior from idle/pycharm and shell kiuhnm03@yahoo.it - 2014-10-27 16:14 -0700

#79805 — different behavior from idle/pycharm and shell

Fromkiuhnm03@yahoo.it
Date2014-10-27 15:38 -0700
Subjectdifferent behavior from idle/pycharm and shell
Message-ID<4417873a-f05f-43b9-9803-1226b1b4ddda@googlegroups.com>
Consider this code:

---
from ctypes import *

user32 = windll.user32
user32.MessageBoxA(0, 'ok', 'ok', 0)
---

If I run it in idle or from pycharm, the messagebox shows 'o' instead of 'ok', but if I run it from shell, it shows 'ok' like it should.
The same happens with msvcrt.printf().
Why?

[toc] | [next] | [standalone]


#79806

Fromsohcahtoa82@gmail.com
Date2014-10-27 15:49 -0700
Message-ID<fa3ed4f8-95cc-41b9-af44-675b261312a4@googlegroups.com>
In reply to#79805
On Monday, October 27, 2014 3:38:31 PM UTC-7, kiuh...@yahoo.it wrote:
> Consider this code:
> 
> ---
> from ctypes import *
> 
> user32 = windll.user32
> user32.MessageBoxA(0, 'ok', 'ok', 0)
> ---
> 
> If I run it in idle or from pycharm, the messagebox shows 'o' instead of 'ok', but if I run it from shell, it shows 'ok' like it should.
> The same happens with msvcrt.printf().
> Why?

If I had to take a guess, in the shell, it is encoding the string characters as single bytes as expected, but in Idle or PyCharm, it is encoding them as WCHARs, which are two-bytes wide.

Two things to try:

1. Change the call to MessageBoxW and see if it works in Idle/PyCharm.  Note that this will probably break the call from the shell.
2. Try a string longer than two characters and see what you get.

[toc] | [prev] | [next] | [standalone]


#79807

FromMRAB <python@mrabarnett.plus.com>
Date2014-10-27 22:55 +0000
Message-ID<mailman.15244.1414450531.18130.python-list@python.org>
In reply to#79805
On 2014-10-27 22:38, kiuhnm03@yahoo.it wrote:
> Consider this code:
>
> ---
> from ctypes import *
>
> user32 = windll.user32
> user32.MessageBoxA(0, 'ok', 'ok', 0)
> ---
>
> If I run it in idle or from pycharm, the messagebox shows 'o' instead of 'ok', but if I run it from shell, it shows 'ok' like it should.
> The same happens with msvcrt.printf().
> Why?
>
You didn't say whether you're using Python 2 or Python 3, but it looks
like you're using Python 3.

There are 2 forms of the MessageBox function, one with the suffix 'A',
which uses bytestrings, and one with the suffix 'W', which uses Unicode
strings.

In Python 3, the str class is a Unicode string, so you'll want the
MessageBoxW function:

from ctypes import *

user32 = windll.user32
user32.MessageBoxW(0, 'ok', 'ok', 0)

Also, the msvcrt.printf function expects a bytestring.

[toc] | [prev] | [next] | [standalone]


#79808

Fromkiuhnm03@yahoo.it
Date2014-10-27 16:14 -0700
Message-ID<0ef8347f-6c83-4d1e-95d4-69e308adefdf@googlegroups.com>
In reply to#79807
On Monday, October 27, 2014 11:55:44 PM UTC+1, MRAB wrote:
> On 2014-10-27 22:38, kiuhnm wrote:
> > Consider this code:
> >
> > ---
> > from ctypes import *
> >
> > user32 = windll.user32
> > user32.MessageBoxA(0, 'ok', 'ok', 0)
> > ---
> >
> > If I run it in idle or from pycharm, the messagebox shows 'o' instead of 'ok', but if I run it from shell, it shows 'ok' like it should.
> > The same happens with msvcrt.printf().
> > Why?
> >
> You didn't say whether you're using Python 2 or Python 3, but it looks
> like you're using Python 3.
> 
> There are 2 forms of the MessageBox function, one with the suffix 'A',
> which uses bytestrings, and one with the suffix 'W', which uses Unicode
> strings.
> 
> In Python 3, the str class is a Unicode string, so you'll want the
> MessageBoxW function:
> 
> from ctypes import *
> 
> user32 = windll.user32
> user32.MessageBoxW(0, 'ok', 'ok', 0)
> 
> Also, the msvcrt.printf function expects a bytestring.

Yes, you're right.
Thank you both.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web