Kodi Community Forum

Full Version: assert statement does not work in Kodi-Python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
The title says it all. It looks like Python assert statement does not work in Kodi's built-in Python runtime. All assert statements are silently ignored.
IMO it is not good when the core language specifications are not followed.
Is it really ignored? probably bad that I use it and never noticed it wasn't testing the condition. Thanks for the info... I agree with you core should have stock functions.
It is 'stock' and clearly documented: https://docs.python.org/2/reference/simp...-statement
(2016-09-16, 18:05)takoi Wrote: [ -> ]It is 'stock' and clearly documented: https://docs.python.org/2/reference/simp...-statement

I understand what you are implying. But still all mainstream Python implementations support assert statement, and there are a number of libraries that rely on it (testing frameworks mostly). So it should not be silently disabled.
(2016-09-21, 10:56)Roman_V_M Wrote: [ -> ]
(2016-09-16, 18:05)takoi Wrote: [ -> ]It is 'stock' and clearly documented: https://docs.python.org/2/reference/simp...-statement

I understand what you are implying. But still all mainstream Python implementations support assert statement, and there are a number of libraries that rely on it (testing frameworks mostly). So it should not be silently disabled.
I'm not sure you did..
As it says link I gave, the python the reference documentation, assert is for debugging. And like in pretty much any other language out there they are stripped in release builds. It is you who are using assert incorrectly when assuming it will always be executed!

Kodi does not have any special runtime here. It runs standard CPython with standard release mode in release builds, as it should.
(2016-09-22, 19:48)takoi Wrote: [ -> ]
(2016-09-21, 10:56)Roman_V_M Wrote: [ -> ]
(2016-09-16, 18:05)takoi Wrote: [ -> ]It is 'stock' and clearly documented: https://docs.python.org/2/reference/simp...-statement

I understand what you are implying. But still all mainstream Python implementations support assert statement, and there are a number of libraries that rely on it (testing frameworks mostly). So it should not be silently disabled.
I'm not sure you did..
As it says link I gave, the python the reference documentation, assert is for debugging. And like in pretty much any other language out there they are stripped in release builds. It is you who are using assert incorrectly when assuming it will always be executed!

Kodi does not have any special runtime here. It runs standard CPython with standard release mode in release builds, as it should.

Forgive my being blunt, but you have no idea what you are talking about. All reference CPython implementations for all major OSes have assert enabled and optimization disabled. Again, there is a number of Python libraries that use assert, starting from Python built-in unittest module, so it's a part of language specification.

For some reason Python in Kodi is compiled with optimization enabled by default (assert not working and compiled modules have .pyo extensions instead of .pyc), but this, in-fact, is a non-standard implementation.
All .pyo in /usr/lib/python here. If you think it's wrong to enabled optimization feel free to pr it, but you're expected to argue for why you think it's incorrect other than 'everyone else does it' (which they don't even do).
out of all the things wrong with kodi python he finds this to nitpick on lol
(2016-09-22, 22:07)takoi Wrote: [ -> ]All .pyo in /usr/lib/python here. If you think it's wrong to enabled optimization feel free to pr it, but you're expected to argue for why you think it's incorrect other than 'everyone else does it' (which they don't even do).

Ubuntu 16.04 LTS - no such thing. In fact, this is quite unusual. The only reasonable explanation I can think of is that those .pyo files have been created by Kodi, because Kodi on Linux uses the sustem-wide Python.

Bottom line: currently the optimization option (-O) is kept for purely historical reasons and almost never used because it does not provide any real optimization. This is a quote from "An Introduction to Python" co-authored by Guido van Rossum himself, so in Python world this is as official as it can be:
Quote:
  • When the Python interpreter is invoked with the -O flag, optimized code is generated and stored in ‘.pyo’ files. The optimizer currently doesn't help much; it only removes assert statements. When -O is used, all bytecode is optimized; .pyc files are ignored and .py files are compiled to optimized bytecode.
  • Passing two -O flags to the Python interpreter (-OO) will cause the bytecode compiler to perform optimizations that could in some rare cases result in malfunctioning programs. Currently only __doc__ strings are removed from the bytecode, resulting in more compact ‘.pyo’ files. Since some programs may rely on having these available, you should only use this option if you know what you're doing.

Moreover, there is a number of mainstream Python libraries, including components of the Standard Library, that rely on working assert statement. So there is no reasonable grounds to disable assert in Kodi.
We set PYTHONOPTIMIZE=1 when loading the interpreter on most platforms. IIRC the decision for this was a bit controversial back then. You should open a PR to revert this behavior, so we have a place for discussion. Imho its useless and should be disabled.
(2016-09-23, 21:58)wsnipex Wrote: [ -> ]We set PYTHONOPTIMIZE=1 when loading the interpreter on most platforms. IIRC the decision for this was a bit controversial back then. You should open a PR to revert this behavior, so we have a place for discussion. Imho its useless and should be disabled.

that explains asserts being ignored... Thanks Big Grin

is -o or -oo flags used at all?
PYTHONOPTIMIZE=1 equals python -o
It is pretty useless, and we may remove it, but you should still follow spec and not rely on assert for anything but debugging. That's what exceptions are forWink
indeed.
(2016-09-24, 09:20)takoi Wrote: [ -> ]It is pretty useless, and we may remove it, but you should still follow spec and not rely on assert for anything but debugging. That's what exceptions are forWink

What "specs" exactly? assert is a part of Python specification and is used in many mainstream Python libraries, and not only for tests but also to signal about some abnormal conditions. Let's take this one, for example. You may not like it, but it's a fact. What you say maybe true for some other languages like C++ or C# where assert is indeed a purely a debugging tool, but not for Python. Personally, I don't use assert for anything but tests, but possible problems with other Python libraries, both standard and third-party, is what bothers me.
Pages: 1 2