how to do random / SystemRandom switch

2011-04-30 Thread Matthias Kievernagel
Dear list,

In my top-level script I want to select if my program
is to use random.xxx functions or the random.SystemRandom.xxx
ones. All the other modules shouldn't know about that
switch and simply use
  import random
  ...
  return random.randint(1, 6)
  ...
for example.

In C I would do a similar thing in the Makefile using
Compiler-/Link-Options (DEBUG/FINAL Build) switching
between two libraries.

Any hints are welcome, especially a search term
would be very helpful :)

Thanks in advance,
Matthias Kievernagel.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to do random / SystemRandom switch

2011-04-30 Thread Matthias Kievernagel
Peter Otten <__pete...@web.de> wrote:
> Matthias Kievernagel wrote:
> 
>> In my top-level script I want to select if my program
>> is to use random.xxx functions or the random.SystemRandom.xxx
>> ones. All the other modules shouldn't know about that
>> switch and simply use
>> import random
>> ...
>> return random.randint(1, 6)
>> ...
>> for example.
> 
> You can inject the SystemRandom instance into the sys.modules cache:
> 
>>>> import random
>>>> random
> 
>>>> sr = random.SystemRandom()
>>>> import sys
>>>> sys.modules["random"] = sr
> 
> Then use it in the other modules:
> 
>>>> import random
>>>> random
> 
> 
> Another approach is to monkey-patch the random module:
> 
> import random
> sr = random.SystemRandom()
> random.randrange = sr.randrange
> random.randint = sr.randint
> ...
> 
Thanks a lot. That's what I was looking for.
I'll give both a try.

Regards,
Matthias Kievernagel
-- 
http://mail.python.org/mailman/listinfo/python-list


spurious BadDrawable error when running test_tk

2018-05-14 Thread Matthias Kievernagel
Dear list,

I changed some detail in the tkinter library,
so I'm running the tk test like this:

./python -m test -u gui -v test_tk

Approximately every 2 or 3 runs I get a BadDrawable error
from the X server, most of the time at the end after
the last test finished successfully.
As this also happens when I run the test on the unmodified CPython sources
I suspect it is a shortcoming of my non-mainstream setup.
I use ctwm with interactive window placement,
so running the test involves a lot of clicks.
Does anyone know if the errors are to be expected
or should it work nonetheless?
I haven't found anything on b.p.o. about this.

Thanks for any insights,
Matthias Kievernagel


-- 
https://mail.python.org/mailman/listinfo/python-list


py3 tkinter Text accepts what bytes?

2010-04-23 Thread Matthias Kievernagel
Hello,

I stumbled upon this one while porting some of my programs
to Python 3.1. The program receives messages from a socket
and displays them in a tkinter Text. Works fine in Python 2
and Python 3.1. The problems arrived when I wanted to know
the details...

First surprise: Text.insert accepts not only str
but also bytes.

So I looked into the sources to see how it is done.
I found no magic in 'tkinter.__init__.py'. All python
objects seem to go unchanged to _tkinter.c.
There they are turned into Tcl objects using Tcl_NewUnicodeObj
(for str) and Tcl_NewStringObj (for bytes).
The man page for Tcl_NewStringObj says that it creates
a tcl string from utf-8 encoded bytes.
So I continued to test...

Second surprise: Text.insert also works for latin-1 encoded bytes.
It even works with mixed utf-8 and latin-1 encoded bytes.
At least it works for me.

Anyone can enlighten me, where this magic is done?
Is it tcl magic or did I miss something in the python sources?
Is this somewhere documented?

Thanks for any hints,
Matthias Kievernagel

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py3 tkinter Text accepts what bytes?

2010-04-23 Thread Matthias Kievernagel
eb303  wrote:
> On Apr 23, 2:00 pm, Matthias Kievernagel 
> wrote:
>> Hello,
>>
>> I stumbled upon this one while porting some of my programs
>> to Python 3.1. The program receives messages from a socket
>> and displays them in a tkinter Text. Works fine in Python 2
>> and Python 3.1. The problems arrived when I wanted to know
>> the details...
>>
>> First surprise: Text.insert accepts not only str
>> but also bytes.
>>
>> So I looked into the sources to see how it is done.
>> I found no magic in 'tkinter.__init__.py'. All python
>> objects seem to go unchanged to _tkinter.c.
>> There they are turned into Tcl objects using Tcl_NewUnicodeObj
>> (for str) and Tcl_NewStringObj (for bytes).
>> The man page for Tcl_NewStringObj says that it creates
>> a tcl string from utf-8 encoded bytes.
>> So I continued to test...
>>
>> Second surprise: Text.insert also works for latin-1 encoded bytes.
>> It even works with mixed utf-8 and latin-1 encoded bytes.
>> At least it works for me.
>>
>> Anyone can enlighten me, where this magic is done?
>> Is it tcl magic or did I miss something in the python sources?
>> Is this somewhere documented?
>>
>> Thanks for any hints,
>> Matthias Kievernagel
> 
> Let me guess: you're on Windows? ;-)
> 
> There is nothing in the Python sources that can help you here.
> Everything is handled by the underlying tcl/tk interpreter. The
> default encoding for strings in tcl happens to be UTF-8. So putting
> bytestrings with a UTF-8 encoding in a Text widget will just work. For
> latin-1 strings, there is some magic going on, but apparently, this
> magic happens only on Windows (hence my guess above???), which seems to
> recognize its default encoding by some means. My advice is: don't
> count on it. It won't work on any other platform, and it might even
> stop working on Windows one day.
> 
> HTH
> - Eric -

Thanks for the info, Eric.
Funny it's working for me, because I'm on Linux.
So I'll take a look at the tcl/tk sources (8.4 btw.)
I don't like this magic at all, run-time errors waiting for you
at the most inconvenient moment.

Best regards,
Matthias Kievernagel.

-- 
http://mail.python.org/mailman/listinfo/python-list


py3 tkinter acceps bytes. why?

2010-05-04 Thread Matthias Kievernagel
From: Matthias Kievernagel 
Subject: py3 tkinter acceps bytes. why?
Newsgroups: comp.lang.python
Summary: 
Keywords: 

In a recent thread named "py3 tkinter Text accepts what bytes?"
(google groups link:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/b75ed69f4e81b202/e2aff9ddd62d210c?lnk=raot)
I asked what kinds of bytes are accepted as tkinter parameters.
I still wonder why they are accepted at all.
Does anyone know a reason for this
or has a link to some relevant discussion?

Thanks for any hint,
Matthias Kievernagel


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py3 tkinter acceps bytes. why?

2010-05-06 Thread Matthias Kievernagel

>>Me:
>> I asked what kinds of bytes are accepted as tkinter parameters.
>> I still wonder why they are accepted at all.
>> Does anyone know a reason for this
>> or has a link to some relevant discussion?
>Terry Reedy:
> I do not remember any particular public discussion of tkinter on the dev 
> lists. I suspect the reason is a) avoid breaking old code and b) tkinter 
> (tk inter-face) passes params on to tk which expects byte strings. I 
> would not be surprised if tkinter has to encode py3 (unicode) strings 
> before passing them on.

If I interpret the code in _tkinter.c correctly
Python bytes are converted to a String Tcl_Obj
and Python str to a Unicode Tcl_Obj:

AsObj(PyObject *value)
{
...
if (PyBytes_Check(value))
return Tcl_NewStringObj(PyBytes_AS_STRING(value),
PyBytes_GET_SIZE(value));
...
else if (PyUnicode_Check(value)) {
...
return Tcl_NewUnicodeObj(inbuf, size);
...
}

And as Martin pointed out:
>That's basically because the port from 2.x was shallow: it supports the
>exact same feature set that 2.x supports, with just the type names 
>swapped.

If I don't want bytes to get passed to tkinter
I just have to raise an exception in AsObj, no?
Or is it even sufficient to just remove the bytes case?

Regards and thanks for your answers.
Matthias Kievernagel.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py3 tkinter acceps bytes. why?

2010-05-07 Thread Matthias Kievernagel
Me:
>> If I don't want bytes to get passed to tkinter
>> I just have to raise an exception in AsObj, no?
>> Or is it even sufficient to just remove the bytes case?
Martin v. Loewis  wrote:
> But why would you want that? There are commands which legitimately
> return bytes, e.g. the file and network io libraries of Tcl (not that
> you would usually want to use them in Python, but Tkinter is actually
> Tclinter, and should support all Tcl commands).

I'm just looking for a reliable error message when I pass
something to GUI functions which is not fit for display,
i.e. not a string. If bytes pass unnoticed,
I'll sooner or later have a surprise.
Just to make sure I decode all bytes (coming from a socket)
before I pass them on to the GUI.

Regards,
Matthias Kievernagel

-- 
http://mail.python.org/mailman/listinfo/python-list