Commit 9ded34ef authored by Marius Wachtler's avatar Marius Wachtler

Add apply(), PyDict_Values(), PyDict_Items()

parent c5a3212a
......@@ -320,6 +320,31 @@ typedef ssize_t Py_ssize_t;
#endif
#endif
/* uintptr_t is the C9X name for an unsigned integral type such that a
* legitimate void* can be cast to uintptr_t and then back to void* again
* without loss of information. Similarly for intptr_t, wrt a signed
* integral type.
*/
#ifdef HAVE_UINTPTR_T
typedef uintptr_t Py_uintptr_t;
typedef intptr_t Py_intptr_t;
#elif SIZEOF_VOID_P <= SIZEOF_INT
typedef unsigned int Py_uintptr_t;
typedef int Py_intptr_t;
#elif SIZEOF_VOID_P <= SIZEOF_LONG
typedef unsigned long Py_uintptr_t;
typedef long Py_intptr_t;
#elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P <= SIZEOF_LONG_LONG)
typedef unsigned PY_LONG_LONG Py_uintptr_t;
typedef PY_LONG_LONG Py_intptr_t;
#else
# error "Python needs a typedef for Py_uintptr_t in pyport.h."
#endif /* HAVE_UINTPTR_T */
#if defined(_MSC_VER)
#define Py_MEMCPY(target, source, length) do { \
size_t i_, n_ = (length); \
......
......@@ -973,6 +973,18 @@ Box* builtinCmp(Box* lhs, Box* rhs) {
throwCAPIException();
}
Box* builtinApply(Box* func, Box* args, Box* keywords) {
if (!PyTuple_Check(args)) {
if (!PySequence_Check(args))
raiseExcHelper(TypeError, "apply() arg 2 expected sequence, found %s", getTypeName(args));
args = PySequence_Tuple(args);
checkAndThrowCAPIException();
}
if (keywords && !PyDict_Check(keywords))
raiseExcHelper(TypeError, "apply() arg 3 expected dictionary, found %s", getTypeName(keywords));
return runtimeCall(func, ArgPassSpec(0, 0, true, keywords != NULL), args, keywords, NULL, NULL, NULL);
}
void setupBuiltins() {
builtins_module = createModule("__builtin__", "__builtin__",
"Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is "
......@@ -1005,6 +1017,10 @@ void setupBuiltins() {
builtins_module->giveAttr("all", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)all, BOXED_BOOL, 1), "all"));
builtins_module->giveAttr("any", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)any, BOXED_BOOL, 1), "any"));
builtins_module->giveAttr(
"apply", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)builtinApply, UNKNOWN, 3, 1, false, false),
"apply", { NULL }));
repr_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)repr, UNKNOWN, 1), "repr");
builtins_module->giveAttr("repr", repr_obj);
......
......@@ -96,20 +96,32 @@ Box* dictKeys(BoxedDict* self) {
return rtn;
}
extern "C" PyObject* PyDict_Keys(PyObject* mp) noexcept {
static PyObject* dict_helper(PyObject* mp, std::function<Box*(BoxedDict*)> f) noexcept {
if (mp == NULL || !PyDict_Check(mp)) {
PyErr_BadInternalCall();
return NULL;
}
try {
return dictKeys(static_cast<BoxedDict*>(mp));
return f(static_cast<BoxedDict*>(mp));
} catch (ExcInfo e) {
setCAPIException(e);
return NULL;
}
}
extern "C" PyObject* PyDict_Keys(PyObject* mp) noexcept {
return dict_helper(mp, dictKeys);
}
extern "C" PyObject* PyDict_Values(PyObject* mp) noexcept {
return dict_helper(mp, dictValues);
}
extern "C" PyObject* PyDict_Items(PyObject* mp) noexcept {
return dict_helper(mp, dictItems);
}
Box* dictViewKeys(BoxedDict* self) {
if (!isSubclass(self->cls, dict_cls)) {
raiseExcHelper(TypeError, "descriptor 'viewkeys' requires a 'dict' object but received a '%s'",
......
......@@ -109,3 +109,7 @@ print round(0.5), round(-0.5)
print list(iter(xrange(100).__iter__().next, 20))
print bytearray(xrange(256))
l = [2, 1, 3]
print apply(sorted, [l])
print apply(sorted, [l], { "reverse" : True })
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