Re: [Python-Dev] python2.7 infinite recursion when loading pickled object

2014-08-11 Thread Peter Otten
Terry Reedy wrote:

> On 8/11/2014 5:10 AM, Schmitt Uwe (ID SIS) wrote:
> 
> Python usage questions should be directed to python-list, for instance.
> 
>> I discovered a problem using cPickle.loads from CPython 2.7.6.
> 
> The problem is your code having infinite recursion. You only discovered
> it with pickle.
> 
> 
>> The last line in the following code raises an infinite recursion
>>
>>  class T(object):
>>
>>  def __init__(self):
>>  self.item = list()
>>
>>  def __getattr__(self, name):
>>  return getattr(self.item, name)
> 
> This is a (common) bug in your program.  __getattr__ should call
> self.__dict__(name) to avoid the recursion.

Read again. The OP tries to delegate attribute lookup to an (existing) 
attribute.

IMO the root cause of the problem is that pickle looks up __dunder__ methods 
in the instance rather than the class.


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


Re: [Python-Dev] python2.7 infinite recursion when loading pickled object

2014-08-11 Thread Peter Otten
Chris Angelico wrote:

> On Mon, Aug 11, 2014 at 9:40 PM, Peter Otten <__pete...@web.de> wrote:
>> Read again. The OP tries to delegate attribute lookup to an (existing)
>> attribute.
>>
>> IMO the root cause of the problem is that pickle looks up __dunder__
>> methods in the instance rather than the class.
> 
> The recursion comes from the attempted lookup of self.item, when
> __init__ hasn't been called.

You are right. Sorry for the confusion.


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


[Python-Dev] Function to handle None in sort operation, was Re: python 3 niggle: None < 1 raises TypeError

2014-02-14 Thread Peter Otten
M.-A. Lemburg wrote:

> IMO, it was a mistake to have None return a TypeError in
> comparisons, since it makes many typical data operations
> fail, e.g.
> 
> Python2:
 l = [1,2,None,4,5,None,6]
 l.sort()
 l
> [None, None, 1, 2, 4, 5, 6]
> 
> Python3:
 l = [1,2,None,4,5,None,6]
 l.sort()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: unorderable types: NoneType() < int()
 
While it is trivial to fix

>>> sorted([1,2,None,4,5,None,6],
... key=lambda x: (x is None, x))
[1, 2, 4, 5, 6, None, None]

maybe the key should be given a name like functools.none_first/none_last in 
order to offer a standard approach? 

On the other hand I'm not sure I like

none_last(x) < none_last(y)

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


[Python-Dev] Re: Inadequate error reporting during function call setup stage

2021-02-22 Thread Peter Otten

On 21/02/2021 23:06, Terry Reedy wrote:

On 2/21/2021 12:04 PM, Paul Sokolovsky wrote:


Traceback (most recent call last):
   File "pseudoc_tool.py", line 91, in 
 first_class_function_value(func, **pass_params)
TypeError: print() got an unexpected keyword argument 'noann'


This is not typical behavior in current Python (3.8+).


The way I understand it's not about print(), it's about
disambiguating multiple functions with the same name.
Example:

PS > type .\ambiguous_names.py
import random

def do_stuff():
pass

f = do_stuff

def do_stuff(a, b):
pass

g = do_stuff

random.choice([f, g])(42)


PS > py .\ambiguous_names.py
Traceback (most recent call last):
  File "...\ambiguous_names.py", line 13, in 
random.choice([f, g])(42)
TypeError: do_stuff() missing 1 required positional argument: 'b'

The traceback gives no clue which of the two do_stuff() functions caused
the error, you have to check both implementations.

If that is a comman problem one might consider including module name and
co_firstlineno in the message, or at least adding the relevant
do_stuff() function to the exception's args.
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/E7OUHLWXP3NHEM3UZOBWP5AQRSFL5RDO/
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-Dev] type.__subclasses__() doesn't work

2013-10-09 Thread Peter Otten
Steven D'Aprano wrote:

> On Wed, Oct 09, 2013 at 12:20:18PM +0200, Antoine Pitrou wrote:
>> 
>> Hello,
>> 
>> Just noticed the following quirk:
>> 
>> >>> type.__subclasses__()
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> TypeError: descriptor '__subclasses__' of 'type' object needs an argument
>> 
>> Yet it would be nice to know about the subclasses of type.
> 
> py> type.__subclasses__(type)
> [, ]

The underlying problem seems to be that there is no helper function to 
bypass the instance attribute. Compare:

>>> class T(type):
... def __len__(self): return 0
... 
>>> class A(metaclass=T):
... def __len__(self): return 1
... 
>>> A.__len__()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: __len__() missing 1 required positional argument: 'self'
>>> len(A)
0

So should there be a subclasses() function, in the operator module perhaps?

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


Re: [Python-Dev] Rethinking intern() and its data structure

2009-04-10 Thread Peter Otten
John Arbash Meinel wrote:

> Not as big of a difference as I thought it would be... But I bet if
> there was a way to put the random shuffle in the inner loop, so you
> weren't accessing the same identical 25k keys internally, you might get
> more interesting results.

You can prepare a few random samples during startup:

$ python -m timeit -s"from random import sample; d =
dict.fromkeys(xrange(10**7)); nextrange = iter([sample(xrange(10**7),25000)
for i in range(200)]).next" "for x in nextrange(): d.get(x)"
10 loops, best of 3: 20.2 msec per loop

To put it into perspective:
 
$ python -m timeit -s"d = dict.fromkeys(xrange(10**7)); nextrange =
iter([range(25000)]*200).next" "for x in nextrange(): d.get(x)"
100 loops, best of 3: 10.9 msec per loop

Peter

___
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] Python3 regret about deleting list.sort(cmp=...)

2011-03-12 Thread Peter Otten
Guido van Rossum wrote:

> I was just reminded that in Python 3, list.sort() and sorted() no
> longer support the cmp (comparator) function argument. The reason is
> that the key function argument is always better. But now I have a
> nagging doubt about this:
> 
> I recently advised a Googler who was sorting a large dataset and
> running out of memory. My analysis of the situation was that he was
> sorting a huge list of short lines of the form "shortstring,integer"
> with a key function that returned a tuple of the form ("shortstring",
> integer). Using the key function argument, in addition to N short
> string objects, this creates N tuples of length 2, N more slightly
> shorter string objects, and N integer objects. (Not to count a
> parallel array of N more pointers.) Given the object overhead, this
> dramatically increased the memory usage. It so happens that in this
> particular Googler's situation, memory is constrained but CPU time is
> not, and it would be better to parse the strings over and over again
> in a comparator function.
> 
> But in Python 3 this solution is no longer available. How bad is that?
> I'm not sure. But I'd like to at least get the issue out in the open.

While there are other arguments to reintroduce cmp (or less_than instead?) 
the memory problem could also be addressed with a dont_cache_keys flag or 
max_cache_keys limit.

Peter

___
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] Playing with a new theme for the docs, iteration 2

2012-03-25 Thread Peter Otten
Georg Brandl wrote:

> Here's another try, mainly with default browser font size, more contrast
> and collapsible sidebar again:
> 
> http://www.python.org/~gbrandl/build/html2/

Nice! Lightweight and readable.

>From the bikeshedding department:

* Inlined code doesn't need the gray background. The bold font makes it 
stand out enough.
* Instead of the box consider italics or another color for [New in ...] 
text.
* Nobody is going to switch off the prompts for interactive sessions.
* Maybe the Next/Previous Page headers on the left could link to the 
respective page.
* Short descriptions in the module index don't need italics.
* The disambiguation in the index table could use a different style instead 
of the parentheses.


___
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] Playing with a new theme for the docs, iteration 2

2012-03-25 Thread Peter Otten
Serhiy Storchaka wrote:

>> * Maybe the Next/Previous Page headers on the left could link to the
>> respective page.
> 
> Do you mean next/previous links in header/footer?
 
No, I mean the two sections in the sidebar on the left, below "Table of 
Contents".

___
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] Checking if unsigned int less then zero.

2012-06-22 Thread Peter Otten
Dmitriy Tochansky wrote:

> Playing with cpython source, I found some strange strings in
> socketmodule.c:
> 
> ---
>  if (flowinfo < 0 || flowinfo > 0xf) {
>  PyErr_SetString(
>  PyExc_OverflowError,
>  "getsockaddrarg: flowinfo must be 0-1048575.");
>  return 0;
>  }
> ---
> 
> ---
>  if (flowinfo < 0 || flowinfo > 0xf) {
>  PyErr_SetString(PyExc_OverflowError,
>  "getsockaddrarg: flowinfo must be 0-1048575.");
>  return NULL;
>  }
> ---
> 
> The flowinfo variable declared few strings above as unsgined int. Is
> there any practical sense in this check? Seems like gcc just removes
> this check. I think any compiler will generate code that checks as
> unsigned, for example in x86 its JAE/JGE. May be this code is for "bad"
> compilers or exotic arch?

I think you are right, the < 0 check is redundant. The developers probably 
forgot to remove it when

http://bugs.python.org/issue9975

was fixed.

___
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] Should urlencode() sort the query parameters (if they come from a dict)?

2012-08-18 Thread Peter Otten
Guido van Rossum wrote:

> I wonder if it wouldn't make sense to change urlencode() to generate
> URLs that don't depend on the hash order, for all versions of Python
> that support PYTHONHASHSEED? It seems a one-line fix:
> 
> query = query.items()
> 
> with this:
> 
> query = sorted(query.items())
> 
> This would not prevent breakage of unit tests, but it would make a
> much simpler fix possible: simply sort the parameters in the URL.
> 
> Thoughts?

There may be people who mix bytes and str or pass other non-str keys:

>>> query = {b"a":b"b", "c":"d", 5:6}
>>> urlencode(query)
'a=b&c=d&5=6'
>>> sorted(query.items())
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: str() < bytes()

Not pretty, but a bugfix should not break such constructs.

___
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] experimental: Misc/NEWS included in docs

2012-09-30 Thread Peter Otten
Georg Brandl wrote:

> at http://docs.python.org/3.3/whatsnew/news.html, there is now
> a rendering of Misc/NEWS with tracker links and a crude filtering
> capability.  I thought that this will complement the "whatsnew"
> documents nicely for people looking for more detail.
> 
> Please let me know if it's useful, or what could be done to make
> it *more* useful.

Maybe you could supress those sections that become empty when a filter is 
applied.

___
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] [Python-checkins] python/dist/src/Lib urllib.py, 1.169, 1.170

2005-09-10 Thread Peter Otten
Am Samstag, 10. September 2005 04:27 schrieb [EMAIL PROTECTED]:

> Update of /cvsroot/python/python/dist/src/Lib
> In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3622
>
> Modified Files:
>   urllib.py
> Log Message:
> Simplify and speed-up quote_plus().
>
> Index: urllib.py
> ===
> RCS file: /cvsroot/python/python/dist/src/Lib/urllib.py,v
> retrieving revision 1.169
> retrieving revision 1.170
> diff -u -d -r1.169 -r1.170
> --- urllib.py 9 Sep 2005 22:27:13 -   1.169
> +++ urllib.py 10 Sep 2005 02:27:41 -  1.170
> @@ -1115,12 +1115,9 @@
>  def quote_plus(s, safe = ''):
>  """Quote the query fragment of a URL; replacing ' ' with '+'"""
>  if ' ' in s:
> -l = s.split(' ')
> -for i in range(len(l)):
> -l[i] = quote(l[i], safe)
> -return '+'.join(l)
> -else:
> -return quote(s, safe)
> +s = s.replace(' ', '+')
> +safe += '+'
> +return quote(s, safe)
>
>  def urlencode(query,doseq=0):
>  """Encode a sequence of two-element tuples or dictionary into a URL
> query string.

You also change the behaviour. Before:

>>> urllib.quote_plus("alpha+beta gamma")
'alpha%2Bbeta+gamma'

After:

>>> urllib.quote_plus("alpha+beta gamma")
'alpha+beta+gamma'

Is that intentional? If so, you also have to update the documentation, which 
currently reads:

quote_plus(string[, safe])

... Plus signs in the original string are escaped unless they are included in 
safe. ...

Peter
___
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