Example #1
0
static PyObject *t_tzinfo_richcmp(t_tzinfo *self, PyObject *other, int op)
{
    if (PyObject_TypeCheck(other, &TZInfoType))
    {
        PyObject *s1 = PyObject_Str((PyObject *) self->tz);
        PyObject *s2 = PyObject_Str((PyObject *) ((t_tzinfo *) other)->tz);
        PyObject *result = PyObject_RichCompare(s1, s2, op);

        Py_DECREF(s1);
        Py_DECREF(s2);

        return result;
    }

    if (PyObject_TypeCheck(other, &FloatingTZType))
    {
        PyObject *s1 = PyObject_Str((PyObject *) self->tz);
        PyObject *result = PyObject_RichCompare(s1, FLOATING_TZNAME, op);

        Py_DECREF(s1);

        return result;
    }

    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}
Example #2
0
/* note on Notify-tuple comparison.
 *
 * Such a comparison is required otherwise a check n == (pid, channel)
 * would fail. We also want to compare two notifies, and the obvious meaning is
 * "check that all the attributes are equal". Unfortunately this leads to an
 * inconsistent situation:
 *      Notify(pid, channel, payload1)
 *   == (pid, channel)
 *   == Notify(pid, channel, payload2)
 * even when payload1 != payload2. We can probably live with that, but hashing
 * makes things worse: hashability is a desirable property for a Notify, and
 * to maintain compatibility we should put a notify object in the same bucket
 * of a 2-item tuples... but we can't put all the payloads with the same
 * (pid, channel) in the same bucket: it would be an extremely poor hash.
 * So we maintain compatibility in the sense that notify without payload
 * behave as 2-item tuples in term of hashability, but if a payload is present
 * the (pid, channel) pair is no more equivalent as dict key to the Notify.
 */
static PyObject *
notify_richcompare(notifyObject *self, PyObject *other, int op)
{
    PyObject *rv = NULL;
    PyObject *tself = NULL;
    PyObject *tother = NULL;

    if (Py_TYPE(other) == &notifyType) {
        if (!(tself = notify_astuple(self, 1))) { goto exit; }
        if (!(tother = notify_astuple((notifyObject *)other, 1))) { goto exit; }
        rv = PyObject_RichCompare(tself, tother, op);
    }
    else if (PyTuple_Check(other)) {
        if (!(tself = notify_astuple(self, 0))) { goto exit; }
        rv = PyObject_RichCompare(tself, other, op);
    }
    else {
        Py_INCREF(Py_False);
        rv = Py_False;
    }

exit:
    Py_XDECREF(tself);
    Py_XDECREF(tother);
    return rv;
}
Example #3
0
static PyObject *t_floatingtz_richcmp(t_floatingtz *self,
                                      PyObject *other, int op)
{
    if (PyObject_TypeCheck(other, &FloatingTZType))
    {
        t_tzinfo *tzi1 = self->tzinfo;
        t_tzinfo *tzi2 = ((t_floatingtz *) other)->tzinfo;

        return PyObject_RichCompare((PyObject *) (tzi1 ? tzi1 : _default),
                                    (PyObject *) (tzi2 ? tzi2 : _default),
                                    op);
    }

    if (PyObject_TypeCheck(other, &TZInfoType))
    {
        PyObject *s2 = PyObject_Str((PyObject *) ((t_tzinfo *) other)->tz);
        PyObject *result = PyObject_RichCompare(FLOATING_TZNAME, s2, op);

        Py_DECREF(s2);

        return result;
    }

    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}
Example #4
0
static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid)
{
    if (opid != Py_EQ && opid != Py_NE) {
#if PY_MAJOR_VERSION < 3
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
#else
        Py_RETURN_NOTIMPLEMENTED;
#endif
    }
    if (PyType_IsSubtype(Py_TYPE(_other), &pysqlite_RowType)) {
        pysqlite_Row *other = (pysqlite_Row *)_other;
        PyObject *res = PyObject_RichCompare(self->description, other->description, opid);
        if ((opid == Py_EQ && res == Py_True)
            || (opid == Py_NE && res == Py_False)) {
            Py_DECREF(res);
            return PyObject_RichCompare(self->data, other->data, opid);
        }
    }
#if PY_MAJOR_VERSION < 3
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
#else
    Py_RETURN_NOTIMPLEMENTED;
#endif
}
Example #5
0
static PyObject * Match_richcompare(
    PyObject *self,
    PyObject *other,
    int op)
{
  PyObject* result = NULL;

  Match *a = (Match *) self;
  Match *b = (Match *) other;

  if(PyObject_TypeCheck(other, &Match_Type))
  {
    switch(op)
    {
    case Py_EQ:
      if (PyObject_RichCompareBool(a->rule, b->rule, Py_EQ) &&
          PyObject_RichCompareBool(a->ns, b->ns, Py_EQ))
        result = Py_True;
      else
        result = Py_False;

      Py_INCREF(result);
      break;

    case Py_NE:
      if (PyObject_RichCompareBool(a->rule, b->rule, Py_NE) ||
          PyObject_RichCompareBool(a->ns, b->ns, Py_NE))
          result = Py_True;
      else
          result = Py_False;

      Py_INCREF(result);
      break;

    case Py_LT:
    case Py_LE:
    case Py_GT:
    case Py_GE:
      if (PyObject_RichCompareBool(a->rule, b->rule, Py_EQ))
        result = PyObject_RichCompare(a->ns, b->ns, op);
      else
        result = PyObject_RichCompare(a->rule, b->rule, op);

      break;
    }
  }
  else
  {
    result = PyErr_Format(
        PyExc_TypeError,
        "'Match' objects must be compared with objects of the same class");
  }

  return result;
}
Example #6
0
/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
int
PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
{
	PyObject *res;
	int ok;

	/* Quick result when objects are the same.
	   Guarantees that identity implies equality. */
	if (v == w) {
		if (op == Py_EQ)
			return 1;
		else if (op == Py_NE)
			return 0;
	}

	res = PyObject_RichCompare(v, w, op);
	if (res == NULL)
		return -1;
	if (PyBool_Check(res))
		ok = (res == Py_True);
	else
		ok = PyObject_IsTrue(res);
	Py_DECREF(res);
	return ok;
}
Example #7
0
  /* For backwards compatability we have to check the action code
   * against an integer
   * The first argument is always an Action object
   */
  PyObject* Action_RichCompare(Action* obj1, PyObject* obj2, int method)
  {
    if (method == Py_EQ)
    {
      if (Action_Check(obj2))
      {
        // both are Action objects
        Action* a2 = (Action*)obj2;

        if (obj1->id == a2->id &&
            obj1->buttonCode == a2->buttonCode &&
            obj1->fAmount1 == a2->fAmount1 &&
            obj1->fAmount2 == a2->fAmount2 &&
            obj1->fRepeat == a2->fRepeat &&
            obj1->strAction == a2->strAction)
        {
          Py_RETURN_TRUE;
        }
        else
        {
          Py_RETURN_FALSE;
        }
      }
      else
      {
        // for backwards compatability in python scripts
        PyObject* o1 = PyLong_FromLong(obj1->id);
        return PyObject_RichCompare(o1, obj2, method);
      }
    }
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
  }
Example #8
0
static PyObject *
proxy_richcompare(PyObject *proxy, PyObject *v, int op)
{
    UNWRAP(proxy);
    UNWRAP(v);
    return PyObject_RichCompare(proxy, v, op);
}
static PyObject *Proxy_richcompare(ProxyObject *self,
        PyObject *other, int opcode)
{
    Proxy__ENSURE_WRAPPED_OR_RETURN_NULL(self);

    return PyObject_RichCompare(self->wrapped, other, opcode);
}
Example #10
0
static PyObject* Bar_richcompare(BarObject *v, PyObject *w, int op) {
  if (!Bar_Check(w)) {
    switch (op) {
    case Py_EQ: Py_RETURN_FALSE;
    case Py_NE: Py_RETURN_FALSE;
    default:
      Py_INCREF(Py_NotImplemented);
      return Py_NotImplemented;
    }
  }
  // Now we know it is a bar object.
  BarObject *x = (BarObject *)w;
  if (v->py_bc != x->py_bc) {
    // "Inherit" the judgement of our containing objects.
    return PyObject_RichCompare((PyObject*)v->py_bc, (PyObject*)x->py_bc, op);
  }
  // Now we know it is a bar object, and part of the same bar
  // collection no less.
  switch (op) {
  case Py_EQ: if (v->index==x->index) Py_RETURN_TRUE; else Py_RETURN_FALSE;
  case Py_NE: if (v->index!=x->index) Py_RETURN_TRUE; else Py_RETURN_FALSE;
  case Py_LE: if (v->index<=x->index) Py_RETURN_TRUE; else Py_RETURN_FALSE;
  case Py_GE: if (v->index>=x->index) Py_RETURN_TRUE; else Py_RETURN_FALSE;
  case Py_LT: if (v->index< x->index) Py_RETURN_TRUE; else Py_RETURN_FALSE;
  case Py_GT: if (v->index> x->index) Py_RETURN_TRUE; else Py_RETURN_FALSE;
  default:
    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
  }
}
Example #11
0
static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid)
{
    if (opid != Py_EQ && opid != Py_NE)
        Py_RETURN_NOTIMPLEMENTED;

    if (PyType_IsSubtype(Py_TYPE(_other), &pysqlite_RowType)) {
        pysqlite_Row *other = (pysqlite_Row *)_other;
        PyObject *res = PyObject_RichCompare(self->description, other->description, opid);
        if ((opid == Py_EQ && res == Py_True)
            || (opid == Py_NE && res == Py_False)) {
            Py_DECREF(res);
            return PyObject_RichCompare(self->data, other->data, opid);
        }
    }
    Py_RETURN_NOTIMPLEMENTED;
}
Example #12
0
static PyObject *
slice_richcompare(PyObject *v, PyObject *w, int op)
{
    PyObject *t1;
    PyObject *t2;
    PyObject *res;

    if (!PySlice_Check(v) || !PySlice_Check(w))
        Py_RETURN_NOTIMPLEMENTED;

    if (v == w) {
        /* XXX Do we really need this shortcut?
           There's a unit test for it, but is that fair? */
        switch (op) {
        case Py_EQ:
        case Py_LE:
        case Py_GE:
            res = Py_True;
            break;
        default:
            res = Py_False;
            break;
        }
        Py_INCREF(res);
        return res;
    }

    t1 = PyTuple_New(3);
    if (t1 == NULL)
        return NULL;
    t2 = PyTuple_New(3);
    if (t2 == NULL) {
        Py_DECREF(t1);
        return NULL;
    }

    PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start);
    PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop);
    PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step);
    PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start);
    PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop);
    PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step);

    res = PyObject_RichCompare(t1, t2, op);

    PyTuple_SET_ITEM(t1, 0, NULL);
    PyTuple_SET_ITEM(t1, 1, NULL);
    PyTuple_SET_ITEM(t1, 2, NULL);
    PyTuple_SET_ITEM(t2, 0, NULL);
    PyTuple_SET_ITEM(t2, 1, NULL);
    PyTuple_SET_ITEM(t2, 2, NULL);

    Py_DECREF(t1);
    Py_DECREF(t2);

    return res;
}
Example #13
0
static PyObject *
namespace_richcompare(PyObject *self, PyObject *other, int op)
{
    if (PyObject_TypeCheck(self, &_PyNamespace_Type) &&
        PyObject_TypeCheck(other, &_PyNamespace_Type))
        return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
                                   ((_PyNamespaceObject *)other)->ns_dict, op);
    Py_RETURN_NOTIMPLEMENTED;
}
Example #14
0
static PyObject *
structseq_richcompare(PyObject *obj, PyObject *o2, int op)
{
	PyObject *tup, *result;
	tup = make_tuple((PyStructSequence*) obj);
	result = PyObject_RichCompare(tup, o2, op);
	Py_DECREF(tup);
	return result;
}
Example #15
0
static PyObject *
complex_richcompare(PyObject *v, PyObject *w, int op)
{
    PyObject *res;
    Py_complex i;
    int equal;

    if (op != Py_EQ && op != Py_NE) {
        goto Unimplemented;
    }

    assert(PyComplex_Check(v));
    TO_COMPLEX(v, i);

    if (PyLong_Check(w)) {
        /* Check for 0.0 imaginary part first to avoid the rich
         * comparison when possible.
         */
        if (i.imag == 0.0) {
            PyObject *j, *sub_res;
            j = PyFloat_FromDouble(i.real);
            if (j == NULL)
                return NULL;

            sub_res = PyObject_RichCompare(j, w, op);
            Py_DECREF(j);
            return sub_res;
        }
        else {
            equal = 0;
        }
    }
    else if (PyFloat_Check(w)) {
        equal = (i.real == PyFloat_AsDouble(w) && i.imag == 0.0);
    }
    else if (PyComplex_Check(w)) {
        Py_complex j;

        TO_COMPLEX(w, j);
        equal = (i.real == j.real && i.imag == j.imag);
    }
    else {
        goto Unimplemented;
    }

    if (equal == (op == Py_EQ))
         res = Py_True;
    else
         res = Py_False;

    Py_INCREF(res);
    return res;

Unimplemented:
    Py_RETURN_NOTIMPLEMENTED;
}
static PyObject *
wrap_richcompare(PyObject* self, PyObject* other, int op)
{
    if (Proxy_Check(self)) {
        self = Proxy_GET_OBJECT(self);
    }
    else {
        other = Proxy_GET_OBJECT(other);
    }
    return PyObject_RichCompare(self, other, op);
}
Example #17
0
/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
int
PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
{
	PyObject *res = PyObject_RichCompare(v, w, op);
	int ok;

	if (res == NULL)
		return -1;
	ok = PyObject_IsTrue(res);
	Py_DECREF(res);
	return ok;
}
Example #18
0
static PyObject* Row_richcompare(PyObject* olhs, PyObject* orhs, int op)
{
    if (!Row_Check(olhs) || !Row_Check(orhs))
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    Row* lhs = (Row*)olhs;
    Row* rhs = (Row*)orhs;

    if (lhs->cValues != rhs->cValues)
    {
        // Different sizes, so use the same rules as the tuple class.
        bool result;
        switch (op)
        {
        case Py_EQ: result = (lhs->cValues == rhs->cValues); break;
        case Py_GE: result = (lhs->cValues >= rhs->cValues); break;
        case Py_GT: result = (lhs->cValues >  rhs->cValues); break;
        case Py_LE: result = (lhs->cValues <= rhs->cValues); break;
        case Py_LT: result = (lhs->cValues <  rhs->cValues); break;
        case Py_NE: result = (lhs->cValues != rhs->cValues); break;
        default:
            // Can't get here, but don't have a cross-compiler way to silence this.
            result = false;
        }
        PyObject* p = result ? Py_True : Py_False;
        Py_INCREF(p);
        return p;
    }

    for (Py_ssize_t i = 0, c = lhs->cValues; i < c; i++)
        if (!PyObject_RichCompareBool(lhs->apValues[i], rhs->apValues[i], Py_EQ))
            return PyObject_RichCompare(lhs->apValues[i], rhs->apValues[i], op);

    // All items are equal.
    switch (op)
    {
    case Py_EQ:
    case Py_GE:
    case Py_LE:
        Py_RETURN_TRUE;

    case Py_GT:
    case Py_LT:
    case Py_NE:
        break;
    }

    Py_RETURN_FALSE;
}
Example #19
0
static PyObject *cmp_gt(PyObject *module, PyObject *args)
{
  PyObject *a, *b;

  if (!PyArg_ParseTuple(args, "OO:gt", &a, &b)) return NULL;

  if (PyNumber_IsNaN(a) || PyNumber_IsNaN(b)) {
    Py_INCREF(Py_False);
    return Py_False;
  }

  return PyObject_RichCompare(a, b, Py_GT);
}
Example #20
0
static PyObject *cmp_ne(PyObject *module, PyObject *args)
{
  PyObject *a, *b;

  if (!PyArg_ParseTuple(args, "OO:ne", &a, &b)) return NULL;

  if (PyNumber_IsNaN(a) || PyNumber_IsNaN(b)) {
    /* Not-A-Number doesn't equal anything, even itself */
    Py_INCREF(Py_True);
    return Py_True;
  }

  return PyObject_RichCompare(a, b, Py_NE);
}
Example #21
0
static PyObject *
cell_richcompare(PyObject *a, PyObject *b, int op)
{
    int result;
    PyObject *v;

    /* neither argument should be NULL, unless something's gone wrong */
    assert(a != NULL && b != NULL);

    /* both arguments should be instances of PyCellObject */
    if (!PyCell_Check(a) || !PyCell_Check(b)) {
        v = Py_NotImplemented;
        Py_INCREF(v);
        return v;
    }

    /* compare cells by contents; empty cells come before anything else */
    a = ((PyCellObject *)a)->ob_ref;
    b = ((PyCellObject *)b)->ob_ref;
    if (a != NULL && b != NULL)
        return PyObject_RichCompare(a, b, op);

    result = (b == NULL) - (a == NULL);
    switch (op) {
    case Py_EQ:
        v = TEST_COND(result == 0);
        break;
    case Py_NE:
        v = TEST_COND(result != 0);
        break;
    case Py_LE:
        v = TEST_COND(result <= 0);
        break;
    case Py_GE:
        v = TEST_COND(result >= 0);
        break;
    case Py_LT:
        v = TEST_COND(result < 0);
        break;
    case Py_GT:
        v = TEST_COND(result > 0);
        break;
    default:
        PyErr_BadArgument();
        return NULL;
    }
    Py_INCREF(v);
    return v;
}
Example #22
0
static PyObject *
keyobject_richcompare(PyObject *ko, PyObject *other, int op)
{
    PyObject *res;
    PyObject *args;
    PyObject *x;
    PyObject *y;
    PyObject *compare;
    PyObject *answer;
    static PyObject *zero;

    if (zero == NULL) {
        zero = PyLong_FromLong(0);
        if (!zero)
            return NULL;
    }

    if (Py_TYPE(other) != &keyobject_type){
        PyErr_Format(PyExc_TypeError, "other argument must be K instance");
        return NULL;
    }
    compare = ((keyobject *) ko)->cmp;
    assert(compare != NULL);
    x = ((keyobject *) ko)->object;
    y = ((keyobject *) other)->object;
    if (!x || !y){
        PyErr_Format(PyExc_AttributeError, "object");
        return NULL;
    }

    /* Call the user's comparison function and translate the 3-way
     * result into true or false (or error).
     */
    args = PyTuple_New(2);
    if (args == NULL)
        return NULL;
    Py_INCREF(x);
    Py_INCREF(y);
    PyTuple_SET_ITEM(args, 0, x);
    PyTuple_SET_ITEM(args, 1, y);
    res = PyObject_Call(compare, args, NULL);
    Py_DECREF(args);
    if (res == NULL)
        return NULL;
    answer = PyObject_RichCompare(res, zero, op);
    Py_DECREF(res);
    return answer;
}
static PyObject *
weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
{
    if (op != Py_EQ || self->ob_type != other->ob_type) {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }
    if (PyWeakref_GET_OBJECT(self) == Py_None
        || PyWeakref_GET_OBJECT(other) == Py_None) {
        PyObject *res = self==other ? Py_True : Py_False;
        Py_INCREF(res);
        return res;
    }
    return PyObject_RichCompare(PyWeakref_GET_OBJECT(self),
                                PyWeakref_GET_OBJECT(other), op);
}
Example #24
0
static PyObject *
column_richcompare(columnObject *self, PyObject *other, int op)
{
    PyObject *rv = NULL;
    PyObject *tself = NULL;

    if (!(tself = PyObject_CallFunctionObjArgs(
            (PyObject *)&PyTuple_Type, (PyObject *)self, NULL))) {
        goto exit;
    }

    rv = PyObject_RichCompare(tself, other, op);

exit:
    Py_XDECREF(tself);
    return rv;
}
Example #25
0
static int
array_contains(PyArrayObject *self, PyObject *el)
{
    /* equivalent to (self == el).any() */

    PyObject *res;
    int ret;

    res = PyArray_EnsureAnyArray(PyObject_RichCompare((PyObject *)self,
                                                      el, Py_EQ));
    if (res == NULL) {
        return -1;
    }
    ret = array_any_nonzero((PyArrayObject *)res);
    Py_DECREF(res);
    return ret;
}
Example #26
0
static PyObject *
keyobject_richcompare(PyObject *ko, PyObject *other, int op)
{
    PyObject *res;
    PyObject *x;
    PyObject *y;
    PyObject *compare;
    PyObject *answer;
    static PyObject *zero;
    PyObject* stack[2];

    if (zero == NULL) {
        zero = PyLong_FromLong(0);
        if (!zero)
            return NULL;
    }

    if (Py_TYPE(other) != &keyobject_type){
        PyErr_Format(PyExc_TypeError, "other argument must be K instance");
        return NULL;
    }
    compare = ((keyobject *) ko)->cmp;
    assert(compare != NULL);
    x = ((keyobject *) ko)->object;
    y = ((keyobject *) other)->object;
    if (!x || !y){
        PyErr_Format(PyExc_AttributeError, "object");
        return NULL;
    }

    /* Call the user's comparison function and translate the 3-way
     * result into true or false (or error).
     */
    stack[0] = x;
    stack[1] = y;
    res = _PyObject_FastCall(compare, stack, 2);
    if (res == NULL) {
        return NULL;
    }

    answer = PyObject_RichCompare(res, zero, op);
    Py_DECREF(res);
    return answer;
}
Example #27
0
static PyObject *
CapObject_getattro(CapObject *self, PyObject *attribute) {

    PyObject *id, *keys, **item;
    Py_ssize_t size;

    id = PyLong_FromLong(self->id);
    if (id == NULL)
        return NULL;

    keys = PyDict_GetItem(_id2key_store, id);
    if (keys == NULL)
        return NULL;

    size = Py_SIZE(keys);
    item = ((PyTupleObject *)keys)->ob_item;

    PyObject *truth, *env_item;
    int i = 0;

    while (--size >= 0) {
        truth = PyObject_RichCompare(*item, attribute, Py_EQ);
        if (truth == NULL)
            return NULL;
        if (truth == Py_True) {
            env_item = ((PyListObject *)self->env)->ob_item[i];
            Py_INCREF(env_item);
            return env_item;
        }
        item++;
        i++;
    }

    truth = PyObject_GenericGetAttr((PyObject *)self, attribute);

    if (truth == NULL)
        return NULL;

    return truth;

    /*     PyErr_SetString(PyExc_AttributeError, "Cannot find."); */
    /*     return NULL; */

}
Example #28
0
static PyObject *
cell_richcompare(PyObject *a, PyObject *b, int op)
{
    /* neither argument should be NULL, unless something's gone wrong */
    assert(a != NULL && b != NULL);

    /* both arguments should be instances of PyCellObject */
    if (!PyCell_Check(a) || !PyCell_Check(b)) {
        Py_RETURN_NOTIMPLEMENTED;
    }

    /* compare cells by contents; empty cells come before anything else */
    a = ((PyCellObject *)a)->ob_ref;
    b = ((PyCellObject *)b)->ob_ref;
    if (a != NULL && b != NULL)
        return PyObject_RichCompare(a, b, op);

    Py_RETURN_RICHCOMPARE(b == NULL, a == NULL, op);
}
Example #29
0
static int
array_contains(PyArrayObject *self, PyObject *el)
{
    /* equivalent to (self == el).any() */

    int ret;
    PyObject *res, *any;

    res = PyArray_EnsureAnyArray(PyObject_RichCompare((PyObject *)self,
                                                      el, Py_EQ));
    if (res == NULL) {
        return -1;
    }
    any = PyArray_Any((PyArrayObject *)res, NPY_MAXDIMS, NULL);
    Py_DECREF(res);
    ret = PyObject_IsTrue(any);
    Py_DECREF(any);
    return ret;
}
Example #30
0
bool CPythonEngine::AddToPythonPath(LPCTSTR pPathName)
{
	PyObject *obPathList = PySys_GetObject("path");
	if (obPathList==NULL) {
		return false;
	}

	// Some pathnames have a leading '\\?\', which tells allows Unicode
	// win32 functions to avoid MAX_PATH limitations.  Notably,
	// GetModulePath for our extension DLL may - presumably as such a
	// path was specified by IIS when loading the DLL.
	// Current Python versions handle neither this, nor Unicode on
	// sys.path, so correct this here.
	size_t len = _tcslen(pPathName);
	if (len > 4 && _tcsncmp(pPathName, _T("\\\\?\\"), 4)==0) {
		pPathName += 4;
		len -= 4;
	}
#if (PY_VERSION_HEX < 0x03000000)
	PyObject *obNew = PyString_FromStringAndSize(pPathName, len);
#else
	PyObject *obNew = PyUnicode_FromWideChar(pPathName, len);
#endif
	if (obNew==NULL) {
		return false;
	}

	bool bFnd=false;
	for (int i=0; i<PyList_Size(obPathList); i++){
		PyObject * obItem = PyList_GetItem(obPathList, i);
		if(PyObject_RichCompare(obNew, obItem, Py_EQ) == Py_True){
			bFnd = true;
			break;
		}
	}

	if (!bFnd)
		PyList_Insert(obPathList, 0, obNew);

	Py_XDECREF(obNew);
	return true;
}