Skip to content

Commit 6313307

Browse files
authored
Introduce use_tmpcpy parameter to Library constructor (#166) (#172)
This patch introduces a parameter to the constructor for the `JLink` instance, as well as the `Library` instance to prevent the internal behaviour of copying the library to allow for multiple debuggers to be used at the same time in earlier versions of the SDK.
1 parent 34083a2 commit 6313307

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

pylink/jlink.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def wrapper(self, *args, **kwargs):
247247
return _interface_required
248248

249249
def __init__(self, lib=None, log=None, detailed_log=None, error=None, warn=None, unsecure_hook=None,
250-
serial_no=None, ip_addr=None, open_tunnel=False):
250+
serial_no=None, ip_addr=None, open_tunnel=False, use_tmpcpy=True):
251251
"""Initializes the J-Link interface object.
252252
253253
Note:
@@ -279,6 +279,7 @@ def __init__(self, lib=None, log=None, detailed_log=None, error=None, warn=None,
279279
of ``open`` method.
280280
If ``None``, the driver will not be opened automatically
281281
(however, it is still closed when exiting the context manager).
282+
use_tmpcpy (bool): True to load a temporary copy of J-Link DLL
282283
283284
Returns:
284285
``None``
@@ -289,7 +290,7 @@ def __init__(self, lib=None, log=None, detailed_log=None, error=None, warn=None,
289290
self._initialized = False
290291

291292
if lib is None:
292-
lib = library.Library()
293+
lib = library.Library(use_tmpcpy=use_tmpcpy)
293294

294295
if lib.dll() is None:
295296
raise TypeError('Expected to be given a valid DLL.')

pylink/library.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def find_library_darwin(cls):
260260
if f.startswith(dll):
261261
yield os.path.join(dir_path, f)
262262

263-
def __init__(self, dllpath=None):
263+
def __init__(self, dllpath=None, use_tmpcpy=True):
264264
"""Initializes an instance of a ``Library``.
265265
266266
Loads the default J-Link DLL if ``dllpath`` is ``None``, otherwise
@@ -269,6 +269,7 @@ def __init__(self, dllpath=None):
269269
Args:
270270
self (Library): the ``Library`` instance
271271
dllpath (str): the DLL to load into the library
272+
use_tmpcpy (bool): True to load a temporary copy of J-Link DLL
272273
273274
Returns:
274275
``None``
@@ -278,6 +279,7 @@ def __init__(self, dllpath=None):
278279
self._path = None
279280
self._windows = sys.platform.startswith('win')
280281
self._cygwin = sys.platform.startswith('cygwin')
282+
self._use_tmpcpy = use_tmpcpy
281283
self._temp = None
282284

283285
if self._windows or self._cygwin:
@@ -387,28 +389,33 @@ def load(self, path=None):
387389
else:
388390
suffix = '.so'
389391

390-
# Copy the J-Link DLL to a temporary file. This will be cleaned up the
391-
# next time we load a DLL using this library or if this library is
392-
# cleaned up.
393-
tf = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
394-
with open(tf.name, 'wb') as outputfile:
395-
with open(self._path, 'rb') as inputfile:
396-
outputfile.write(inputfile.read())
392+
lib_path = self._path
397393

398-
# This is needed to work around a WindowsError where the file is not
399-
# being properly cleaned up after exiting the with statement.
400-
tf.close()
394+
if self._use_tmpcpy:
395+
# Copy the J-Link DLL to a temporary file. This will be cleaned up the
396+
# next time we load a DLL using this library or if this library is
397+
# cleaned up.
398+
tf = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
399+
with open(tf.name, 'wb') as outputfile:
400+
with open(self._path, 'rb') as inputfile:
401+
outputfile.write(inputfile.read())
401402

402-
self._temp = tf
403-
self._lib = ctypes.cdll.LoadLibrary(tf.name)
403+
# This is needed to work around a WindowsError where the file is not
404+
# being properly cleaned up after exiting the with statement.
405+
tf.close()
406+
407+
lib_path = tf.name
408+
self._temp = tf
409+
410+
self._lib = ctypes.cdll.LoadLibrary(lib_path)
404411

405412
if self._windows:
406413
# The J-Link library uses a mix of __cdecl and __stdcall function
407414
# calls. While this is fine on a nix platform or in cygwin, this
408415
# causes issues with Windows, where it expects the __stdcall
409416
# methods to follow the standard calling convention. As a result,
410417
# we have to convert them to windows function calls.
411-
self._winlib = ctypes.windll.LoadLibrary(tf.name)
418+
self._winlib = ctypes.windll.LoadLibrary(lib_path)
412419
for stdcall in self._standard_calls_:
413420
if hasattr(self._winlib, stdcall):
414421
# Backwards compatibility. Some methods do not exist on

0 commit comments

Comments
 (0)