Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Replace usage of assert by explicit exceptions (fixes #592)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Nov 25, 2015
1 parent 928997c commit 90cb2e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -24,6 +24,9 @@ This document describes changes between each past release.

- Fixed a few details in quickstart docs since backends are not Redis by default
anymore
- Replace usage of ``assert`` by explicit exceptions since the former can
be ignored when python is ran with ``-O`` (fixes #592)


2.11.0 (2015-11-17)
-------------------
Expand Down
15 changes: 9 additions & 6 deletions cliquet/resource/__init__.py
Expand Up @@ -705,9 +705,10 @@ def _raise_304_if_not_modified(self, record=None):
if_none_match = decode_header(if_none_match)

try:
assert if_none_match[0] == if_none_match[-1] == '"'
if not (if_none_match[0] == if_none_match[-1] == '"'):
raise ValueError()
modified_since = int(if_none_match[1:-1])
except (IndexError, AssertionError, ValueError):
except (IndexError, ValueError):
if if_none_match != '*':
error_details = {
'location': 'headers',
Expand Down Expand Up @@ -744,9 +745,10 @@ def _raise_412_if_modified(self, record=None):
modified_since = -1 # Always raise.
elif if_match:
try:
assert if_match[0] == if_match[-1] == '"'
if not (if_match[0] == if_match[-1] == '"'):
raise ValueError()
modified_since = int(if_match[1:-1])
except (IndexError, AssertionError, ValueError):
except (IndexError, ValueError):
message = ("Invalid value for If-Match. The value should "
"be integer between double quotes.")
error_details = {
Expand Down Expand Up @@ -957,10 +959,11 @@ def _extract_pagination_rules_from_token(self, limit, sorting):
if token:
try:
tokeninfo = json.loads(decode64(token))
assert isinstance(tokeninfo, dict)
if not isinstance(tokeninfo, dict):
raise ValueError()
last_record = tokeninfo['last_record']
offset = tokeninfo['offset']
except (ValueError, KeyError, TypeError, AssertionError):
except (ValueError, KeyError, TypeError):
error_msg = '_token has invalid content'
error_details = {
'location': 'querystring',
Expand Down
6 changes: 4 additions & 2 deletions cliquet/storage/postgresql/__init__.py
Expand Up @@ -108,7 +108,8 @@ def initialize_schema(self):
expected = migration[0]
current = self._get_installed_version()
error_msg = "Expected version %s. Found version %s."
assert expected == current, error_msg % (expected, current)
if expected != current:
raise AssertionError(error_msg % (expected, current))

logger.info('Migrate schema from version %s to %s.' % migration)
filepath = 'migration_%03d_%03d.sql' % migration
Expand Down Expand Up @@ -139,7 +140,8 @@ def _check_database_encoding(self):
result = conn.execute(query)
record = result.fetchone()
encoding = record['encoding'].lower()
assert encoding == 'utf8', 'Unexpected database encoding %s' % encoding
if encoding != 'utf8': # pragma: no cover
raise AssertionError('Unexpected database encoding %s' % encoding)

def _get_installed_version(self):
"""Return current version of schema or None if not any found.
Expand Down

0 comments on commit 90cb2e6

Please sign in to comment.