# Buggy code
def _sector_erase(self, addresses):
    flash = None
    currentRegion = None

    for spec in addresses:
        # Convert the spec into a start and end address.
        page_addr, end_addr = self._convert_spec(spec)

        while page_addr < end_addr:
            # Look up the flash memory region for the current address.
            region = self._session.target.memory_map.get_region_for_address(page_addr)
            if region is None:
                LOG.warning("address 0x%08x is not within a memory region", page_addr)
                break
            if not region.is_flash:
                LOG.warning("address 0x%08x is not in flash", page_addr)
                break

            # Handle switching regions.
            if region is not currentRegion:
                # Clean up previous flash.
                if flash is not None:
                    flash.cleanup()

                currentRegion = region
                flash = region.flash
                flash.init(flash.Operation.ERASE)

            # Get page info for the current address.
            page_info = flash.get_page_info(page_addr)
            if not page_info:
                # Should not fail to get page info within a flash region.
                raise RuntimeError(
                    "sector address 0x%08x within flash region '%s' is invalid"
                    % (page_addr, region.name)
                )

            # Align first page address.
            delta = page_addr % page_info.size
            if delta:
                LOG.warning("sector address 0x%08x is unaligned", page_addr)
                page_addr -= delta

            # Erase this page.
            LOG.info("Erasing sector 0x%08x (%d bytes)", page_addr, page_info.size)
            flash.erase_sector(page_addr)

            page_addr += page_info.size

    if flash is not None:
        flash.cleanup()


# Traceback (TypeError)
pyocd gdb -t stm32f401re --pack /Users/matost01/Downloads/CMSIS/STM32F4xx_DFP.2.13.0.pack
0000355:INFO:pack_target:Loading CMSIS-Pack: /Users/matost01/Downloads/CMSIS/STM32F4xx_DFP.2.13.0.pack
0000457:INFO:board:Target type is stm32f401re
0000464:WARNING:flash_algo:Not enough space for flash algorithm
0000464:ERROR:__main__:uncaught exception: unsupported operand type(s) for -: 'tuple' and 'int'
Traceback (most recent call last):
File "/Users/matost01/mbed-studio-tools/python/lib/python2.7/site-packages/pyocd/__main__.py", line 304, in run
self._commands[self._args.cmd]()
File "/Users/matost01/mbed-studio-tools/python/lib/python2.7/site-packages/pyocd/__main__.py", line 463, in do_gdbserver
**sessionOptions)
File "/Users/matost01/mbed-studio-tools/python/lib/python2.7/site-packages/pyocd/core/helpers.py", line 196, in session_with_chosen_probe
session = Session(allProbes[0], options=options, **kwargs)
File "/Users/matost01/mbed-studio-tools/python/lib/python2.7/site-packages/pyocd/core/session.py", line 127, in __init__
or Board(self, self._options.get('target_override', None))
File "/Users/matost01/mbed-studio-tools/python/lib/python2.7/site-packages/pyocd/board/board.py", line 47, in __init__
self.target = TARGET[self._target_type](session)
File "/Users/matost01/mbed-studio-tools/python/lib/python2.7/site-packages/pyocd/target/pack/pack_target.py", line 31, in _pack_target__init__
super(self.__class__, self).__init__(session, self._pack_device.memory_map)
File "/Users/matost01/mbed-studio-tools/python/lib/python2.7/site-packages/pyocd/target/pack/cmsis_pack.py", line 454, in memory_map
self._build_flash_regions()
File "/Users/matost01/mbed-studio-tools/python/lib/python2.7/site-packages/pyocd/target/pack/cmsis_pack.py", line 386, in _build_flash_regions
length = nextStart - start
TypeError: unsupported operand type(s) for -: 'tuple' and 'int'```

# Fixed code
def _sector_erase(self, addresses):
    flash = None
    currentRegion = None

    for spec in addresses:
        # Convert the spec into a start and end address.
        sector_addr, end_addr = self._convert_spec(spec)

        while sector_addr < end_addr:
            # Look up the flash memory region for the current address.
            region = self._session.target.memory_map.get_region_for_address(sector_addr)
            if region is None:
                LOG.warning("address 0x%08x is not within a memory region", sector_addr)
                break
            if not region.is_flash:
                LOG.warning("address 0x%08x is not in flash", sector_addr)
                break

            # Handle switching regions.
            if region is not currentRegion:
                # Clean up previous flash.
                if flash is not None:
                    flash.cleanup()

                currentRegion = region
                flash = region.flash
                flash.init(flash.Operation.ERASE)

            # Get sector info for the current address.
            sector_info = flash.get_sector_info(sector_addr)
            if not sector_info:
                # Should not fail to get sector info within a flash region.
                raise RuntimeError(
                    "sector address 0x%08x within flash region '%s' is invalid"
                    % (sector_addr, region.name)
                )

            # Align first page address.
            delta = sector_addr % sector_info.size
            if delta:
                LOG.warning("sector address 0x%08x is unaligned", sector_addr)
                sector_addr -= delta

            # Erase this page.
            LOG.info("Erasing sector 0x%08x (%d bytes)", sector_addr, sector_info.size)
            flash.erase_sector(sector_addr)

            sector_addr += sector_info.size

    if flash is not None:
        flash.cleanup()