Skip to content

Running Python with optimizations enabled strips assertions #4

@rkiyanchuk

Description

@rkiyanchuk

Just wanted to point out that if Python is run with -O (optimize) option, it will strip all assert statements and these checks will effectively disappear:

assert len(salt) == ARGON2_SALT_LENGTH

You can read about it more in Python docs.

TL;DR:

Assertions should not be used to test for failure cases that can occur because of bad user input or operating system/environment failures, such as a file not being found. Instead, you should raise an exception, or print an error message, or whatever is appropriate. One important reason why assertions should only be used for self-tests of the program is that assertions can be disabled at compile time.
If Python is started with the -O option, then assertions will be stripped out and not evaluated.

It might still be okay, depending on what the intent is, just wanted to make sure you're aware of this nuance.

PS: Thanks for posting about your work in ModernCrypto mailing list 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions