File: generic_ops.c

package info (click to toggle)
mypy 0.812-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 18,596 kB
  • sloc: python: 74,869; cpp: 11,212; ansic: 3,935; makefile: 238; sh: 13
file content (59 lines) | stat: -rw-r--r-- 1,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Generic primitive operations
//
// These are registered in mypyc.primitives.generic_ops.

#include <Python.h>
#include "CPy.h"

CPyTagged CPyObject_Hash(PyObject *o) {
    Py_hash_t h = PyObject_Hash(o);
    if (h == -1) {
        return CPY_INT_TAG;
    } else {
        // This is tragically annoying. The range of hash values in
        // 64-bit python covers 64-bits, and our short integers only
        // cover 63. This means that half the time we are boxing the
        // result for basically no good reason. To add insult to
        // injury it is probably about to be immediately unboxed by a
        // tp_hash wrapper.
        return CPyTagged_FromSsize_t(h);
    }
}

PyObject *CPyObject_GetAttr3(PyObject *v, PyObject *name, PyObject *defl)
{
    PyObject *result = PyObject_GetAttr(v, name);
    if (!result && PyErr_ExceptionMatches(PyExc_AttributeError)) {
        PyErr_Clear();
        Py_INCREF(defl);
        result = defl;
    }
    return result;
}

PyObject *CPyIter_Next(PyObject *iter)
{
    return (*iter->ob_type->tp_iternext)(iter);
}

PyObject *CPyNumber_Power(PyObject *base, PyObject *index)
{
    return PyNumber_Power(base, index, Py_None);
}

PyObject *CPyObject_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) {
    PyObject *start_obj = CPyTagged_AsObject(start);
    PyObject *end_obj = CPyTagged_AsObject(end);
    if (unlikely(start_obj == NULL || end_obj == NULL)) {
        return NULL;
    }
    PyObject *slice = PySlice_New(start_obj, end_obj, NULL);
    Py_DECREF(start_obj);
    Py_DECREF(end_obj);
    if (unlikely(slice == NULL)) {
        return NULL;
    }
    PyObject *result = PyObject_GetItem(obj, slice);
    Py_DECREF(slice);
    return result;
}