Skip to content

Commit

Permalink
Merge pull request #374 from flit/feature/py3_support
Browse files Browse the repository at this point in the history
Python 3 support
  • Loading branch information
flit authored Jul 17, 2018
2 parents e7d0841 + 24c12cd commit d7d0904
Show file tree
Hide file tree
Showing 52 changed files with 826 additions and 678 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ htmlcov
# Common virtualenv environment names
/env
/env3
/venv
/venv*
/dev
/windev

Expand Down
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
language: python
dist: trusty
sudo: false
language: python
python:
- "2.7"
- "3.6"
# command to install dependencies
install: "pip install -r dev-requirements.txt"
install:
- pip install -r dev-requirements.txt
# command to run tests
script: pytest --cache-clear
script:
- pytest --cache-clear
7 changes: 6 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
include pytest.ini
include Doxyfile
include dev-requirements.txt
recursive-include binaries *
recursive-include elf_files *
recursive-include pyOCD *.ld *.txt *.c *.bat
recursive-include src *.ld *.txt *.c *.bat *.py
recursive-include test *.py *.sh
global-exclude .DS_Store
16 changes: 8 additions & 8 deletions pyOCD/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
limitations under the License.
"""

import board
import core
import debug
import flash
import gdbserver
import target
import utility
import coresight
from . import board
from . import core
from . import debug
from . import flash
from . import gdbserver
from . import target
from . import utility
from . import coresight

from ._version import version as __version__

4 changes: 2 additions & 2 deletions pyOCD/board/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
limitations under the License.
"""

import mbed_board
from mbed_board import MbedBoard
from . import mbed_board
from .mbed_board import MbedBoard
42 changes: 24 additions & 18 deletions pyOCD/board/mbed_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
limitations under the License.
"""

from __future__ import print_function
import sys, os
import logging, array
from time import sleep
import colorama
from board import Board
import six
from .board import Board
from ..pyDAPAccess import DAPAccess
from .board_ids import BOARD_ID_TO_INFO

Expand Down Expand Up @@ -97,8 +99,12 @@ def listConnectedBoards(dap_class=DAPAccess):
all_mbeds = MbedBoard.getAllConnectedBoards(dap_class, close=True,
blocking=False)
if len(all_mbeds) > 0:
print(colorama.Fore.BLUE + "## => Board Name | Unique ID")
print("-- -- ----------------------")
for index, mbed in enumerate(sorted(all_mbeds, key=lambda x:x.getInfo())):
print("%d => %s boardId => %s" % (index, mbed.getInfo().encode('ascii', 'ignore'), mbed.unique_id))
print(colorama.Fore.GREEN + "%2d => %s | %s" % (
index, mbed.getInfo(),
colorama.Fore.CYAN + mbed.unique_id) + colorama.Style.RESET_ALL)
else:
print("No available boards are connected")

Expand Down Expand Up @@ -155,9 +161,9 @@ def chooseBoard(dap_class=DAPAccess, blocking=True, return_first=False,
for mbed in all_mbeds:
head, sep, tail = mbed.unique_id.lower().rpartition(board_id)
highlightedId = head + colorama.Fore.RED + sep + colorama.Style.RESET_ALL + tail
print "%s | %s" % (
mbed.getInfo().encode('ascii', 'ignore'),
highlightedId)
print("%s | %s" % (
mbed.getInfo(),
highlightedId))
return None
all_mbeds = new_mbed_list

Expand All @@ -173,20 +179,20 @@ def chooseBoard(dap_class=DAPAccess, blocking=True, return_first=False,
if return_first:
all_mbeds = all_mbeds[0:1]

# Ask use to select boards if there is more than 1 left
# Ask user to select boards if there is more than 1 left
if len(all_mbeds) > 1:
all_mbeds = sorted(all_mbeds, key=lambda x:x.getInfo())
print colorama.Fore.BLUE + "## => Board Name | Unique ID"
print "-- -- ----------------------"
print(colorama.Fore.BLUE + "## => Board Name | Unique ID")
print("-- -- ----------------------")
for index, mbed in enumerate(all_mbeds):
print colorama.Fore.GREEN + "%2d => %s | %s" % (
index, mbed.getInfo().encode('ascii', 'ignore'),
colorama.Fore.CYAN + mbed.unique_id)
print colorama.Fore.RED + " q => Quit"
print(colorama.Fore.GREEN + "%2d => %s | %s" % (
index, mbed.getInfo(),
colorama.Fore.CYAN + mbed.unique_id))
print(colorama.Fore.RED + " q => Quit")
while True:
print colorama.Style.RESET_ALL
print "Enter the number of the board to connect:"
line = raw_input("> ")
print(colorama.Style.RESET_ALL)
print("Enter the number of the board to connect:")
line = six.moves.input("> ")
valid = False
if line.strip().lower() == 'q':
return None
Expand All @@ -196,10 +202,10 @@ def chooseBoard(dap_class=DAPAccess, blocking=True, return_first=False,
except ValueError:
pass
if not valid:
print colorama.Fore.YELLOW + "Invalid choice: %s\n" % line
print(colorama.Fore.YELLOW + "Invalid choice: %s\n" % line)
for index, mbed in enumerate(all_mbeds):
print colorama.Fore.GREEN + "%d => %s" % (index, mbed.getInfo())
print colorama.Fore.RED + "q => Exit"
print(colorama.Fore.GREEN + "%d => %s" % (index, mbed.getInfo()))
print(colorama.Fore.RED + "q => Exit")
else:
break
all_mbeds = all_mbeds[ch:ch + 1]
Expand Down
2 changes: 1 addition & 1 deletion pyOCD/core/coresight_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def selected_core(self):
return self.cores[self._selected_core]

def select_core(self, num):
if not self.cores.has_key(num):
if num not in self.cores:
raise ValueError("invalid core number")
logging.debug("selected core #%d" % num)
self._selected_core = num
Expand Down
2 changes: 1 addition & 1 deletion pyOCD/core/memory_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
__all__ = ['MemoryRange', 'MemoryRegion', 'MemoryMap', 'RamRegion', 'RomRegion',
'FlashRegion', 'DeviceRegion', 'AliasRegion']

MAP_XML_HEADER = """<?xml version="1.0"?>
MAP_XML_HEADER = b"""<?xml version="1.0"?>
<!DOCTYPE memory-map PUBLIC "+//IDN gnu.org//DTD GDB Memory Map V1.0//EN" "http://sourceware.org/gdb/gdb-memory-map.dtd">
"""

Expand Down
2 changes: 1 addition & 1 deletion pyOCD/coresight/ap.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def readBlockMemoryUnaligned8(self, addr, size):

# try to read aligned block of 32bits
if (size >= 4):
mem = self.readBlockMemoryAligned32(addr, size/4)
mem = self.readBlockMemoryAligned32(addr, size//4)
res += conversion.u32leListToByteList(mem)
size -= 4*len(mem)
addr += 4*len(mem)
Expand Down
4 changes: 2 additions & 2 deletions pyOCD/coresight/dap.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def find_aps(self):
if idr == 0:
break
logging.info("AP#%d IDR = 0x%08x", ap_num, idr)
except Exception, e:
except Exception as e:
logging.error("Exception reading AP#%d IDR: %s", ap_num, repr(e))
break
ap_num += 1
Expand Down Expand Up @@ -242,7 +242,7 @@ def writeAP(self, addr, data):

# Don't need to write CSW if it's not changing value.
if ap_regaddr == AP_REG['CSW']:
if self._csw.has_key(ap_sel) and data == self._csw[ap_sel]:
if ap_sel in self._csw and data == self._csw[ap_sel]:
if LOG_DAP:
self.logger.info("writeAP:%06d cached (addr=0x%08x) = 0x%08x", num, addr, data)
return
Expand Down
8 changes: 4 additions & 4 deletions pyOCD/debug/breakpoints/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ def get_breakpoint_type(self, addr):
return bp.type if (bp is not None) else None

def filter_memory(self, addr, size, data):
for provider in [p for p in self._providers.itervalues() if p.do_filter_memory]:
for provider in [p for p in self._providers.values() if p.do_filter_memory]:
data = provider.filter_memory(addr, size, data)
return data

def filter_memory_unaligned_8(self, addr, size, data):
for provider in [p for p in self._providers.itervalues() if p.do_filter_memory]:
for provider in [p for p in self._providers.values() if p.do_filter_memory]:
for i, d in enumerate(data):
data[i] = provider.filter_memory(addr + i, 8, d)
return data

def filter_memory_aligned_32(self, addr, size, data):
for provider in [p for p in self._providers.itervalues() if p.do_filter_memory]:
for provider in [p for p in self._providers.values() if p.do_filter_memory]:
for i, d in enumerate(data):
data[i] = provider.filter_memory(addr + i, 32, d)
return data
Expand All @@ -170,7 +170,7 @@ def remove_all_breakpoints(self):

def _flush_all(self):
# Flush all providers.
for provider in self._providers.itervalues():
for provider in self._providers.values():
provider.flush()

def flush(self):
Expand Down
2 changes: 1 addition & 1 deletion pyOCD/debug/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def readCoreRegistersRaw(self, reg_list):
reg_set = set(reg_list)

# Get list of values we have cached.
cached_set = set(r for r in reg_list if self._cache.has_key(r))
cached_set = set(r for r in reg_list if r in self._cache)
self._metrics.hits += len(cached_set)

# Read uncached registers from the target.
Expand Down
5 changes: 3 additions & 2 deletions pyOCD/debug/semihost.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import threading
import socket
import traceback
import six
import pyOCD
from ..gdbserver.gdb_socket import GDBSocket
from ..gdbserver.gdb_websocket import GDBWebSocket
Expand Down Expand Up @@ -171,7 +172,7 @@ def __init__(self):
}

def _is_valid_fd(self, fd):
return self.open_files.has_key(fd) and self.open_files[fd] is not None
return fd in self.open_files and self.open_files[fd] is not None

def cleanup(self):
for f in (self.open_files[k] for k in self.open_files if k > STDERR_FD):
Expand Down Expand Up @@ -217,7 +218,7 @@ def write(self, fd, ptr, length):
try:
f = self.open_files[fd]
if 'b' not in f.mode:
data = unicode(data)
data = six.text_type(data)
f.write(data)
f.flush()
return 0
Expand Down
4 changes: 2 additions & 2 deletions pyOCD/flash/flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import logging
from struct import unpack
from time import time
from flash_builder import FlashBuilder
from .flash_builder import FlashBuilder

DEFAULT_PAGE_PROGRAM_WEIGHT = 0.130
DEFAULT_PAGE_ERASE_WEIGHT = 0.048
Expand Down Expand Up @@ -88,7 +88,7 @@ def __init__(self, target, flash_algo):
self.min_program_length = flash_algo.get('min_program_length', 0)

# Check for double buffering support.
if flash_algo.has_key('page_buffers'):
if 'page_buffers' in flash_algo:
self.page_buffers = flash_algo['page_buffers']
else:
self.page_buffers = [self.begin_data]
Expand Down
2 changes: 1 addition & 1 deletion pyOCD/gdbserver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
limitations under the License.
"""

from gdbserver import GDBServer
from .gdbserver import GDBServer
17 changes: 9 additions & 8 deletions pyOCD/gdbserver/context_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ..utility import conversion
from . import signals
import logging
import six

# Maps the fault code found in the IPSR to a GDB signal value.
FAULT = [
Expand Down Expand Up @@ -49,12 +50,12 @@ def getRegisterContext(self):
return hexadecimal dump of registers as expected by GDB
"""
logging.debug("GDB getting register context")
resp = ''
reg_num_list = map(lambda reg:reg.reg_num, self._register_list)
resp = b''
reg_num_list = [reg.reg_num for reg in self._register_list]
vals = self._context.readCoreRegistersRaw(reg_num_list)
#print("Vals: %s" % vals)
for reg, regValue in zip(self._register_list, vals):
resp += conversion.u32beToHex8le(regValue)
resp += six.b(conversion.u32beToHex8le(regValue))
logging.debug("GDB reg: %s = 0x%X", reg.name, regValue)

return resp
Expand Down Expand Up @@ -92,7 +93,7 @@ def gdbGetRegister(self, reg):
if reg < len(self._register_list):
regName = self._register_list[reg].name
regValue = self._context.readCoreRegisterRaw(regName)
resp = conversion.u32beToHex8le(regValue)
resp = six.b(conversion.u32beToHex8le(regValue))
logging.debug("GDB reg: %s = 0x%X", regName, regValue)
return resp

Expand All @@ -103,9 +104,9 @@ def getTResponse(self, forceSignal=None):
The current value of the important registers (sp, lr, pc).
"""
if forceSignal is not None:
response = 'T' + conversion.byteToHex2(forceSignal)
response = six.b('T' + conversion.byteToHex2(forceSignal))
else:
response = 'T' + conversion.byteToHex2(self.getSignalValue())
response = six.b('T' + conversion.byteToHex2(self.getSignalValue()))

# Append fp(r7), sp(r13), lr(r14), pc(r15)
response += self.getRegIndexValuePairs([7, 13, 14, 15])
Expand All @@ -131,10 +132,10 @@ def getRegIndexValuePairs(self, regIndexList):
for the T response string. NN is the index of the
register to follow MMMMMMMM is the value of the register.
"""
str = ''
str = b''
regList = self._context.readCoreRegistersRaw(regIndexList)
for regIndex, reg in zip(regIndexList, regList):
str += conversion.byteToHex2(regIndex) + ':' + conversion.u32beToHex8le(reg) + ';'
str += six.b(conversion.byteToHex2(regIndex) + ':' + conversion.u32beToHex8le(reg) + ';')
return str

def getMemoryMapXML(self):
Expand Down
Loading

0 comments on commit d7d0904

Please sign in to comment.