Home > front end >  Hello World in Python: fatal block stack overflow?
Hello World in Python: fatal block stack overflow?

Time:03-31

I'm trying to implement a simple Hello World program in Python. The following code prints "Hello World" just fine:

def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8]):
    print(chr(data[0]), end="")
    if len(data) > 1:
        data[0]  = data.pop(1)
        raise Exception()
    

if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        try:
            main()
        except Exception as e:
            try:
                main()
            except Exception as e:
                try:
                    main()
                except Exception as e:
                    try:
                        main()
                    except Exception as e:
                        try:
                            main()
                        except Exception as e:
                            try:
                                main()
                            except Exception as e:
                                try:
                                    main()
                                except Exception as e:
                                    try:
                                        main()
                                    except Exception as e:
                                        try:
                                            main()
                                        except Exception as e:
                                            main()

...but when I try to add an exclamation point at the end, like this:

def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67]):
    if len(data) > 1:
        data[0]  = data.pop(1)
        raise Exception()
    

if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        try:
            main()
        except Exception as e:
            try:
                main()
            except Exception as e:
                try:
                    main()
                except Exception as e:
                    try:
                        main()
                    except Exception as e:
                        try:
                            main()
                        except Exception as e:
                            try:
                                main()
                            except Exception as e:
                                try:
                                    main()
                                except Exception as e:
                                    try:
                                        main()
                                    except Exception as e:
                                        try:
                                            main()
                                        except Exception as e:
                                            try:
                                                main()
                                            except Exception as e:
                                                main()

...I get a Fatal Python error: XXX block stack overflow.

I've tried adding more try/except clauses to catch the block stack overflow error, but nothing seems to work. What the heck is the block stack, and why would such a straightforward approach not work?

CodePudding user response:

This is a known issue that's been "fixed". However, it's more the exact error message, and nesting depth limit, that changed. Python doesn't really intend to support arbitrarily deep static nesting. It would if doing so were trivial, but it's not trivial. Rather than "a straightforward approach", I expect most programmers would consider such deep nesting to be "a nightmare" ;-)

CodePudding user response:

With Python 3.10 I get an error message that's more self-explanatory:

    try:
    ^^^^
SyntaxError: too many statically nested blocks

You delved too greedily, and too deep. Restructure the code to not have this many blocks nested inside of each other.

CodePudding user response:

I can only guess that you're trying to experiment with and understand exception handling in Python. Perhaps this will help:

def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67]):
    print(chr(data[0]), end="")
    if len(data) > 1:
        data[0]  = data.pop(1)
        raise Exception()

if __name__ == '__main__':
    while True:
        try:
            main()
            # when the *data* list contains only one element
            # main() will implicitly return None - i.e., no exception
            break
        except Exception:
            pass

Output:

Hello world!

Having said that, your code could be simplified as:

def main(data=[72, 29, 7, 0, 3, -79, 55, 24, 3, -6, -8, -67]):
    print(chr(data[0]), end="")
    data[0]  = data.pop(1)

if __name__ == '__main__':
    while True:
        try:
            main()
        except IndexError:
            break

CodePudding user response:

Do not complicate the code, just write the whole code on paper and you easily get rid of the error!

  • Related