Re: [Django] #30546: SimpleLazyObject not so lazy on Python3

2019-06-05 Thread Django
#30546: SimpleLazyObject not so lazy on Python3
-+-
 Reporter:  Sławek Ehlert|Owner:  nobody
 Type:  Bug  |   Status:  new
Component:  Utilities|  Version:  2.2
 Severity:  Normal   |   Resolution:
 Keywords:  SimpleLazyObject | Triage Stage:
  lazy evaluation|  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-

Comment (by Sławek Ehlert):

 To add more info. The behaviour of the example I gave is actually more
 nuanced than I thought. The function actually does get called on both
 Python 2 and 3. I've added a simple {{{print("inside a_func")}}} inside
 that function and it turns out it gets printed in both cases. The
 exception however occurs only on Python 3 and it seems it's... getting
 swallowed by {{{isinstance}}} on Python 2 (?). IDK.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.f3f96230c8def80bb26269129987bd53%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #30546: SimpleLazyObject not so lazy on Python3

2019-06-05 Thread Django
#30546: SimpleLazyObject not so lazy on Python3
-+-
 Reporter:  Sławek Ehlert|Owner:  nobody
 Type:  Bug  |   Status:  new
Component:  Utilities|  Version:  2.2
 Severity:  Normal   |   Resolution:
 Keywords:  SimpleLazyObject | Triage Stage:
  lazy evaluation|  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-
Description changed by Sławek Ehlert:

Old description:

> When running this little script:
>
> {{{
> from django.utils.functional import *
>
> def a_func():
> raise Exception('naah')
>
> slo = SimpleLazyObject(a_func)
>
> assert not isinstance(slo, type)
> }}}
>
> I'd expect the assertion to pass. It works as I'd expect it to work on
> Django 1.11 and Python 2.7. However, it does blow up with the {{{naah}}}
> exception on both Django 1.11 and 2.2 on Python 3.5 (also on Python 3.6
> and 3.7) with the following traceback:
> {{{
> Traceback (most recent call last):
>   File "", line 1, in 
> assert not isinstance(slo, type)
>   File "... /lib/python3.5/site-packages/django/utils/functional.py",
> line 238, in inner
> self._setup()
>   File "... /lib/python3.5/site-packages/django/utils/functional.py",
> line 386, in _setup
> self._wrapped = self._setupfunc()
>   File "", line 2, in a_func
> raise Exception('naah')
> Exception: naah
> }}}
>
> I'm not sure if this is by design, but the behaviour is certainly
> inconsistent on Py3 vs. Py2.
>
> I found this problem while trying to run our test suite on Python 3.
> Unittest framework does an obvious {{{if isinstance(obj, type) and
> issubclass(obj, case.TestCase):}}} check when collecting the test suite
> (see
> https://github.com/python/cpython/blob/v3.5.7/Lib/unittest/loader.py#L122),
> so when we have a module-level {{{SimpleLazyObject}}} (obviously it wraps
> a different function than the one I've given here, but also a failing) it
> gets unexpectedly evaluated and raises an exception, which prevents us
> from even gathering the tests on Python 3.
>
> I'm not really sure why this is happening. After some brief debugging, it
> looks like {{{isinstance}}} function on Python 3 is accessing the
> {{{__class__}}} attribute (which is wrapped with the
> {{{new_method_proxy}}} function - see
> https://github.com/django/django/blob/2.2.2/django/utils/functional.py#L348)
> causing evaluation of the underlying function, whereas on Python 2.7
> {{{isinstance}}} doesn't seem to do that.

New description:

 When running this little script:

 {{{
 from django.utils.functional import *

 def a_func():
 raise Exception('naah')

 slo = SimpleLazyObject(a_func)

 assert not isinstance(slo, type)
 }}}

 I'd expect the assertion to pass. It works as I'd expect it to work on
 Django 1.11 and Python 2.7. However, it does blow up with the {{{naah}}}
 exception on both Django 1.11 and 2.2 on Python 3.5 (also on Python 3.6
 and 3.7) with the following traceback:
 {{{
 Traceback (most recent call last):
   File "", line 1, in 
 assert not isinstance(slo, type)
   File "... /lib/python3.5/site-packages/django/utils/functional.py", line
 238, in inner
 self._setup()
   File "... /lib/python3.5/site-packages/django/utils/functional.py", line
 386, in _setup
 self._wrapped = self._setupfunc()
   File "", line 2, in a_func
 raise Exception('naah')
 Exception: naah
 }}}

 I'm not sure if this is by design, but the behaviour is certainly
 inconsistent on Py3 vs. Py2.

 I found this problem while trying to run our test suite on Python 3.
 Unittest framework does an obvious {{{if isinstance(obj, type) and
 issubclass(obj, case.TestCase):}}} check when collecting the test suite
 (see
 https://github.com/python/cpython/blob/v3.5.7/Lib/unittest/loader.py#L122),
 so when we have a module-level {{{SimpleLazyObject}}} (obviously it wraps
 a different function than the one I've given here, but also a failing one)
 it gets unexpectedly evaluated and raises an exception, which prevents us
 from even gathering the tests on Python 3.

 I'm not really sure why this is happening. After some brief debugging, it
 looks like {{{isinstance}}} function on Python 3 is accessing the
 {{{__class__}}} attribute (which is wrapped with the
 {{{new_method_proxy}}} function - see
 https://github.com/django/django/blob/2.2.2/django/utils/functional.py#L348)
 causing evaluation of the underlying function, whereas on Python 2.7
 {{{isinstance}}} doesn't seem to do that.

--

-- 
Ticket URL: 
Django 
The Web 

[Django] #30546: SimpleLazyObject not so lazy on Python3

2019-06-05 Thread Django
#30546: SimpleLazyObject not so lazy on Python3
-+-
   Reporter:  Sławek |  Owner:  nobody
  Ehlert |
   Type:  Bug| Status:  new
  Component:  Utilities  |Version:  2.2
   Severity:  Normal |   Keywords:  SimpleLazyObject
   Triage Stage: |  lazy evaluation
  Unreviewed |  Has patch:  0
Needs documentation:  0  |Needs tests:  0
Patch needs improvement:  0  |  Easy pickings:  0
  UI/UX:  0  |
-+-
 When running this little script:

 {{{
 from django.utils.functional import *

 def a_func():
 raise Exception('naah')

 slo = SimpleLazyObject(a_func)

 assert not isinstance(slo, type)
 }}}

 I'd expect the assertion to pass. It works as I'd expect it to work on
 Django 1.11 and Python 2.7. However, it does blow up with the {{{naah}}}
 exception on both Django 1.11 and 2.2 on Python 3.5 (also on Python 3.6
 and 3.7) with the following traceback:
 {{{
 Traceback (most recent call last):
   File "", line 1, in 
 assert not isinstance(slo, type)
   File "... /lib/python3.5/site-packages/django/utils/functional.py", line
 238, in inner
 self._setup()
   File "... /lib/python3.5/site-packages/django/utils/functional.py", line
 386, in _setup
 self._wrapped = self._setupfunc()
   File "", line 2, in a_func
 raise Exception('naah')
 Exception: naah
 }}}

 I'm not sure if this is by design, but the behaviour is certainly
 inconsistent on Py3 vs. Py2.

 I found this problem while trying to run our test suite on Python 3.
 Unittest framework does an obvious {{{if isinstance(obj, type) and
 issubclass(obj, case.TestCase):}}} check when collecting the test suite
 (see
 https://github.com/python/cpython/blob/v3.5.7/Lib/unittest/loader.py#L122),
 so when we have a module-level {{{SimpleLazyObject}}} (obviously it wraps
 a different function than the one I've given here, but also a failing) it
 gets unexpectedly evaluated and raises an exception, which prevents us
 from even gathering the tests on Python 3.

 I'm not really sure why this is happening. After some brief debugging, it
 looks like {{{isinstance}}} function on Python 3 is accessing the
 {{{__class__}}} attribute (which is wrapped with the
 {{{new_method_proxy}}} function - see
 https://github.com/django/django/blob/2.2.2/django/utils/functional.py#L348)
 causing evaluation of the underlying function, whereas on Python 2.7
 {{{isinstance}}} doesn't seem to do that.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.4c2c4cca8d061498de3a5d8fef23ba8b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.