Re: [Python-ideas] "old" values in postconditions

Hmm, I was wrong: there is no reliable way to get the code of a lambda function. If it was possible to execute all code paths of the function, we could monkey patch the builtins so { } used our own custom set class. Alternatively, the decorator could also accept a string. Or maybe we could send a PEP to add the .func_code attribute to lambdas as well as normal functions. There’s also a technique online where they find the lambda’s source by locating the file the function was defined in and then removing the irrelevant parts, but that just doesn’t sound practical to me. There’s also MacroPy. I think the best solution would be to mock the old object and record the operations done to the object, like the other replier gave a PoC of. Proposed syntax from icontract import post, old @post(lambda: ..., key=old.self.key(), ) Sent from my iPhone

Ew, magic. `{{foo}}` is already valid syntax (though it will always fail). I don't like this path. If the proposal requires new syntax or magic, it will be less likely to get accepted or even pip'd. Remember also that PyPy, IronPython, and Jython are still alive, and the latter two are still aiming for Python 3 (PyPy 3 is already available). On Tue, Sep 25, 2018 at 12:52 PM James Lu <jamtlu@gmail.com> wrote:
Hmm, I was wrong: there is no reliable way to get the code of a lambda function.
You may be looking for inspect.signature(func).
Or maybe we could send a PEP to add the .func_code attribute to lambdas as well as normal functions.
The new name is `.__code__`. There is some documentation of code objects in the `inspect` documentation (names beginning with `co_`). More documentation: https://docs.python.org/3.7/reference/datamodel.html#index-55
There’s also a technique online where they find the lambda’s source by locating the file the function was defined in and then removing the irrelevant parts, but that just doesn’t sound practical to me.
I'm surprised you haven't found inspect.getsource(func) Doesn't always work if the source is not associated to the definition of func, such as with functions defined in C. That shouldn't be a problem with lambdas. You might need to worry about file permission. I don't know if other interpreters will find it difficult to support `inspect`. You may be looking for `ast.parse(inspect.getsource(func))`.

Hi James and Franklin, getsource() definitely does not work. I tried for a long, long time to make it work and finally gave up. I parse in icontract the whole file where the lambda function resides and use asttokens to locate the node of the lambda (along some tree traversing upwards and making assumptions where the condition lambda lives). Have a look at: https://github.com/Parquery/icontract/blob/391d43005287831892b19dfdcbcfd3d48... and https://github.com/Parquery/icontract/blob/391d43005287831892b19dfdcbcfd3d48... On Tue, 25 Sep 2018 at 19:48, James Lu <jamtlu@gmail.com> wrote:

Ah. It wasn't clear to me from the thread that James was using `inspect`. As it happens, not only does getsource give more than it should, it also gives less than it should. The following bug still exists in 3.6.1. It was closed as a wontfix bug back in Python 2 because, I presume, fixing it would require changing code objects. https://bugs.python.org/issue17631 Regardless, I think it'd be better not to rely on such magic for the proposal. While it's a fun puzzle, whether it's possible or not, it still modifies the syntax and/or scoping and/or evaluation order rules of Python. On Tue, Sep 25, 2018 at 1:54 PM Marko Ristin-Kaufmann <marko.ristin@gmail.com> wrote:

Ew, magic. `{{foo}}` is already valid syntax (though it will always fail). I don't like this path. If the proposal requires new syntax or magic, it will be less likely to get accepted or even pip'd. Remember also that PyPy, IronPython, and Jython are still alive, and the latter two are still aiming for Python 3 (PyPy 3 is already available). On Tue, Sep 25, 2018 at 12:52 PM James Lu <jamtlu@gmail.com> wrote:
Hmm, I was wrong: there is no reliable way to get the code of a lambda function.
You may be looking for inspect.signature(func).
Or maybe we could send a PEP to add the .func_code attribute to lambdas as well as normal functions.
The new name is `.__code__`. There is some documentation of code objects in the `inspect` documentation (names beginning with `co_`). More documentation: https://docs.python.org/3.7/reference/datamodel.html#index-55
There’s also a technique online where they find the lambda’s source by locating the file the function was defined in and then removing the irrelevant parts, but that just doesn’t sound practical to me.
I'm surprised you haven't found inspect.getsource(func) Doesn't always work if the source is not associated to the definition of func, such as with functions defined in C. That shouldn't be a problem with lambdas. You might need to worry about file permission. I don't know if other interpreters will find it difficult to support `inspect`. You may be looking for `ast.parse(inspect.getsource(func))`.

Hi James and Franklin, getsource() definitely does not work. I tried for a long, long time to make it work and finally gave up. I parse in icontract the whole file where the lambda function resides and use asttokens to locate the node of the lambda (along some tree traversing upwards and making assumptions where the condition lambda lives). Have a look at: https://github.com/Parquery/icontract/blob/391d43005287831892b19dfdcbcfd3d48... and https://github.com/Parquery/icontract/blob/391d43005287831892b19dfdcbcfd3d48... On Tue, 25 Sep 2018 at 19:48, James Lu <jamtlu@gmail.com> wrote:

Ah. It wasn't clear to me from the thread that James was using `inspect`. As it happens, not only does getsource give more than it should, it also gives less than it should. The following bug still exists in 3.6.1. It was closed as a wontfix bug back in Python 2 because, I presume, fixing it would require changing code objects. https://bugs.python.org/issue17631 Regardless, I think it'd be better not to rely on such magic for the proposal. While it's a fun puzzle, whether it's possible or not, it still modifies the syntax and/or scoping and/or evaluation order rules of Python. On Tue, Sep 25, 2018 at 1:54 PM Marko Ristin-Kaufmann <marko.ristin@gmail.com> wrote:
participants (3)
-
Franklin? Lee
-
James Lu
-
Marko Ristin-Kaufmann