On Thu, Sep 30, 2021 at 01:25:42PM -0300, Soni L. wrote:

>     def foo():
>         raise Bar
>     def baz() with Bar:
>         foo()
>     baz()
> 
> would make a RuntimeError, because foo raised a Bar and the VM sees that
> Bar is in baz's with.

So the syntax "with SomeException" in a function declaration would mean 
that the function can not raise SomeException?

Wouldn't "without SomeException" be a less misleading name then?


> *except "raise" opcodes SKIP checking these (within the context of the
> function), so the following:
> 
>     def baz() with Bar:
>         raise Bar
>     baz()
> 
> raises a Bar, not a RuntimeError from a Bar.

Every time I think I understand your proposal, I read some more about 
it, and get more confused.

So the "with SomeException" syntax guarantees that the function *won't* 
raise SomeException (raising RuntimeError instead), unless it does raise 
SomeException, which under some unspecified conditions it will allow it
through.

And this syntax can only apply to an entire function at a time, 
you can't apply it to a block of code like a with-statement unless you 
move the entire block into its own function.


> You can then document your exceptions as you normally do: "This raises
> Bar when so and so are invalid", and then add "with Bar" to your
> function. the VM then makes sure that Bar is only raised when so and so
> are invalid, but under no other circumstances.

Wait. This is even more magical. So if I document "this only raises Bar 
if P == NP" the compiler will solve the P versus NP problem?

https://en.wikipedia.org/wiki/P_versus_NP_problem

Okay, I jest a bit. A more realistic example:


    def myfunc(s:str) with ValueError:
        """This raises ValueError only if s is a palindrome."""
        assert isinstance(s, str)
        do_stuff()  # Guard: ValueError --> RuntimeError
        if s == s[::-1]:
            raise ValueError("spam")  # No guard.
        n = math.sqrt(float(s))  # Guard: ValueError --> RuntimeError
        ... # more code here is still guarded


Surely you aren't suggesting that the compiler infers meaning from the 
natural language docstring, but if not, how does it know which parts of 
the function to guard and which parts not to?

How does the VM know that ValueError("spam") is okay but 
ValueError("math domain error") is not? What is the actual criteria used 
to allow some ValueErrors through and not others?


> e.g. someone
> monkeypatched your function to try and raise a Bar outside of the place
> it was meant to raise a Bar? it'll raise a RuntimeError instead.

So if my function is this:

    def function(arg) with ValueError:
        """Raises ValueError if arg == 0."""
        if arg = 0:
            raise ValueError
        return arg + 1

you are saying that if I monkeypatch it to this:

    def patched(arg):
        if arg == 0:
            return 1
        raise ValueError

    function = patched
    function(99)

that somehow it will raise RuntimeError? I don't want to believe that 
this is what you actually mean, but if it isn't this, then I don't know 
what you do mean.

I wish your proposals and ideas would be more *precise* in their 
specifications. This is not the first time that I have found it very 
hard to work out precisely what you are suggesting and what you are not. 


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/NZAUJB7NVNK5PQWYIVB3VJS5DNAOOP3E/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to