Skip to content

Commit

Permalink
Merge branch 'release/0.7.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-blanchard committed Jan 6, 2014
2 parents e74a37f + 8e8d8f9 commit 0d523f9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ License
Changelog
~~~~~~~~~

- v0.7.4

- Switch to using preferred encoding from ``locale`` module for converting
strings to binary. This should prevent some lingering ``UnicodeEncodeError``
crashes on Python 2.7.

- v0.7.3

- Fix a couple crashes when certain functions that expect ``str`` were passed
Expand Down
5 changes: 5 additions & 0 deletions drmaa/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@

from __future__ import absolute_import, print_function, unicode_literals

import locale

# Encoding to use for passing strings to C library
ENCODING = locale.getpreferredencoding()

# drmaa_get_attribute()
ATTR_BUFFER = 1024

Expand Down
22 changes: 12 additions & 10 deletions drmaa/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ctypes import (byref, c_uint, create_string_buffer, POINTER, pointer,
sizeof)

from drmaa.const import ATTR_BUFFER, NO_MORE_ELEMENTS
from drmaa.const import ATTR_BUFFER, ENCODING, NO_MORE_ELEMENTS
from drmaa.errors import error_buffer
from drmaa.wrappers import (drmaa_attr_names_t, drmaa_attr_values_t,
drmaa_get_attribute, drmaa_get_attribute_names,
Expand Down Expand Up @@ -61,10 +61,10 @@ class BoolConverter(object):

def __init__(self, true=b'y', false=b'n'):
if isinstance(true, str):
true = true.encode()
true = true.encode(ENCODING)
self.true = true
if isinstance(false, str):
false = false.encode()
false = false.encode(ENCODING)
self.false = false

def to_drmaa(self, value):
Expand Down Expand Up @@ -104,7 +104,8 @@ def __get__(self, *args):

Version = namedtuple("Version", "major minor")
if sys.version_info < (3, 0):
Version.__str__ = lambda x: "{0}.{1}".format(x.major, x.minor).encode()
Version.__str__ = lambda x: "{0}.{1}".format(x.major,
x.minor).encode(ENCODING)
else:
Version.__str__ = lambda x: "{0}.{1}".format(x.major, x.minor)

Expand Down Expand Up @@ -139,15 +140,15 @@ def __init__(self, name, type_converter=None):
implementation. See BoolConverter for an example.
"""
if isinstance(name, str):
name = name.encode()
name = name.encode(ENCODING)
self.name = name
self.converter = type_converter

def __set__(self, instance, value):
if self.converter:
v = self.converter.to_drmaa(value)
elif isinstance(value, str):
v = value.encode()
v = value.encode(ENCODING)
else:
v = value
c(drmaa_set_attribute, instance, self.name, v)
Expand All @@ -174,7 +175,7 @@ class VectorAttribute(object):

def __init__(self, name):
if isinstance(name, str):
name = name.encode()
name = name.encode(ENCODING)
self.name = name

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

def __init__(self, name):
if isinstance(name, str):
name = name.encode()
name = name.encode(ENCODING)
self.name = name

def __set__(self, instance, value):
v = ["{0}={1}".format(k, v).encode() for (k, v) in value.items()]
v = ["{0}={1}".format(k, v).encode(ENCODING) for (k, v) in
value.items()]
c(drmaa_set_vector_attribute, instance, self.name,
string_vector(v))

Expand Down Expand Up @@ -296,7 +298,7 @@ def string_vector(v):
vlen = len(v)
values = (STRING * (vlen + 1))()
for i, el in enumerate(v):
values[i] = STRING(el.encode() if isinstance(el, str) else el)
values[i] = STRING(el.encode(ENCODING) if isinstance(el, str) else el)
values[vlen] = STRING()
return values

Expand Down
41 changes: 23 additions & 18 deletions drmaa/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
from ctypes import byref, c_int, create_string_buffer, pointer, POINTER, sizeof

from drmaa.const import (BLOCK_EMAIL, DEADLINE_TIME, DURATION_HLIMIT,
DURATION_SLIMIT, ERROR_PATH, INPUT_PATH, JOB_CATEGORY,
JOB_IDS_SESSION_ALL, JOB_IDS_SESSION_ANY, JOB_NAME,
JobState, JobControlAction, JobSubmissionState,
JOIN_FILES, JS_STATE, NATIVE_SPECIFICATION,
OUTPUT_PATH, REMOTE_COMMAND, SIGNAL_BUFFER, START_TIME,
status_to_string, string_to_control_action,
TIMEOUT_NO_WAIT, TIMEOUT_WAIT_FOREVER, TRANSFER_FILES,
V_ARGV, V_EMAIL, V_ENV, WCT_HLIMIT, WCT_SLIMIT, WD)
DURATION_SLIMIT, ENCODING, ERROR_PATH, INPUT_PATH,
JOB_CATEGORY, JOB_IDS_SESSION_ALL, JOB_IDS_SESSION_ANY,
JOB_NAME, JobState, JobControlAction,
JobSubmissionState, JOIN_FILES, JS_STATE,
NATIVE_SPECIFICATION, OUTPUT_PATH, REMOTE_COMMAND,
SIGNAL_BUFFER, START_TIME, status_to_string,
string_to_control_action, TIMEOUT_NO_WAIT,
TIMEOUT_WAIT_FOREVER, TRANSFER_FILES, V_ARGV, V_EMAIL,
V_ENV, WCT_HLIMIT, WCT_SLIMIT, WD)
from drmaa.helpers import (adapt_rusage, Attribute, attribute_names_iterator,
BoolConverter, c, DictAttribute, IntConverter,
run_bulk_job, SessionStringAttribute,
Expand Down Expand Up @@ -376,7 +377,7 @@ def control(jobId, operation):
or jobs submitted via native utilities.
"""
if isinstance(jobId, str):
jobId = jobId.encode()
jobId = jobId.encode(ENCODING)
c(drmaa_control, jobId, string_to_control_action(operation))

# takes string list, num value and boolean, no return value
Expand All @@ -401,20 +402,24 @@ def synchronize(jobIds, timeout=-1, dispose=False):
data record, which includes a record of the job's consumption of
system resources during its execution and other statistical
information. If set to True, the DRM will dispose of the job's
data record at the end of the synchroniize() call. If set to
data record at the end of the synchronize() call. If set to
False, the data record will be left for future access via the
wait() method.
wait() method. It is the responsibility of the application to
make sure that either `synchronize()` or `wait()`is called for
every job. Not doing so creates a memory leak. Note that calling
synchronize() with dispose set to true flushes all accounting
information for all jobs in the list.
To avoid thread race conditions in multithreaded applications, the
DRMAA implementation user should explicitly synchronize this call with
any other job submission calls or control calls that may change the
number of remote jobs.
If the call exits before the
timeout has elapsed, all the jobs have been waited on or there was an
interrupt. If the invocation exits on timeout, an ExitTimeoutException
is thrown. The caller should check system time before and after this
call in order to be sure of how much time has passed.
If the call exits before the timeout has elapsed, all the jobs have
been waited on or there was an interrupt. If the invocation exits on
timeout, an ExitTimeoutException is thrown. The caller should check
system time before and after this call in order to be sure of how much
time has passed.
"""
if dispose:
d = 1
Expand Down Expand Up @@ -461,7 +466,7 @@ def wait(jobId, timeout=-1):
jid_out = create_string_buffer(128)
rusage = pointer(POINTER(drmaa_attr_values_t)())
if isinstance(jobId, str):
jobId = jobId.encode()
jobId = jobId.encode(ENCODING)
c(drmaa_wait, jobId, jid_out, sizeof(jid_out), byref(stat), timeout,
rusage)
res_usage = adapt_rusage(rusage)
Expand Down Expand Up @@ -509,7 +514,7 @@ def jobStatus(jobId):
"""
status = c_int()
if isinstance(jobId, str):
jobId = jobId.encode()
jobId = jobId.encode(ENCODING)
c(drmaa_job_ps, jobId, byref(status))
return status_to_string(status.value)

Expand Down
2 changes: 1 addition & 1 deletion drmaa/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
:author: Dan Blanchard ([email protected])
'''

__version__ = '0.7.3'
__version__ = '0.7.4'
VERSION = tuple(int(x) for x in __version__.split('.'))
3 changes: 2 additions & 1 deletion drmaa/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
POINTER, RTLD_GLOBAL, sizeof, Structure)
from ctypes.util import find_library

from drmaa.const import ENCODING
from drmaa.errors import error_check, error_buffer


Expand Down Expand Up @@ -68,7 +69,7 @@

def py_drmaa_init(contact=None):
if isinstance(contact, str):
contact = contact.encode()
contact = contact.encode(ENCODING)
return _lib.drmaa_init(contact, error_buffer, sizeof(error_buffer))

_lib.drmaa_exit.argtypes = [c_char_p, c_size_t]
Expand Down

0 comments on commit 0d523f9

Please sign in to comment.