Skip to content

Commit 0d523f9

Browse files
committed
Merge branch 'release/0.7.4'
2 parents e74a37f + 8e8d8f9 commit 0d523f9

File tree

6 files changed

+49
-30
lines changed

6 files changed

+49
-30
lines changed

README.rst

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ License
3939
Changelog
4040
~~~~~~~~~
4141

42+
- v0.7.4
43+
44+
- Switch to using preferred encoding from ``locale`` module for converting
45+
strings to binary. This should prevent some lingering ``UnicodeEncodeError``
46+
crashes on Python 2.7.
47+
4248
- v0.7.3
4349

4450
- Fix a couple crashes when certain functions that expect ``str`` were passed

drmaa/const.py

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525

2626
from __future__ import absolute_import, print_function, unicode_literals
2727

28+
import locale
29+
30+
# Encoding to use for passing strings to C library
31+
ENCODING = locale.getpreferredencoding()
32+
2833
# drmaa_get_attribute()
2934
ATTR_BUFFER = 1024
3035

drmaa/helpers.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from ctypes import (byref, c_uint, create_string_buffer, POINTER, pointer,
3232
sizeof)
3333

34-
from drmaa.const import ATTR_BUFFER, NO_MORE_ELEMENTS
34+
from drmaa.const import ATTR_BUFFER, ENCODING, NO_MORE_ELEMENTS
3535
from drmaa.errors import error_buffer
3636
from drmaa.wrappers import (drmaa_attr_names_t, drmaa_attr_values_t,
3737
drmaa_get_attribute, drmaa_get_attribute_names,
@@ -61,10 +61,10 @@ class BoolConverter(object):
6161

6262
def __init__(self, true=b'y', false=b'n'):
6363
if isinstance(true, str):
64-
true = true.encode()
64+
true = true.encode(ENCODING)
6565
self.true = true
6666
if isinstance(false, str):
67-
false = false.encode()
67+
false = false.encode(ENCODING)
6868
self.false = false
6969

7070
def to_drmaa(self, value):
@@ -104,7 +104,8 @@ def __get__(self, *args):
104104

105105
Version = namedtuple("Version", "major minor")
106106
if sys.version_info < (3, 0):
107-
Version.__str__ = lambda x: "{0}.{1}".format(x.major, x.minor).encode()
107+
Version.__str__ = lambda x: "{0}.{1}".format(x.major,
108+
x.minor).encode(ENCODING)
108109
else:
109110
Version.__str__ = lambda x: "{0}.{1}".format(x.major, x.minor)
110111

@@ -139,15 +140,15 @@ def __init__(self, name, type_converter=None):
139140
implementation. See BoolConverter for an example.
140141
"""
141142
if isinstance(name, str):
142-
name = name.encode()
143+
name = name.encode(ENCODING)
143144
self.name = name
144145
self.converter = type_converter
145146

146147
def __set__(self, instance, value):
147148
if self.converter:
148149
v = self.converter.to_drmaa(value)
149150
elif isinstance(value, str):
150-
v = value.encode()
151+
v = value.encode(ENCODING)
151152
else:
152153
v = value
153154
c(drmaa_set_attribute, instance, self.name, v)
@@ -174,7 +175,7 @@ class VectorAttribute(object):
174175

175176
def __init__(self, name):
176177
if isinstance(name, str):
177-
name = name.encode()
178+
name = name.encode(ENCODING)
178179
self.name = name
179180

180181
def __set__(self, instance, value):
@@ -195,11 +196,12 @@ class DictAttribute(object):
195196

196197
def __init__(self, name):
197198
if isinstance(name, str):
198-
name = name.encode()
199+
name = name.encode(ENCODING)
199200
self.name = name
200201

201202
def __set__(self, instance, value):
202-
v = ["{0}={1}".format(k, v).encode() for (k, v) in value.items()]
203+
v = ["{0}={1}".format(k, v).encode(ENCODING) for (k, v) in
204+
value.items()]
203205
c(drmaa_set_vector_attribute, instance, self.name,
204206
string_vector(v))
205207

@@ -296,7 +298,7 @@ def string_vector(v):
296298
vlen = len(v)
297299
values = (STRING * (vlen + 1))()
298300
for i, el in enumerate(v):
299-
values[i] = STRING(el.encode() if isinstance(el, str) else el)
301+
values[i] = STRING(el.encode(ENCODING) if isinstance(el, str) else el)
300302
values[vlen] = STRING()
301303
return values
302304

drmaa/session.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
from ctypes import byref, c_int, create_string_buffer, pointer, POINTER, sizeof
2828

2929
from drmaa.const import (BLOCK_EMAIL, DEADLINE_TIME, DURATION_HLIMIT,
30-
DURATION_SLIMIT, ERROR_PATH, INPUT_PATH, JOB_CATEGORY,
31-
JOB_IDS_SESSION_ALL, JOB_IDS_SESSION_ANY, JOB_NAME,
32-
JobState, JobControlAction, JobSubmissionState,
33-
JOIN_FILES, JS_STATE, NATIVE_SPECIFICATION,
34-
OUTPUT_PATH, REMOTE_COMMAND, SIGNAL_BUFFER, START_TIME,
35-
status_to_string, string_to_control_action,
36-
TIMEOUT_NO_WAIT, TIMEOUT_WAIT_FOREVER, TRANSFER_FILES,
37-
V_ARGV, V_EMAIL, V_ENV, WCT_HLIMIT, WCT_SLIMIT, WD)
30+
DURATION_SLIMIT, ENCODING, ERROR_PATH, INPUT_PATH,
31+
JOB_CATEGORY, JOB_IDS_SESSION_ALL, JOB_IDS_SESSION_ANY,
32+
JOB_NAME, JobState, JobControlAction,
33+
JobSubmissionState, JOIN_FILES, JS_STATE,
34+
NATIVE_SPECIFICATION, OUTPUT_PATH, REMOTE_COMMAND,
35+
SIGNAL_BUFFER, START_TIME, status_to_string,
36+
string_to_control_action, TIMEOUT_NO_WAIT,
37+
TIMEOUT_WAIT_FOREVER, TRANSFER_FILES, V_ARGV, V_EMAIL,
38+
V_ENV, WCT_HLIMIT, WCT_SLIMIT, WD)
3839
from drmaa.helpers import (adapt_rusage, Attribute, attribute_names_iterator,
3940
BoolConverter, c, DictAttribute, IntConverter,
4041
run_bulk_job, SessionStringAttribute,
@@ -376,7 +377,7 @@ def control(jobId, operation):
376377
or jobs submitted via native utilities.
377378
"""
378379
if isinstance(jobId, str):
379-
jobId = jobId.encode()
380+
jobId = jobId.encode(ENCODING)
380381
c(drmaa_control, jobId, string_to_control_action(operation))
381382

382383
# takes string list, num value and boolean, no return value
@@ -401,20 +402,24 @@ def synchronize(jobIds, timeout=-1, dispose=False):
401402
data record, which includes a record of the job's consumption of
402403
system resources during its execution and other statistical
403404
information. If set to True, the DRM will dispose of the job's
404-
data record at the end of the synchroniize() call. If set to
405+
data record at the end of the synchronize() call. If set to
405406
False, the data record will be left for future access via the
406-
wait() method.
407+
wait() method. It is the responsibility of the application to
408+
make sure that either `synchronize()` or `wait()`is called for
409+
every job. Not doing so creates a memory leak. Note that calling
410+
synchronize() with dispose set to true flushes all accounting
411+
information for all jobs in the list.
407412
408413
To avoid thread race conditions in multithreaded applications, the
409414
DRMAA implementation user should explicitly synchronize this call with
410415
any other job submission calls or control calls that may change the
411416
number of remote jobs.
412417
413-
If the call exits before the
414-
timeout has elapsed, all the jobs have been waited on or there was an
415-
interrupt. If the invocation exits on timeout, an ExitTimeoutException
416-
is thrown. The caller should check system time before and after this
417-
call in order to be sure of how much time has passed.
418+
If the call exits before the timeout has elapsed, all the jobs have
419+
been waited on or there was an interrupt. If the invocation exits on
420+
timeout, an ExitTimeoutException is thrown. The caller should check
421+
system time before and after this call in order to be sure of how much
422+
time has passed.
418423
"""
419424
if dispose:
420425
d = 1
@@ -461,7 +466,7 @@ def wait(jobId, timeout=-1):
461466
jid_out = create_string_buffer(128)
462467
rusage = pointer(POINTER(drmaa_attr_values_t)())
463468
if isinstance(jobId, str):
464-
jobId = jobId.encode()
469+
jobId = jobId.encode(ENCODING)
465470
c(drmaa_wait, jobId, jid_out, sizeof(jid_out), byref(stat), timeout,
466471
rusage)
467472
res_usage = adapt_rusage(rusage)
@@ -509,7 +514,7 @@ def jobStatus(jobId):
509514
"""
510515
status = c_int()
511516
if isinstance(jobId, str):
512-
jobId = jobId.encode()
517+
jobId = jobId.encode(ENCODING)
513518
c(drmaa_job_ps, jobId, byref(status))
514519
return status_to_string(status.value)
515520

drmaa/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
:author: Dan Blanchard (dblanchard@ets.org)
2323
'''
2424

25-
__version__ = '0.7.3'
25+
__version__ = '0.7.4'
2626
VERSION = tuple(int(x) for x in __version__.split('.'))

drmaa/wrappers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
POINTER, RTLD_GLOBAL, sizeof, Structure)
3030
from ctypes.util import find_library
3131

32+
from drmaa.const import ENCODING
3233
from drmaa.errors import error_check, error_buffer
3334

3435

@@ -68,7 +69,7 @@
6869

6970
def py_drmaa_init(contact=None):
7071
if isinstance(contact, str):
71-
contact = contact.encode()
72+
contact = contact.encode(ENCODING)
7273
return _lib.drmaa_init(contact, error_buffer, sizeof(error_buffer))
7374

7475
_lib.drmaa_exit.argtypes = [c_char_p, c_size_t]

0 commit comments

Comments
 (0)