# Buggy code
def create_flash(self):
    """! @brief Instantiates flash objects for memory regions.

    This init task iterates over flash memory regions and for each one creates the Flash
    instance. It uses the flash_algo and flash_class properties of the region to know how
    to construct the flash object.
    """
    for region in self.memory_map.get_regions_of_type(MemoryType.FLASH):
        # If a path to an FLM file was set on the region, examine it first.
        if region.flm is not None:
            flmPath = self.session.find_user_file(None, [region.flm])
            if flmPath is not None:
                logging.info("creating flash algo from: %s", flmPath)
                packAlgo = PackFlashAlgo(flmPath)
                algo = packAlgo.get_pyocd_flash_algo(
                    max(s[1] for s in packAlgo.sector_sizes),
                    self.memory_map.get_first_region_of_type(MemoryType.RAM),
                )

                # If we got a valid algo from the FLM, set it on the region. This will then
                # be used below.
                if algo is not None:
                    region.algo = algo
            else:
                logging.warning("Failed to find FLM file: %s", region.flm)

        # If the constructor of the region's flash class takes the flash_algo arg, then we
        # need the region to have a flash algo dict to pass to it. Otherwise we assume the
        # algo is built-in.
        klass = region.flash_class
        argspec = getargspec(klass.__init__)
        if "flash_algo" in argspec.args:
            if region.algo is not None:
                obj = klass(self, region.algo)
            else:
                logging.warning("flash region '%s' has no flash algo" % region.name)
                continue
        else:
            obj = klass(self)

        # Set the region in the flash instance.
        obj.region = region

        # Store the flash object back into the memory region.
        region.flash = obj


# 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 create_flash(self):
    """! @brief Instantiates flash objects for memory regions.

    This init task iterates over flash memory regions and for each one creates the Flash
    instance. It uses the flash_algo and flash_class properties of the region to know how
    to construct the flash object.
    """
    for region in self.memory_map.get_regions_of_type(MemoryType.FLASH):
        # If a path to an FLM file was set on the region, examine it first.
        if region.flm is not None:
            flmPath = self.session.find_user_file(None, [region.flm])
            if flmPath is not None:
                logging.info("creating flash algo from: %s", flmPath)
                packAlgo = PackFlashAlgo(flmPath)
                if self.session.options.get("debug.log_flm_info", False):
                    logging.debug("Flash algo info: %s", packAlgo.flash_info)
                page_size = packAlgo.page_size
                if page_size <= 32:
                    page_size = min(s[1] for s in packAlgo.sector_sizes)
                algo = packAlgo.get_pyocd_flash_algo(
                    page_size, self.memory_map.get_first_region_of_type(MemoryType.RAM)
                )

                # If we got a valid algo from the FLM, set it on the region. This will then
                # be used below.
                if algo is not None:
                    region.algo = algo
            else:
                logging.warning("Failed to find FLM file: %s", region.flm)

        # If the constructor of the region's flash class takes the flash_algo arg, then we
        # need the region to have a flash algo dict to pass to it. Otherwise we assume the
        # algo is built-in.
        klass = region.flash_class
        argspec = getargspec(klass.__init__)
        if "flash_algo" in argspec.args:
            if region.algo is not None:
                obj = klass(self, region.algo)
            else:
                logging.warning("flash region '%s' has no flash algo" % region.name)
                continue
        else:
            obj = klass(self)

        # Set the region in the flash instance.
        obj.region = region

        # Store the flash object back into the memory region.
        region.flash = obj