classification
Title: Segmentation fault with nonlocal and two underscores
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: acucci, benjamin.peterson, brett.cannon, georg.brandl, ncoghlan, python-dev, serhiy.storchaka, sirkonst, yselivanov, ztane
Priority: normal Keywords:

Created on 2015-12-29 11:40 by sirkonst, last changed 2016-03-05 17:21 by ncoghlan. This issue is now closed.

Files
File name Uploaded Description Edit
test_issue25973.py serhiy.storchaka, 2015-12-29 12:45
Messages (10)
msg257174 - (view) Author: Konstantin Enchant (sirkonst) Date: 2015-12-29 11:40
Code:
# -------------------------------
__obj = object()

class Foo:
    def f1(self):
        nonlocal __obj

f = Foo()
f.f1()  # <-- segmentation fault
# -------------------------------
msg257177 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-12-29 12:45
I get a crash when just compile the example.
msg257180 - (view) Author: Alessandro Cucci (acucci) * Date: 2015-12-29 12:58
I don't think the problem is about the underscores, since this work...

class Foo:
    def f1(self):
        __obj = object()
        def f2():
            nonlocal __obj
            __obj = []
        f2()
        return isinstance(__obj, list)
        
f = Foo()
print(f.f1())
msg257182 - (view) Author: Konstantin Enchant (sirkonst) Date: 2015-12-29 13:10
The problem happens only when "nonlocal __something" in a class method. In your case f2() isn't class method.

More interesting behavior with underscores - https://gist.github.com/sirkonst/6eff694c4546700417ea
msg257183 - (view) Author: Alessandro Cucci (acucci) * Date: 2015-12-29 13:21
quoting the docs:

The statement [nonlocal] allows encapsulated code to rebind variables outside of the local scope BESIDES the global (module) scope.
msg257185 - (view) Author: Konstantin Enchant (sirkonst) Date: 2015-12-29 13:34
Yes. Case:

# -------------------
class A:
    def f(self):
        nonlocal __x
# -------------------

must raises SyntaxError like case:

# -------------------
class A:
    def f(self):
        nonlocal x

>> SyntaxError: no binding for nonlocal 'x' found
# -------------------

but doesn't crash with SegFault as it is now.
msg257197 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-12-29 16:09
New changeset 4fa8c0c69ee9 by Benjamin Peterson in branch '3.5':
make recording and reporting errors and nonlocal and global directives more robust (closes #25973)
https://hg.python.org/cpython/rev/4fa8c0c69ee9

New changeset c64e68d703cf by Benjamin Peterson in branch 'default':
merge 3.5 (#25973)
https://hg.python.org/cpython/rev/c64e68d703cf
msg261200 - (view) Author: Antti Haapala (ztane) * Date: 2016-03-04 21:47
So no fix for 3.4 for an obvious SIGSEGV?

    % python3  
    Python 3.4.3 (default, Mar 26 2015, 22:03:40) 
    [GCC 4.9.2] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class A:
    ...     def f(self):
    ...         nonlocal __x
    ... 
    [4]    19173 segmentation fault (core dumped)  python3
msg261201 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-03-04 21:59
Only security fixes are accepted for 3.4.
msg261216 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-03-05 17:21
And as some additional background as to why segmentation faults provoked by Python code aren't currently considered a security bug: since CPython doesn't include a security sandbox, we're already relying entirely on the OS to provide process isolation.

That OS level security boundary isn't affected by whether the code is running "normally", or in a modified state following a deliberately triggered segmentation fault.
History
Date User Action Args
2016-03-05 17:21:34ncoghlansetmessages: + msg261216
2016-03-04 21:59:16serhiy.storchakasetmessages: + msg261201
2016-03-04 21:47:58ztanesetnosy: + ztane
messages: + msg261200
2015-12-29 16:09:27python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg257197

resolution: fixed
stage: resolved
2015-12-29 13:34:45sirkonstsetmessages: + msg257185
2015-12-29 13:21:26acuccisetmessages: + msg257183
2015-12-29 13:10:42sirkonstsetmessages: + msg257182
2015-12-29 12:58:27acuccisetnosy: + acucci
messages: + msg257180
2015-12-29 12:45:25serhiy.storchakasetfiles: + test_issue25973.py

messages: + msg257177
2015-12-29 12:32:45serhiy.storchakasetnosy: + brett.cannon, georg.brandl, ncoghlan, benjamin.peterson, serhiy.storchaka, yselivanov
2015-12-29 11:48:44SilentGhostsetcomponents: + Interpreter Core
versions: + Python 3.5, Python 3.6, - Python 3.4
2015-12-29 11:40:04sirkonstcreate