Why can't I set sys.ps1 to a unicode string?

Discussion in 'Python' started by RG, Aug 12, 2010.

  1. RG

    RG Guest

    More precisely, why does sys.ps1 not appear if I set it to a unicode
    string? This problem is hard for me to describe here because my
    newsreader is not properly unicode enabled, but here's the gist of it:

    Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>>


    First, let's make sure our encodings are set properly:

    >>> import sys
    >>> sys.stdin.encoding

    'utf-8'
    >>> sys.stdout.encoding

    'utf-8'

    Looks good. Now, let's make two unicode strings, identical except for
    one character:

    >>> s1 = u'%%% %%% '
    >>> s2 = u'%%% ' + u'\u262f' + '%%% '
    >>> print s1

    %%% %%%
    >>> print s2

    %%% /&%%%

    If this were a properly unicode-enabled newsreader you would see a
    yin-yang symbol in the middle of s2.

    Now the weird part:

    >>> sys.ps1 = s1

    %%% %%% sys.ps1 = s2 # This is as expected
    print s1 # But this isn't. There's no prompt!
    %%% %%% # Everything still works
    print s2
    %%% /&%%%
    sys.ps1 = s1 # If we reset sys.ps1 we get our prompt back
    %%% %%% sys.ps1 = '>>> '
    >>> sys.ps1 = u'>>> '
    >>>


    So... why does having a non-ascii character in sys.ps1 make the prompt
    vanish?

    (If you're wondering why I care, I want to connect to an interactive
    python interpreter from another program, and I want a non-ascii
    delimiter to unambiguously mark the end of the interpreter's output on
    every interaction.)

    Thanks,
    rg
    RG, Aug 12, 2010
    #1
    1. Advertising

  2. > So... why does having a non-ascii character in sys.ps1 make the prompt
    > vanish?


    I can't pinpoint it to a specific line of code. Most likely, it tries
    to encode the prompt as ASCII before writing it to stdout. That fails,
    and it silently ignores the error.

    FWIW, this is fixed in Python 3.

    Regards,
    Martin
    Martin v. Loewis, Aug 12, 2010
    #2
    1. Advertising

  3. RG

    RG Guest

    In article <i407mn$lah$>,
    "Martin v. Loewis" <> wrote:

    > > So... why does having a non-ascii character in sys.ps1 make the prompt
    > > vanish?

    >
    > I can't pinpoint it to a specific line of code. Most likely, it tries
    > to encode the prompt as ASCII before writing it to stdout. That fails,
    > and it silently ignores the error.
    >
    > FWIW, this is fixed in Python 3.


    Guess it's time to upgrade. :)

    Thanks!

    rg
    RG, Aug 12, 2010
    #3
  4. On Thursday 12 August 2010, it occurred to RG to exclaim:
    > %%% /&%%%
    >
    > If this were a properly unicode-enabled newsreader you would see a
    > yin-yang symbol in the middle of s2.


    Are you sure about that? Now maybe the mailing list gateway is messing things
    up, but I rather suspect your newsreader (or USENET server) is sending out
    nonsense. After all, Unicode usually works around here.

    Some excerpts from the message, as I received it:

    Content-Type: text/plain;
    charset="us-ascii"
    Content-Transfer-Encoding: 7bit

    [...]

    >>> print s2

    %%% /&%%%

    No quoted-printable. No UTF-7. Just three completely random characters?
    Thomas Jollans, Aug 12, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,850
    Smokey Grindel
    Dec 2, 2006
  2. vatsa
    Replies:
    3
    Views:
    264
    Keith Thompson
    Apr 3, 2006
  3. Jon Dobson

    sys.ps1 with formatting (linux)

    Jon Dobson, Jul 23, 2007, in forum: Python
    Replies:
    0
    Views:
    374
    Jon Dobson
    Jul 23, 2007
  4. Hugo
    Replies:
    0
    Views:
    393
  5. PS1 shell variable

    , Dec 4, 2006, in forum: Perl Misc
    Replies:
    16
    Views:
    273
    Ingo Menger
    Dec 6, 2006
Loading...

Share This Page