codon icon indicating copy to clipboard operation
codon copied to clipboard

Segfault when getting access to 'self.__name__' in a class

Open xiaxinmeng opened this issue 2 years ago • 5 comments

The following code defines a class Foo with a method goo. Inside the method, it attempts to print the __name__ attribute of the object it is called upon. However, the code should raise an AttributeError instead of a segmentation fault, since the __name__ attribute is not defined for instances of the Foo class.

test.py

class Foo:
    def goo(self):
    	print(self.__name__)

Foo().goo()

The actual output of Codon v016.0: Segmentation Fault

The expected output (CPython 3.10.8):

Traceback (most recent call last):
  File "/home/xxm/Desktop/Codon/test.py", line 363, in <module>
    Foo().goo()
  File "/home/xxm/Desktop/Codon/test.py", line 361, in goo
    print(self.__name__)
AttributeError: 'Foo' object has no attribute '__name__'. Did you mean: '__ne__'?

Reproduce step:

download the Pre-built binaries for Linux Type " codon run --release test.py" in the console.

Environment: Ubuntu 18.04 Codon v0.16.0 CPython 3.10.8

xiaxinmeng avatar Apr 17 '23 02:04 xiaxinmeng

it's not a codon's issue, and it probably is not an issue at all. magic variable 'name' is given by pvm to the modules, not classes. run the same code in python and you will get an AttributeError

class Foo:
    ...

>>> name in dir(Foo)
False

raceychan avatar Apr 20 '23 09:04 raceychan

@xiaxinmeng Did you mean to print class's name(?) In other words, avoid using variable names that are unclear or confusing. Instead, opt for descriptive variable names like foo, bar, and so on. The special variables __name__, __class__, __llvm_name__, and __is_static__ have their intended use.

class Foo:
    __name__: str = 'This is __name__'   # you must declare class members and their types in the preamble of each class

    def goo(self):
        print(self.__name__)


Foo().goo()  # This is __name__

Running the program with debugging enabled:

class Foo:
    def goo(self):
        print(self.__name__)


Foo().goo()

prints a proper error message:

Assert failed:  invalid index
Expression:     index >= 0
Source:         /github/workspace/codon/cir/llvm/llvisitor.cpp:3013
Aborted (core dumped)

elisbyberi avatar Apr 20 '23 22:04 elisbyberi

@elisbyberi, I apologize for any confusion my previous comment may have caused. I completely agree with you that special variables such as __name__ have their intended use. My point was that perhaps it would be beneficial to implement a checker in Codon that could identify and report more appropriate errors in the release mode, such as an IndexError or an AttributeError, instead of a segmentation fault if these attributes are misused.

xiaxinmeng avatar Apr 21 '23 02:04 xiaxinmeng

@xiaxinmeng In "release" mode, debugging is disabled and no error messages should be printed. It is recommended that you debug programs in "debug" mode, which is the default mode.

By default, running codon run fib.py executes the program in "debug" mode.

elisbyberi avatar Apr 21 '23 13:04 elisbyberi

This is bug on our side. Thank you for the report.

inumanag avatar Jul 26 '23 21:07 inumanag