Skip to content

Commit 43ed63a

Browse files
committed
Fix crash when some functions are called with integers instead of strings.
1 parent c2c5592 commit 43ed63a

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

drmaa/helpers.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
drmaa_version, STRING)
4747

4848

49+
# Python 3 compatability help
50+
if sys.version_info < (3, 0):
51+
bytes = str
52+
str = unicode
53+
54+
4955
_BUFLEN = ATTR_BUFFER
5056

5157

@@ -54,10 +60,10 @@ class BoolConverter(object):
5460
"""Helper class to convert to/from bool attributes."""
5561

5662
def __init__(self, true=b'y', false=b'n'):
57-
if not isinstance(true, bytes):
63+
if isinstance(true, str):
5864
true = true.encode()
5965
self.true = true
60-
if not isinstance(false, bytes):
66+
if isinstance(false, str):
6167
false = false.encode()
6268
self.false = false
6369

@@ -132,15 +138,15 @@ def __init__(self, name, type_converter=None):
132138
a converter to translate attribute values to/from the underlying
133139
implementation. See BoolConverter for an example.
134140
"""
135-
if not isinstance(name, bytes):
141+
if isinstance(name, str):
136142
name = name.encode()
137143
self.name = name
138144
self.converter = type_converter
139145

140146
def __set__(self, instance, value):
141147
if self.converter:
142148
v = self.converter.to_drmaa(value)
143-
elif not isinstance(value, bytes):
149+
elif isinstance(value, str):
144150
v = value.encode()
145151
else:
146152
v = value
@@ -167,7 +173,7 @@ class VectorAttribute(object):
167173
"""
168174

169175
def __init__(self, name):
170-
if not isinstance(name, bytes):
176+
if isinstance(name, str):
171177
name = name.encode()
172178
self.name = name
173179

@@ -188,7 +194,7 @@ class DictAttribute(object):
188194
"""
189195

190196
def __init__(self, name):
191-
if not isinstance(name, bytes):
197+
if isinstance(name, str):
192198
name = name.encode()
193199
self.name = name
194200

@@ -290,7 +296,7 @@ def string_vector(v):
290296
vlen = len(v)
291297
values = (STRING * (vlen + 1))()
292298
for i, el in enumerate(v):
293-
values[i] = STRING(el.encode() if not isinstance(el, bytes) else el)
299+
values[i] = STRING(el.encode() if isinstance(el, str) else el)
294300
values[vlen] = STRING()
295301
return values
296302

drmaa/session.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from __future__ import absolute_import, print_function, unicode_literals
2424

25+
import sys
2526
from collections import namedtuple
2627
from ctypes import byref, c_int, create_string_buffer, pointer, POINTER, sizeof
2728

@@ -50,6 +51,12 @@
5051
py_drmaa_exit, py_drmaa_init)
5152

5253

54+
# Python 3 compatability help
55+
if sys.version_info < (3, 0):
56+
bytes = str
57+
str = unicode
58+
59+
5360
JobInfo = namedtuple("JobInfo",
5461
"""jobId hasExited hasSignal terminatedSignal hasCoreDump
5562
wasAborted exitStatus resourceUsage""")
@@ -368,8 +375,8 @@ def control(jobId, operation):
368375
jobs submitted by other DRMAA session in other DRMAA implementations
369376
or jobs submitted via native utilities.
370377
"""
371-
if not isinstance(jobId, bytes):
372-
jobId = jobId.encode()
378+
if isinstance(jobId, str):
379+
jobId = jobId.encode()
373380
c(drmaa_control, jobId, string_to_control_action(operation))
374381

375382
# takes string list, num value and boolean, no return value
@@ -453,9 +460,9 @@ def wait(jobId, timeout=-1):
453460
stat = c_int()
454461
jid_out = create_string_buffer(128)
455462
rusage = pointer(POINTER(drmaa_attr_values_t)())
456-
if not isinstance(jobId, bytes):
457-
jobId = jobId.encode()
458-
c(drmaa_wait, jobId, jid_out, sizeof(jid_out), byref(stat), timeout,
463+
if isinstance(jobId, str):
464+
jobId = jobId.encode()
465+
c(drmaa_wait, jobId, jid_out, sizeof(jid_out), byref(stat), timeout,
459466
rusage)
460467
res_usage = adapt_rusage(rusage)
461468
exited = c_int()
@@ -501,8 +508,8 @@ def jobStatus(jobId):
501508
jobs return a FAILED status.
502509
"""
503510
status = c_int()
504-
if not isinstance(jobId, bytes):
505-
jobId = jobId.encode()
511+
if isinstance(jobId, str):
512+
jobId = jobId.encode()
506513
c(drmaa_job_ps, jobId, byref(status))
507514
return status_to_string(status.value)
508515

drmaa/wrappers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030

3131
from drmaa.errors import error_check, error_buffer
3232

33+
34+
# Python 3 compatability help
35+
if sys.version_info < (3, 0):
36+
bytes = str
37+
str = unicode
38+
39+
3340
# the name of the OS environment variable optionally
3441
# containing the full path to the drmaa library
3542
_drmaa_lib_env_name = 'DRMAA_LIBRARY_PATH'
@@ -59,7 +66,7 @@
5966

6067

6168
def py_drmaa_init(contact=None):
62-
if not isinstance(contact, bytes) and contact is not None:
69+
if isinstance(contact, str):
6370
contact = contact.encode()
6471
return _lib.drmaa_init(contact, error_buffer, sizeof(error_buffer))
6572

0 commit comments

Comments
 (0)