summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Bishop <kanzure@gmail.com>2013-09-01 15:40:22 -0500
committerBryan Bishop <kanzure@gmail.com>2013-09-01 15:40:22 -0500
commitccc1d82d957bd98ffd60c15410eec67888f5e212 (patch)
treeb2a18451555c2aab2768425a0b5093b816a5ee39
parent74a9f14502a51f25f16be62135d28dc7adba6fbb (diff)
use a custom AddressException instead of asserts
There's no reason to have asserts thrown around in the source code like that. This replaces some of them with an AddressException and a new file called "exceptions.py" to store the exception definitions.
-rw-r--r--pokemontools/crystal.py19
-rw-r--r--pokemontools/exceptions.py8
2 files changed, 24 insertions, 3 deletions
diff --git a/pokemontools/crystal.py b/pokemontools/crystal.py
index 596f6eb..f21e097 100644
--- a/pokemontools/crystal.py
+++ b/pokemontools/crystal.py
@@ -59,6 +59,7 @@ import trainers
import pokemon_constants
import item_constants
import wram
+import exceptions
# ---- script_parse_table explanation ----
# This is an IntervalMap that keeps track of previously parsed scripts, texts
@@ -2097,7 +2098,11 @@ class ApplyMovementData:
address = self.address
# i feel like checking myself
- assert is_valid_address(address), "ApplyMovementData.parse must be given a valid address"
+ if not is_valid_address(address):
+ raise exceptions.AddressException(
+ "ApplyMovementData.parse must be given a valid address (but got {0})."
+ .format(hex(address))
+ )
current_address = copy(self.address)
start_address = copy(current_address)
@@ -3639,7 +3644,11 @@ class ItemFragment(Command):
}
def __init__(self, address=None, bank=None, map_group=None, map_id=None, debug=False, label=None):
- assert is_valid_address(address), "ItemFragment must be given a valid address"
+ if not is_valid_address(address):
+ raise exceptions.AddressException(
+ "ItemFragment must be given a valid address (but got {0})."
+ .format(hex(address))
+ )
self.address = address
self.last_address = address + self.size
self.bank = bank
@@ -4399,7 +4408,11 @@ class PeopleEvent(Command):
return output
def __init__(self, address, id, bank=None, map_group=None, map_id=None, debug=False, label=None, force=False):
- assert is_valid_address(address), "PeopleEvent must be given a valid address"
+ if not is_valid_address(address):
+ raise exceptions.AddressException(
+ "PeopleEvent must be given a valid address (but got {0})."
+ .format(hex(address))
+ )
self.address = address
self.last_address = address + people_event_byte_size
self.id = id
diff --git a/pokemontools/exceptions.py b/pokemontools/exceptions.py
new file mode 100644
index 0000000..674f829
--- /dev/null
+++ b/pokemontools/exceptions.py
@@ -0,0 +1,8 @@
+"""
+Custom exceptions used throughout the project.
+"""
+
+class AddressException(Exception):
+ """
+ There was a problem with an address. Maybe it was out of range or invalid.
+ """