[issue35098] Deleting __new__ does not restore previous behavior

2018-10-30 Thread Josh Rosenberg


Change by Josh Rosenberg :


--
resolution:  -> duplicate
stage:  -> resolved
status: open -> closed
superseder:  -> Assigning and deleting __new__ attr on the class does not allow 
to create instances of this class

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35098] Deleting __new__ does not restore previous behavior

2018-10-29 Thread ppperry


ppperry  added the comment:

This is a duplicate of issue25731

--
nosy: +ppperry

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35098] Deleting __new__ does not restore previous behavior

2018-10-29 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

> Its quite valid to assign to __new__ to replace the behavior of how an 
> instance is created.

Of course it is, and I never argued otherwise.

> Finally as for `Color.__x__` assignment, this has nothing to do with 
> `__slots__` as it is assigning to `Color`, not to an instance of 
> `Color`.

Of course, sorry for the noise.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35098] Deleting __new__ does not restore previous behavior

2018-10-28 Thread Joy Diamond


Joy Diamond  added the comment:

Its quite valid to assign to __new__ to replace the behavior of how an instance 
is created.

(Obviously you would not really assign `0` to it; my example was just to show 
the `del Color.__new__` fails - so what was assigned was not relevant).

Here is a more realistic assignment to __new__ -- this one shows we are 
"caching" the instance "green" -- so it is reused:

class Color(object):
__slots__ = (('name',))

def __init__(self, name):
self.name = name


green = Color('green')  #   Works
assert green.name == 'green'


@staticmethod
def Color__new__cache_green(m, name):
if name == 'green':
return green

return object.__new__(m, name)


Color.__new__ = Color__new__cache_green

green_2 = Color('green')
assert green_2 == green

blue = Color('blue')
assert blue.name == 'blue'

del Color.__new__

red = Color('red')  #   Fails in Python 3; works in Python 2 & pypy
assert red.name == 'red'

Finally as for `Color.__x__` assignment, this has nothing to do with 
`__slots__` as it is assigning to `Color`, not to an instance of `Color`.

`green.__x__ = 0` properly fails since there is no `__x__` slot:

AttributeError: 'Color' object has no attribute '__x__'

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35098] Deleting __new__ does not restore previous behavior

2018-10-28 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

I think the real WTF here is that you can write to arbitrary dunder attributes 
even if they aren't listed in __slots__.

py> Color.__NOBODY_expects_the_Spanish_Inquisition__ = "What?"
py> Color.__NOBODY_expects_the_Spanish_Inquisition__
'What?'

I think that assigning to Color.__new__ should have failed in the first place.

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35098] Deleting __new__ does not restore previous behavior

2018-10-28 Thread Joy Diamond


New submission from Joy Diamond :

Related: https://bugs.python.org/issue5322

Consider the following program:

class Color(object):
__slots__ = (('name',))

def __init__(self, name):
self.name = name

green = Color('green')  #   Works
assert green.name == 'green'

Color.__new__ = 0
del Color.__new__

red = Color('red')  #   Fails in Python 3; works in Python 2 & pypy
assert red.name == 'red'

This works in Python 2, pypy, but fails in Python 3 as follows:

Traceback (most recent call last):
  File "x.py", line 13, in 
red = Color('red')  #   Fails in Python 3; works in Python 2 & pypy
TypeError: object() takes no parameters

--
messages: 328773
nosy: joydiamond
priority: normal
severity: normal
status: open
title: Deleting __new__ does not restore previous behavior
versions: Python 3.5, Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com