Opened 8 years ago

Last modified 4 years ago

#5474 new defect

Switch assert statements into proper exceptions

Reported by: Christopher Allan Webber Owned by:
Priority: major Milestone: 0.11.0
Component: programming Keywords: small
Cc: Parent Tickets:


See this article; we use assert as a "cheap and easy" way to throw errors if we don't want something, but it's not meant for that... and we might even miss serious / dangerous things. Once python is compiled into bytecode, assert might not run.

Change History (4)

comment:1 by Ben Sturmfels, 8 years ago

Keywords: small added

comment:2 by Ben Sturmfels, 8 years ago

The following command gives a list of files using assertions (minus tests and third-party code):

$ find -name '*.py' -exec grep -nH -e 'assert ' {} + | grep -v test | grep -v site-packages | grep -v 'Paste-2.0' | grep -v 'tox' | grep -v 'extlib'
./mediagoblin/db/        assert sess is not None, "Can't save, %r has a detached session" % self
./mediagoblin/db/        assert sess is not None, "Not going to delete detached %r" % self
./mediagoblin/db/        assert self.database_current_migration is not None
./mediagoblin/db/        assert migration_number > 0, "Migration number must be > 0!"
./mediagoblin/db/        assert migration_number not in migration_registry, \
./mediagoblin/db/        assert migration_number <= 44, ('Alembic should be used for '
./mediagoblin/media_types/stl/        assert os.path.exists(workbench_path)
./mediagoblin/plugins/piwigo/        assert not cmd_name in self._cmd_table
./mediagoblin/plugins/piwigo/        assert not self.cmd_name in self._cmd_table
./mediagoblin/plugins/piwigo/        assert self.in_pwg_session
./mediagoblin/storage/        assert (len(rem_2) > 0) or (None not in table), \
./mediagoblin/storage/        assert (len(rem_2) > 0) or (len(table) == 0), \
./mediagoblin/tools/    assert __itsda_secret is not None

comment:3 by ShawnRisk, 7 years ago


comment:4 by Ben Sturmfels, 4 years ago

Note: See TracTickets for help on using tickets.