01: package org.python.core;
02:
03: public abstract class PyNewWrapper extends PyBuiltinMethod {
04:
05: public PyNewWrapper(Class c, String name, int minargs, int maxargs) {
06: super (PyType.fromClass(c), new DefaultInfo(name, minargs,
07: maxargs));
08: for_type = (PyType) getSelf();
09: }
10:
11: protected PyBuiltinFunction bind(PyObject self) {
12: throw Py.SystemError("__new__ wrappers are already bound");
13: }
14:
15: public PyObject __call__(PyObject[] args) {
16: return __call__(args, Py.NoKeywords);
17: }
18:
19: public PyObject __call__(PyObject[] args, String[] keywords) {
20: int nargs = args.length;
21: if (nargs < 1 || nargs == keywords.length) {
22: throw Py.TypeError(for_type.fastGetName()
23: + ".__new__(): not enough arguments");
24: }
25: PyObject arg0 = args[0];
26: if (!(arg0 instanceof PyType)) {
27: throw Py.TypeError(for_type.fastGetName()
28: + ".__new__(X): X is not a type object ("
29: + arg0.getType().fastGetName() + ")");
30: }
31: PyType subtype = (PyType) arg0;
32: if (!subtype.isSubType(for_type)) {
33: throw Py.TypeError(for_type.fastGetName() + ".__new__("
34: + subtype.fastGetName() + "): "
35: + subtype.fastGetName() + " is not a subtype of "
36: + for_type.fastGetName());
37: }
38: if (subtype.getStatic() != for_type) {
39: throw Py.TypeError(for_type.fastGetName() + ".__new__("
40: + subtype.fastGetName() + ") is not safe, use "
41: + subtype.fastGetName() + ".__new__()");
42: }
43: PyObject[] rest = new PyObject[nargs - 1];
44: System.arraycopy(args, 1, rest, 0, nargs - 1);
45: return new_impl(false, subtype, rest, keywords);
46: }
47:
48: // init true => invoke subtype.__init__(...) unless it is known to be
49: // unnecessary
50: public abstract PyObject new_impl(boolean init, PyType subtype,
51: PyObject[] args, String[] keywords);
52:
53: protected PyType for_type;
54: }
|