News for hackers. Stuff that matters.
Re:Understanding Python through its builtins by nneonneo on Sunday October 10, 2021 @03:58PM (#28821469)
Cute fact about __debug__: it is one of the only ways to get compile-time conditionals in Python. Performing a comparison with `if __debug__:` will output byte code for the ensuing statement if and only if the interpreter is in debug mode - notably, in `-O` mode, it will not even generate a load of __debug__ and a conditional jump, and acts as if the statement didn’t exist at all.
Re:Understanding Python through its builtins by Fordec on Sunday October 10, 2021 @05:11PM (#28822012)
Not going to lie, if I could enable stricter compile time conditions without debug mode that would be a very welcome RFC
Re:Understanding Python through its builtins by BiteCode_dev on Sunday October 10, 2021 @05:26PM (#28822138)
Unfortunatly you often can't use -o because of the 3rd party libs that didnt' get the memo and use assert for error checking.

We still have -X dev and sys.flags but it's runtime only.

Re:Understanding Python through its builtins by robot_no_419 on Sunday October 10, 2021 @07:53PM (#28823054)
Unless I'm misunderstanding, they are wrapping their asserts in try/catch blocks? That's... yikes.
Re:Understanding Python through its builtins by masklinn on Monday October 11, 2021 @03:06AM (#28825611)
Most likely they just `assert` error conditions and let that blow up as error reporting, rather than write a full conditional and raise a custom exception.

I’ve been guilty of that in my own code when I just want to tell myself about an error.

Re:Understanding Python through its builtins by andi999 on Monday October 11, 2021 @04:26AM (#28826056)
Isnt just assert the error condition how assert is actually intended to be used?
Re:Understanding Python through its builtins by masklinn on Monday October 11, 2021 @07:12AM (#28827013)
Depends. The issue is that since `assert` is stripped out in “O” mode, if the codebase depends on `assert` for correctness… they’re not compatible.
Re:Understanding Python through its builtins by andi999 on Tuesday October 12, 2021 @12:25AM (#28836040)
What do you mean by depends on assert for correctness? How?
Re:Understanding Python through its builtins by Doxin on Tuesday October 12, 2021 @07:30AM (#28838464)

    get_president_inside_bunker()
    assert president_is_inside_bunker()
    close_bunker_doors()
    assert bunker_doors_are_closed()
    send_nukes()
With asserts enabled this is fine. With asserts disabled you might start sending nukes while the president is still outside or the doors to the bunker are still open.
Re:Understanding Python through its builtins by andi999 on Wednesday October 13, 2021 @05:10AM (#28849923)
Thanks! I never thought anybody would do such a thing.
Re:Understanding Python through its builtins by Doxin on Thursday October 14, 2021 @04:07AM (#28861655)
No one should, but it happens.
Re:Understanding Python through its builtins by alshel on Sunday October 10, 2021 @05:50PM (#28822296)
Care to name any 3rd party libs in particular?
Re:Understanding Python through its builtins by BiteCode_dev on Sunday October 10, 2021 @05:54PM (#28822333)
Nope, I just remember my server crashing years ago so I stopped ever since.

Would be interesting to scan pypi and check if this is still a thing.

Maybe create a bot warning lib authors.

Re:Understanding Python through its builtins by zarzavat on Sunday October 10, 2021 @06:18PM (#28822498)
Seems like a better idea would be to make a separate flag "--remove-assertions" for people who desire that.
Re:Understanding Python through its builtins by BiteCode_dev on Monday October 11, 2021 @04:11AM (#28825979)
"-o" is mostly "--remove-assertions" already, so that wouldn't help.

While today's use case for assert is unit testing, the actual killer feature of the keyword is that it's removed only from prod.

The idea is that you can write things like function contracts, that are expensive, but only exist in dev.

Now, if one of you dependencies use assert for something they expect to still be in prod, which is the problem we are talking about in the first place, "-o" or "--remove-assertions" will strip their assert too, breaking their code, and hence, yours since it depends on it.

Re:Understanding Python through its builtins by athrowaway3z on Monday October 11, 2021 @04:36AM (#28826120)
Before this thread i had no idea assert was debug only in python.

Another solution would be to accept the current role of assert as quick error checking and add in debug_assert to indicate a conditional error check. The biggest issue with that approach is that a majority will suddenly ask "Wait, python has a debug mode?"

Re:Understanding Python through its builtins by BiteCode_dev on Monday October 11, 2021 @10:46AM (#28828901)
Not because the -o mode also remove any block testing on "__debug__", and that's a very useful thing as well.

The original feature is perfect. But there is 5 good years of educating users so it can be used.

Re:Understanding Python through its builtins by andi999 on Tuesday October 12, 2021 @12:24AM (#28836033)
I still do not understand what the problem is and what users need to know. So assert throws and exception in debug mode and is 'commented out' in production, right?

So the problem is some vendors ship libs that have failing asserts so cannot run in debug mode?

Re:Understanding Python through its builtins by human_error on Sunday October 10, 2021 @06:33PM (#28822584)
Starlette, SQLAlchemy.
Re:Understanding Python through its builtins by zbentley on Sunday October 10, 2021 @11:49PM (#28824453)
Django Rest Framework.
Re:Understanding Python through its builtins by globular-toast on Sunday October 10, 2021 @05:09PM (#28821998)
I knew that `assert` behaved in a similar way. Turns out it's actually equivalent to an `if __debug__:` https://docs.python.org/3/reference/simple_stmts.html#gramma...
Re:Understanding Python through its builtins by tusharsadhwani on Sunday October 10, 2021 @04:29PM (#28821692)
that is indeed interesting. Mind if I add this in the article?
Re:Understanding Python through its builtins by nneonneo on Sunday October 10, 2021 @08:15PM (#28823185)
Sure, go ahead :)