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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
"""generates code for binops in PyObject and for all "simple" ops in PyInstance"""
binops = \
[('add', '+'), ('sub', '-'), ('mul', '*'), ('div', '/'),
('floordiv', '//'), ('truediv', '/'),
('mod', '%'), ('divmod', 'divmod'), ('pow', '**'),
('lshift', '<<'), ('rshift', '>>'), ('and', '&'), ('or', '|'), ('xor', '^')]
#
# this bit is superseded by src/templates/make_binops.py
#
## template = """\
## /**
## * Equivalent to the standard Python __%(name)s__ method
## * @param other the object to perform this binary operation with
## * (the right-hand operand).
## * @return the result of the %(name)s, or null if this operation
## * is not defined
## **/
## public PyObject __%(name)s__(PyObject other) { %(function)s }
## /**
## * Equivalent to the standard Python __r%(name)s__ method
## * @param other the object to perform this binary operation with
## * (the left-hand operand).
## * @return the result of the %(name)s, or null if this operation
## * is not defined.
## **/
## public PyObject __r%(name)s__(PyObject other) { %(rfunction)s }
## /**
## * Equivalent to the standard Python __i%(name)s__ method
## * @param other the object to perform this binary operation with
## * (the right-hand operand).
## * @return the result of the %(name)s, or null if this operation
## * is not defined
## **/
## public PyObject __i%(name)s__(PyObject other) { return _%(name)s(other); }
## /**
## * Implements the Python expression <code>this %(op)s other</code>
## * @param other the object to perform this binary operation with.
## * @return the result of the %(name)s.
## * @exception PyTypeError if this operation can't be performed
## * with these operands.
## **/
## public final PyObject _%(name)s(PyObject o2) {%(divhook)s
## PyObject x = __%(name)s__(o2);
## if (x != null)
## return x;
## x = o2.__r%(name)s__(this);
## if (x != null)
## return x;
## throw Py.TypeError(
## "__%(name)s__ nor __r%(name)s__ defined for these operands");
## }
## """
## fp = open('binops.txt', 'w')
## fp.write(' // Generated by make_binops.py (Begin)\n\n')
## for name, op in binops:
## rfunction = function = 'return null;'
## divhook = ""
## if name == 'pow':
## function = 'return __pow__(other, null);'
## if name == 'div':
## divhook = '''
## if (Options.Qnew)
## return _truediv(o2);'''
## fp.write(template % {
## 'name':name,
## 'op':op,
## 'function':function,
## 'rfunction':rfunction,
## 'divhook':divhook
## })
## fp.write(' // Generated by make_binops.py (End)\n\n')
## fp.close()
fp = open('binopsi.txt', 'w')
fp.write(' // Generated by make_binops.py\n\n')
comment = """\
/**
* Implements the __%(name)s__ method by looking it up
* in the instance's dictionary and calling it if it is found.
**/
"""
template1 = """\
@Override
public %(ret)s __%(name)s__() {
return instance___%(name)s__();
}
""" + comment + """\
@ExposedMethod
final %(ret)s instance___%(name)s__() {
PyObject ret = invoke("__%(name)s__");
if (%(checks)s)
return %(cast)sret;
throw Py.TypeError("__%(name)s__() should return a %(retname)s");
}
"""
template2 = """\
@Override
public PyObject __%(name)s__() {
return instance___%(name)s__();
}
""" + comment + """\
@ExposedMethod
public PyObject instance___%(name)s__() {
return invoke("__%(name)s__");
}
"""
string = 'PyString', 'string'
ops = [('hex', string), ('oct', string),
('int', ('PyObject', 'int'), ('PyLong', 'PyInteger')),
('float', ('PyFloat', 'float')),
('long', ('PyObject', 'long'), ('PyLong', 'PyInteger')),
('complex', ('PyComplex', 'complex')),
('pos', None), ('neg', None), ('abs', None), ('invert', None)]
fp.write(' // Unary ops\n\n')
for item in ops:
checks = None
if len(item) == 2:
name, ret = item
else:
name, ret, checks = item
if ret is None:
fp.write(template2 % {'name':name})
else:
ret, retname = ret
if checks:
checks = ' || '.join(['ret instanceof %s' % check for check in checks])
else:
checks = 'ret instanceof %s' % ret
if ret == 'PyObject':
cast = ''
else:
cast = '(%s)' % ret
fp.write(template1 % {'name':name, 'ret':ret, 'retname':retname,
'checks':checks, 'cast':cast})
template = """\
@Override
public PyObject __%(name)s__(PyObject o) {
return instance___%(name)s__(o);
}
""" + comment + """\
@ExposedMethod(type = MethodType.BINARY)
public PyObject instance___%(name)s__(PyObject o) {
Object ctmp = __coerce_ex__(o);
if (ctmp == null || ctmp == Py.None)
return invoke_ex("__%(name)s__", o);
else {
PyObject o1 = ((PyObject[])ctmp)[0];
PyObject o2 = ((PyObject[])ctmp)[1];
if (this == o1) {
// Prevent recusion if __coerce__ return self
return invoke_ex("__%(name)s__", o2);
}
else {
ThreadState ts = Py.getThreadState();
if (ts.recursion_depth++ > ts.systemState.getrecursionlimit())
throw Py.RuntimeError("maximum recursion depth exceeded");
try {
return %(function)s;
} finally {
--ts.recursion_depth;
}
}
}
}
"""
template2 = """\
@Override
public PyObject __%(name)s__(PyObject o) {
return instance___%(name)s__(o);
}
""" + comment + """\
@ExposedMethod(type = MethodType.BINARY)
public PyObject instance___%(name)s__(PyObject o) {
PyObject ret = invoke_ex("__%(name)s__", o);
if (ret != null)
return ret;
return super.__%(name)s__(o);
}
"""
fp.write(' // Binary ops\n\n')
for name, op in binops:
fp.write(template % {'name':name, 'function':'o1._%s(o2)' % name })
fp.write(template % {'name':'r'+name, 'function':'o2._%s(o1)' % name })
if name != 'divmod':
fp.write(template2 % {'name':'i'+name
})
fp.close()
|