summaryrefslogtreecommitdiff
path: root/cad/src/samevals.pyx
blob: f96d5f0139a199a4aab4006ed0e026aba3729dd2 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright 2008-2009 Nanorex, Inc.  See LICENSE file for details.
"""
@author: EricM
@version: $Id$
@copyright: 2008-2009 Nanorex, Inc.  See LICENSE file for details.
"""

import types
import Numeric

cdef extern from "Python.h":
    int PyType_Check(obj)
    int PyCallable_Check(obj)
    int PyList_Check(obj)

cdef extern from "samevalshelp.c":
    int c_setArrayType(object atype)
    int c_setInstanceCopier(object copier)
    int c_setGeneralCopier(object copier)
    int c_setArrayCopier(object copier)
    int c_setInstanceLikeClasses(object classList)
    object c_same_vals(object o1, object o2)
    object c_copy_val(object obj)

class _old_style_class:
    pass

_old_style_instance = _old_style_class()

def same_vals(o1, o2):
    res = c_same_vals(o1, o2)

    # TODO: we need to fix a bug in c_same_vals exception handling,
    # in which it fails to properly handle an exception in its call to
    # PyObject_RichCompareBool (for example, a ValueError when it's used
    # on objects of type numpy.ndarray). Until that's fixed in the C code
    # (not just for ndarray but for any potential exception from that callback),
    # we work around that bug in c_same_vals here, by the following KLUGE --
    # this getattr will raise that unhandled exception from its own location
    # in this file, but will cause no harm (except a slowdown, of unknown
    # importance) if there is no unhandled exception.
    # TODO: also extend c_same_vals to correctly compare numpy.ndarray,
    # like the Python version (in utilities/Comparison.py) now does.
    # [bruce 081202]

    # disabled to get ubuntu 7.04 running again [bryan 2012-04-30]
    #getattr3(_old_style_instance, 'missingattr', None)
        # for doc of getattr3: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/Manual/basics.html

    return res

def copy_val(obj):
    return c_copy_val(obj)

def setArrayType(atype):
    if (PyType_Check(atype)):
        c_setArrayType(atype)
    else:
        raise TypeError, "argument must be a type"

def setInstanceCopier(copier):
    if (PyCallable_Check(copier)):
        c_setInstanceCopier(copier)
    else:
        raise TypeError, "argument must be callable"

def setGeneralCopier(copier):
    if (PyCallable_Check(copier)):
        c_setGeneralCopier(copier)
    else:
        raise TypeError, "argument must be callable"

def setArrayCopier(copier):
    if (PyCallable_Check(copier)):
        c_setArrayCopier(copier)
    else:
        raise TypeError, "argument must be callable"

def setInstanceLikeClasses(classList): # deprecated, bruce 090206
    if (PyList_Check(classList)):
        c_setInstanceLikeClasses(classList)
    else:
        raise TypeError, "argument must be a list"