Skip to content

Instantly share code, notes, and snippets.

@sirkonst
Last active December 29, 2015 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirkonst/6eff694c4546700417ea to your computer and use it in GitHub Desktop.
Save sirkonst/6eff694c4546700417ea to your computer and use it in GitHub Desktop.
How to get segmentation fault with crazy __ (python 2 and 3)
__obj = object()
# uncomment for fix:
# _Class__obj = __obj
def func():
print('func:', __obj)
class Class:
def class_func(self):
# uncomment for get !!SEGMENTATION FAULT!!:
# nonlocal __obj
print('class_func:', __obj)
print('---')
func()
# OK:
# -> func: <object object at 0x7f515eccf070>
print('---')
c = Class()
c.class_func()
# WTF??>:
# -> NameError: name '_Class__obj' is not defined
@sirkonst
Copy link
Author

https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references:

Any identifier of the form _spam (at least two leading underscores, at most one trailing underscore) is textually replaced with classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.

@sirkonst
Copy link
Author

Segmentation fault report - https://bugs.python.org/issue25973

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment