Commit 9f603716 authored by Stefan Behnel's avatar Stefan Behnel

replace PyMapping_Keys() usage by safer code (PyMapping_Keys() may return a...

replace PyMapping_Keys() usage by safer code (PyMapping_Keys() may return a tuple, but we try to sort it as a list afterwards)
parent 24dfb784
...@@ -7059,13 +7059,24 @@ class SortedDictKeysNode(ExprNode): ...@@ -7059,13 +7059,24 @@ class SortedDictKeysNode(ExprNode):
def generate_result_code(self, code): def generate_result_code(self, code):
dict_result = self.arg.py_result() dict_result = self.arg.py_result()
if self.arg.type is Builtin.dict_type: if self.arg.type is Builtin.dict_type:
function = 'PyDict_Keys' code.putln('%s = PyDict_Keys(%s); %s' % (
self.result(), dict_result,
code.error_goto_if_null(self.result(), self.pos)))
code.put_gotref(self.py_result())
else: else:
function = 'PyMapping_Keys' # originally used PyMapping_Keys() here, but that may return a tuple
code.putln('%s = %s(%s); %s' % ( code.globalstate.use_utility_code(UtilityCode.load_cached(
self.result(), function, dict_result, 'PyObjectCallMethod', 'ObjectHandling.c'))
keys_cname = code.intern_identifier(StringEncoding.EncodedString("keys"))
code.putln('%s = __Pyx_PyObject_CallMethod0(%s, %s); %s' % (
self.result(), dict_result, keys_cname,
code.error_goto_if_null(self.result(), self.pos))) code.error_goto_if_null(self.result(), self.pos)))
code.put_gotref(self.py_result()) code.put_gotref(self.py_result())
code.putln("if (unlikely(!PyList_Check(%s))) {" % self.result())
code.put_decref_set(self.result(), "PySequence_List(%s)" % self.result())
code.putln(code.error_goto_if_null(self.result(), self.pos))
code.put_gotref(self.py_result())
code.putln("}")
code.put_error_if_neg( code.put_error_if_neg(
self.pos, 'PyList_Sort(%s)' % self.py_result()) self.pos, 'PyList_Sort(%s)' % self.py_result())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment