Skip to content

Commit

Permalink
Merge pull request #531 from flit/bugfix/py2_pack_fixes
Browse files Browse the repository at this point in the history
Python 2 CMSIS DFP and other fixes
  • Loading branch information
flit authored Feb 7, 2019
2 parents 9312065 + 3f1b2f2 commit 5e056ce
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 60 deletions.
109 changes: 55 additions & 54 deletions pyocd/target/pack/pack.py → pyocd/target/pack/cmsis_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ def devices(self):
def _parse_devices(self, parent):
# Extract device description elements we care about.
newState = _DeviceInfo(element=parent)
children = []
for elem in parent:
if elem.tag == 'memory':
newState.memories.append(elem)
elif elem.tag == 'algorithm':
newState.algos.append(elem)
elif elem.tag == 'debug':
newState.debugs.append(elem)
# Save any elements that we will recurse into.
elif elem.tag in ('subFamily', 'device', 'variant'):
children.append(elem)

# Push the new device description state onto the stack.
self._state_stack.append(newState)
Expand All @@ -134,7 +138,7 @@ def _parse_devices(self, parent):
self._devices.append(dev)

# Recursively process subelements.
for elem in [e for e in parent if e.tag in ('subFamily', 'device', 'variant')]:
for elem in children:
self._parse_devices(elem)

self._state_stack.pop()
Expand All @@ -149,68 +153,65 @@ def _extract_families(self):
families += [elem.attrib['DsubFamily']]
return families

def _extract_memories(self):
def _extract_items(self, state_info_name, filter):
map = {}
for state in self._state_stack:
for elem in state.memories:
for elem in getattr(state, state_info_name):
try:
if 'name' in elem.attrib:
name = elem.attrib['name']
elif 'id' in elem.attrib:
name = elem.attrib['id']
else:
# Neither option for memory name was specified, so skip this region.
LOG.debug("skipping unnamed memmory region")
continue

map[name] = elem
except KeyError as err:
filter(map, elem)
except (KeyError, ValueError) as err:
LOG.debug("error parsing CMSIS-Pack: " + str(err))
return list(map.values())

def _extract_memories(self):
def filter(map, elem):
if 'name' in elem.attrib:
name = elem.attrib['name']
elif 'id' in elem.attrib:
name = elem.attrib['id']
else:
# Neither option for memory name was specified, so skip this region.
LOG.debug("skipping unnamed memmory region")
return

map[name] = elem

return self._extract_items('memories', filter)

def _extract_algos(self):
algos = {}
for state in self._state_stack:
for elem in state.algos:
try:
# We only support Keil FLM style flash algorithms (for now).
if ('style' in elem.attrib) and (elem.attrib['style'] != 'Keil'):
LOG.debug("skipping non-Keil flash algorithm")
continue

# Both start and size are required.
start = int(elem.attrib['start'], base=0)
size = int(elem.attrib['size'], base=0)
memrange = (start, size)

# An algo with the same range as an existing algo will override the previous.
algos[memrange] = elem
except (KeyError, ValueError) as err:
# Ignore errors.
LOG.debug("error parsing CMSIS-Pack: " + str(err))
return list(algos.values())
def filter(map, elem):
# We only support Keil FLM style flash algorithms (for now).
if ('style' in elem.attrib) and (elem.attrib['style'] != 'Keil'):
LOG.debug("skipping non-Keil flash algorithm")
return None, None

# Both start and size are required.
start = int(elem.attrib['start'], base=0)
size = int(elem.attrib['size'], base=0)
memrange = (start, size)

# An algo with the same range as an existing algo will override the previous.
map[memrange] = elem

return self._extract_items('algos', filter)

def _extract_debugs(self):
debugs = {}
for state in self._state_stack:
for elem in state.debugs:
try:
if 'Pname' in elem.attrib:
name = elem.attrib['Pname']
unit = elem.attrib.get('Punit', 0)
name += str(unit)

if '*' in debugs:
debug = {}
debugs[name] = elem
else:
# No processor name was provided, so this debug element applies to
# all processors.
debugs = {'*': elem}
except (KeyError, ValueError) as err:
# Ignore errors.
LOG.debug("error parsing CMSIS-Pack: " + str(err))
return list(debugs.values())
def filter(map, elem):
if 'Pname' in elem.attrib:
name = elem.attrib['Pname']
unit = elem.attrib.get('Punit', 0)
name += str(unit)

if '*' in map:
map.clear()
map[name] = elem
else:
# No processor name was provided, so this debug element applies to
# all processors.
map.clear()
map['*'] = elem

return self._extract_items('debugs', filter)

def get_file(self, filename):
"""! @brief Return file-like object for a file within the pack.
Expand Down
6 changes: 4 additions & 2 deletions pyocd/target/pack/pack_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import logging
import six

from .pack import (CmsisPack, MalformedCmsisPackError)
from .cmsis_pack import (CmsisPack, MalformedCmsisPackError)
from ..family import FAMILIES
from .. import TARGET
from ...core.coresight_target import CoreSightTarget
Expand Down Expand Up @@ -57,7 +57,9 @@ def _find_family_class(dev):
# Scan each level of families
for familyName in dev.families:
for regex in familyInfo.matches:
if regex.fullmatch(familyName):
# Require the regex to match the entire family name.
match = regex.match(familyName)
if match and match.span() == (0, len(familyName)):
return familyInfo.klass
else:
# Default target superclass.
Expand Down
51 changes: 51 additions & 0 deletions pyocd/test/test_py3_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# pyOCD debugger
# Copyright (c) 2019 Arm Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pyocd.utility.py3_helpers import (
PY3,
iter_single_bytes,
to_bytes_safe,
to_str_safe,
)
import pytest
import six

class TestPy3Helpers(object):
def test_iter_single_bytes_bytes(self):
i = iter_single_bytes(b"1234")
assert next(i) == b'1'
assert next(i) == b'2'
assert next(i) == b'3'
assert next(i) == b'4'

def test_to_bytes_safe(self):
if PY3:
assert to_bytes_safe(b"hello") == b"hello"
assert to_bytes_safe("string") == b"string"
else:
assert to_bytes_safe(b"hello") == b"hello"
assert to_bytes_safe("string") == b"string"
assert to_bytes_safe(u"unicode") == b"unicode"

def test_to_str_safe(self):
if PY3:
assert to_str_safe(b"bytes") == "bytes"
assert to_str_safe("string") == "string"
assert to_str_safe('System Administrator\u2019s Mouse') == 'System Administrator\u2019s Mouse'
else:
assert to_str_safe(b"bytes") == "bytes"
assert to_str_safe("string") == "string"
assert to_str_safe(u"string") == "string"
assert to_str_safe(u'System Administrator\u2019s Mouse') == 'System Administrator\xe2\x80\x99s Mouse'
8 changes: 4 additions & 4 deletions pyocd/utility/py3_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
if PY3:
def to_bytes_safe(v):
if type(v) is str:
return v.encode('latin-1')
return v.encode('utf-8')
else:
return v
else:
def to_bytes_safe(v):
if type(v) is unicode:
return v.encode('latin-1')
return v.encode('utf-8')
else:
return v

Expand All @@ -52,11 +52,11 @@ def to_str_safe(v):
if type(v) is str:
return v
else:
return v.decode('latin-1')
return v.decode('utf-8')
else:
def to_str_safe(v):
if type(v) is unicode:
return v.decode('latin-1')
return v.encode('utf-8')
else:
return v

0 comments on commit 5e056ce

Please sign in to comment.