Skip to content

Commit 0a9b3c5

Browse files
bretambroseBret Ambrose
andauthored
Helper functions for non-optional integers should fail if the value is None (awslabs#642)
Co-authored-by: Bret Ambrose <bambrose@amazon.com>
1 parent 90a1db4 commit 0a9b3c5

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

source/module.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,57 +119,83 @@ PyObject *PyUnicode_FromAwsString(const struct aws_string *aws_str) {
119119
uint32_t PyObject_GetAttrAsUint32(PyObject *o, const char *class_name, const char *attr_name) {
120120
uint32_t result = UINT32_MAX;
121121

122-
PyObject *attr = PyObject_GetAttrString(o, attr_name);
122+
PyObject *attr = PyObject_GetAttrString(o, attr_name); /* new reference */
123123
if (!attr) {
124124
PyErr_Format(PyExc_AttributeError, "'%s.%s' attribute not found", class_name, attr_name);
125125
return result;
126126
}
127127

128+
if (attr == Py_None) {
129+
PyErr_Format(PyExc_AttributeError, "'%s.%s' required integral attribute is None", class_name, attr_name);
130+
goto done;
131+
}
132+
128133
PyObject_GetAsOptionalUint32(attr, class_name, attr_name, &result);
129134

135+
done:
136+
130137
Py_DECREF(attr);
131138
return result;
132139
}
133140

134141
uint16_t PyObject_GetAttrAsUint16(PyObject *o, const char *class_name, const char *attr_name) {
135142
uint16_t result = UINT16_MAX;
136143

137-
PyObject *attr = PyObject_GetAttrString(o, attr_name);
144+
PyObject *attr = PyObject_GetAttrString(o, attr_name); /* new reference */
138145
if (!attr) {
139146
PyErr_Format(PyExc_AttributeError, "'%s.%s' attribute not found", class_name, attr_name);
140147
return result;
141148
}
142149

150+
if (attr == Py_None) {
151+
PyErr_Format(PyExc_AttributeError, "'%s.%s' required integral attribute is None", class_name, attr_name);
152+
goto done;
153+
}
154+
143155
PyObject_GetAsOptionalUint16(attr, class_name, attr_name, &result);
144156

157+
done:
158+
145159
Py_DECREF(attr);
146160
return result;
147161
}
148162

149163
uint8_t PyObject_GetAttrAsUint8(PyObject *o, const char *class_name, const char *attr_name) {
150164
uint8_t result = UINT8_MAX;
151165

152-
PyObject *attr = PyObject_GetAttrString(o, attr_name);
166+
PyObject *attr = PyObject_GetAttrString(o, attr_name); /* new reference */
153167
if (!attr) {
154168
PyErr_Format(PyExc_AttributeError, "'%s.%s' attribute not found", class_name, attr_name);
155169
return result;
156170
}
157171

172+
if (attr == Py_None) {
173+
PyErr_Format(PyExc_AttributeError, "'%s.%s' required integral attribute is None", class_name, attr_name);
174+
goto done;
175+
}
176+
158177
PyObject_GetAsOptionalUint8(attr, class_name, attr_name, &result);
159178

179+
done:
180+
160181
Py_DECREF(attr);
161182
return result;
162183
}
163184

164185
bool PyObject_GetAttrAsBool(PyObject *o, const char *class_name, const char *attr_name) {
165186
bool result = false;
166187

167-
PyObject *attr = PyObject_GetAttrString(o, attr_name);
188+
PyObject *attr = PyObject_GetAttrString(o, attr_name); /* new reference */
168189
if (!attr) {
169190
PyErr_Format(PyExc_AttributeError, "'%s.%s' attribute not found", class_name, attr_name);
170191
return result;
171192
}
172193

194+
if (attr == Py_None) {
195+
PyErr_Format(PyExc_AttributeError, "'%s.%s' required boolean attribute is None", class_name, attr_name);
196+
goto done;
197+
}
198+
173199
int val = PyObject_IsTrue(attr);
174200
if (val == -1) {
175201
PyErr_Format(PyExc_TypeError, "Cannot convert %s.%s to bool", class_name, attr_name);
@@ -185,14 +211,22 @@ bool PyObject_GetAttrAsBool(PyObject *o, const char *class_name, const char *att
185211
int PyObject_GetAttrAsIntEnum(PyObject *o, const char *class_name, const char *attr_name) {
186212
int result = -1;
187213

188-
PyObject *attr = PyObject_GetAttrString(o, attr_name);
214+
PyObject *attr = PyObject_GetAttrString(o, attr_name); /* new reference */
189215
if (!attr) {
190216
PyErr_Format(PyExc_AttributeError, "'%s.%s' attribute not found", class_name, attr_name);
191217
return result;
192218
}
193219

220+
if (attr == Py_None) {
221+
PyErr_Format(
222+
PyExc_AttributeError, "'%s.%s' required integral enumeration attribute is None", class_name, attr_name);
223+
goto done;
224+
}
225+
194226
PyObject_GetAsOptionalIntEnum(attr, class_name, attr_name, &result);
195227

228+
done:
229+
196230
Py_DECREF(attr);
197231
return result;
198232
}

0 commit comments

Comments
 (0)