Skip to content

Commit 6b64339

Browse files
committed
Better checking of "None" in string fields
1 parent ba3061d commit 6b64339

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

code/ibmmq/mqcallback.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def _internal_cb(hc, md, gmo, buf, cbc):
121121
else:
122122
gmo_up = GMO()
123123

124-
# Call the real user function with the unpacked forms of the structures
124+
# Call the real user function with the unpacked forms of the structures.
125+
# If the callback_function is actually a method within a class, then the "self"
126+
# parameter is automatically added to the parameters.
125127
cb.callback_function(queue_manager=qmgr, queue=queue, md=md_up, gmo=gmo_up, msg=buf, cbc=cbc_up)
126128

127129
def real_cb(obj, kwargs):

code/ibmmq/mqopts.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,32 @@ def pack(self) -> bytes:
130130
# are in the list elements.
131131
for i in self.__list:
132132
v = getattr(self, i[0])
133+
133134
# Flatten attribs that are arrays
134135
if isinstance(v, list):
135136
for x in v:
136137
args.append(ensure_strings_are_bytes(x))
137138
else:
138-
# print(f"{i[0]} is of value {v}: type {type(v)}")
139+
# print(f"Field: {i[0]} format '{i[2]}' is of value {v}: type {type(v)}")
139140
if i[0] in _binary_fields:
140141
if not isinstance(v, bytes):
141142
err = f'{i[0]} must be a byte array'
142143
raise TypeError(err)
144+
elif v is None:
145+
# If a string field is set to None, the pack() function
146+
# fails with an unobvious error. Let's try to diagnose
147+
# it a bit earlier. And if there's a Pointer that has escaped being
148+
# set to NULL, then fix it up.
149+
if i[2].endswith('s'):
150+
err = f'Class:{type(self).__name__} Field:{i[0]} must not be None'
151+
raise TypeError(err)
152+
if i[2] == 'P':
153+
v = 0
154+
143155
args.append(ensure_strings_are_bytes(v))
144156

145157
# print(f"Args to be packed are {args}")
158+
146159
return struct.pack(*args)
147160

148161
def unpack(self, buff: bytes):

code/ibmmq/mqqmgr.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,8 @@ def connect_with_options(self, name: Union[str, bytes], *args: Any, **kwargs: Di
231231
csp._set_ptr_field('Token', token)
232232

233233
# We need to fix these up even if they are not going to be used
234-
if user:
235-
csp._set_ptr_field('CSPUserId', None)
236-
if password:
237-
csp._set_ptr_field('CSPPassword', None)
234+
csp._set_ptr_field('CSPUserId', None)
235+
csp._set_ptr_field('CSPPassword', None)
238236

239237
csp.AuthenticationType = CMQC.MQCSP_AUTH_ID_TOKEN
240238
csp.Version = max(csp.Version, CMQC.MQCSP_VERSION_3)
@@ -249,6 +247,10 @@ def connect_with_options(self, name: Union[str, bytes], *args: Any, **kwargs: Di
249247
csp._set_ptr_field('CSPUserId', user)
250248
csp._set_ptr_field('CSPPassword', password)
251249
csp.AuthenticationType = CMQC.MQCSP_AUTH_USER_ID_AND_PWD
250+
else:
251+
# Force these to be None
252+
csp._set_ptr_field('CSPUserId', None)
253+
csp._set_ptr_field('CSPPassword', None)
252254

253255
if initial_key:
254256
csp._set_ptr_field('InitialKey', initial_key)

0 commit comments

Comments
 (0)