On 19/01/2021 5:48 pm, Guido van Rossum wrote:
I'm not clear on how you plan to implement this in CPython.

I can totally see that if a Python function calls another Python function, you can avoid the C stack frame and hence you can have as many Python call levels as you want.

However, there are many scenarios where a Python function calls a C function (e.g. `filter()`, or `dict.__setitem__()`) and that C function at some point calls a Python function (e.g. the `__hash__()` method of the key, or even the `__del__()` method of the value being replaced). Then that Python function can recursively do a similar thing.

Indeed, that is the second case below, where a Python __add__
function recursively performs addition. Most likely, the C stack will get exhausted before the recursion limit is hit, so you'll get a StackOverflow exception.


Are you proposing to also support that kind of thing to go on for a million levels of C stack frames?

No, most likely 10k to 20k calls before a StackOverflow exception.


(Do we even have a cross-platform way of avoiding segfaults due to C stack overflow?)

Arithmetic and comparisons on pointers from within the C stack may not strictly conform to the C standard, but it works just fine. It's pretty standard VM implementation stuff. All the JVMs do this sort of thing.

IMO practically beats purity in this case, and crashing less has to be a good thing.


A rather hacky proof of concept for the stack overflow handling is here:

https://github.com/python/cpython/compare/master...markshannon:pep-overflow-implementation


Cheers,
Mark.


On Tue, Jan 19, 2021 at 5:38 AM Mark Shannon <m...@hotpy.org <mailto:m...@hotpy.org>> wrote:

    Hi everyone,

    It's time for yet another PEP :)

    Fortunately, this one is a small one that doesn't change much.
    It's aim is to make the VM more robust.

    Abstract
    ========

    This PEP proposes that machine stack overflow is treated differently
    from runaway recursion. This would allow programs to set the maximum
    recursion depth to fit their needs and provide additional safety
    guarantees.

    The following program will run safely to completion:

          sys.setrecursionlimit(1_000_000)

          def f(n):
              if n:
                  f(n-1)

          f(500_000)

    The following program will raise a StackOverflow, without causing a VM
    crash:

          sys.setrecursionlimit(1_000_000)

          class X:
              def __add__(self, other):
                  return self + other

          X() + 1

    -----------

    The full PEP can be found here:
    https://www.python.org/dev/peps/pep-0651

    As always, comments are welcome.

    Cheers,
    Mark.
    _______________________________________________
    Python-Dev mailing list -- python-dev@python.org
    <mailto:python-dev@python.org>
    To unsubscribe send an email to python-dev-le...@python.org
    <mailto:python-dev-le...@python.org>
    https://mail.python.org/mailman3/lists/python-dev.python.org/
    Message archived at
    
https://mail.python.org/archives/list/python-dev@python.org/message/ZY32N43YZJM3WYXSVD7OCGVNDGPR6DUM/
    Code of Conduct: http://python.org/psf/codeofconduct/



--
--Guido van Rossum (python.org/~guido <http://python.org/~guido>)
/Pronouns: he/him //(why is my pronoun here?)/ <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/PYJHVAA63AWOL72OJMNFDY7VODIT5KM7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to