KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyObject


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 import java.text.MessageFormat JavaDoc;
5
6 /**
7  * All objects known to the Jython runtime system are represented
8  * by an instance of the class <code>PyObject</code> or one of
9  * its subclasses.
10  *
11  **/

12
13 public class PyObject implements java.io.Serializable JavaDoc {
14     /* type info */
15
16     public static final String JavaDoc exposed_name = "object";
17
18     public static void typeSetup(PyObject dict, PyType.Newstyle marker) {
19         dict.__setitem__("__class__", new PyGetSetDescr("__class__",
20                 PyObject.class, "getType", null));
21         dict.__setitem__("__doc__", new PyGetSetDescr("__doc__",
22                 PyObject.class, "getDoc", null));
23         class exposed___str__ extends PyBuiltinFunctionNarrow {
24
25             private PyObject self;
26
27             public PyObject getSelf() {
28                 return self;
29             }
30
31             exposed___str__(PyObject self, PyBuiltinFunction.Info info) {
32                 super(info);
33                 this.self = self;
34             }
35
36             public PyBuiltinFunction makeBound(PyObject self) {
37                 return new exposed___str__((PyObject) self, info);
38             }
39
40             public PyObject __call__() {
41                 return self.__repr__();
42             }
43
44             public PyObject inst_call(PyObject gself) {
45                 PyObject self = (PyObject) gself;
46                 return self.__repr__();
47             }
48
49         }
50         dict.__setitem__("__str__", new PyMethodDescr("__str__",
51                 PyObject.class, 0, 0, new exposed___str__(null, null)));
52         class exposed___getattribute__ extends PyBuiltinFunctionNarrow {
53
54             private PyObject self;
55
56             public PyObject getSelf() {
57                 return self;
58             }
59
60             exposed___getattribute__(PyObject self, PyBuiltinFunction.Info info) {
61                 super(info);
62                 this.self = self;
63             }
64
65             public PyBuiltinFunction makeBound(PyObject self) {
66                 return new exposed___getattribute__((PyObject) self, info);
67             }
68
69             public PyObject __call__(PyObject arg0) {
70                 try {
71                     String JavaDoc name = (arg0.asName(0));
72                     PyObject ret = self.object___findattr__(name);
73                     if (ret == null)
74                         self.noAttributeError(name);
75                     return ret;
76                 } catch (PyObject.ConversionException e) {
77                     String JavaDoc msg;
78                     switch (e.index) {
79                     case 0:
80                         msg = "attribute name must be string";
81                         break;
82                     default:
83                         msg = "xxx";
84                     }
85                     throw Py.TypeError(msg);
86                 }
87             }
88
89             public PyObject inst_call(PyObject gself, PyObject arg0) {
90                 PyObject self = (PyObject) gself;
91                 try {
92                     String JavaDoc name = (arg0.asName(0));
93                     PyObject ret = self.object___findattr__(name);
94                     if (ret == null)
95                         self.noAttributeError(name);
96                     return ret;
97                 } catch (PyObject.ConversionException e) {
98                     String JavaDoc msg;
99                     switch (e.index) {
100                     case 0:
101                         msg = "attribute name must be string";
102                         break;
103                     default:
104                         msg = "xxx";
105                     }
106                     throw Py.TypeError(msg);
107                 }
108             }
109
110         }
111         dict.__setitem__("__getattribute__", new PyMethodDescr(
112                 "__getattribute__", PyObject.class, 1, 1,
113                 new exposed___getattribute__(null, null)));
114         class exposed___setattr__ extends PyBuiltinFunctionNarrow {
115
116             private PyObject self;
117
118             public PyObject getSelf() {
119                 return self;
120             }
121
122             exposed___setattr__(PyObject self, PyBuiltinFunction.Info info) {
123                 super(info);
124                 this.self = self;
125             }
126
127             public PyBuiltinFunction makeBound(PyObject self) {
128                 return new exposed___setattr__((PyObject) self, info);
129             }
130
131             public PyObject __call__(PyObject arg0, PyObject arg1) {
132                 try {
133                     self.object___setattr__(arg0.asName(0), arg1);
134                     return Py.None;
135                 } catch (PyObject.ConversionException e) {
136                     String JavaDoc msg;
137                     switch (e.index) {
138                     case 0:
139                         msg = "attribute name must be string";
140                         break;
141                     default:
142                         msg = "xxx";
143                     }
144                     throw Py.TypeError(msg);
145                 }
146             }
147
148             public PyObject inst_call(PyObject gself, PyObject arg0,
149                     PyObject arg1) {
150                 PyObject self = (PyObject) gself;
151                 try {
152                     self.object___setattr__(arg0.asName(0), arg1);
153                     return Py.None;
154                 } catch (PyObject.ConversionException e) {
155                     String JavaDoc msg;
156                     switch (e.index) {
157                     case 0:
158                         msg = "attribute name must be string";
159                         break;
160                     default:
161                         msg = "xxx";
162                     }
163                     throw Py.TypeError(msg);
164                 }
165             }
166
167         }
168         dict.__setitem__("__setattr__", new PyMethodDescr("__setattr__",
169                 PyObject.class, 2, 2, new exposed___setattr__(null, null)));
170         class exposed___delattr__ extends PyBuiltinFunctionNarrow {
171
172             private PyObject self;
173
174             public PyObject getSelf() {
175                 return self;
176             }
177
178             exposed___delattr__(PyObject self, PyBuiltinFunction.Info info) {
179                 super(info);
180                 this.self = self;
181             }
182
183             public PyBuiltinFunction makeBound(PyObject self) {
184                 return new exposed___delattr__((PyObject) self, info);
185             }
186
187             public PyObject __call__(PyObject arg0) {
188                 try {
189                     self.object___delattr__(arg0.asName(0));
190                     return Py.None;
191                 } catch (PyObject.ConversionException e) {
192                     String JavaDoc msg;
193                     switch (e.index) {
194                     case 0:
195                         msg = "attribute name must be string";
196                         break;
197                     default:
198                         msg = "xxx";
199                     }
200                     throw Py.TypeError(msg);
201                 }
202             }
203
204             public PyObject inst_call(PyObject gself, PyObject arg0) {
205                 PyObject self = (PyObject) gself;
206                 try {
207                     self.object___delattr__(arg0.asName(0));
208                     return Py.None;
209                 } catch (PyObject.ConversionException e) {
210                     String JavaDoc msg;
211                     switch (e.index) {
212                     case 0:
213                         msg = "attribute name must be string";
214                         break;
215                     default:
216                         msg = "xxx";
217                     }
218                     throw Py.TypeError(msg);
219                 }
220             }
221
222         }
223         dict.__setitem__("__delattr__", new PyMethodDescr("__delattr__",
224                 PyObject.class, 1, 1, new exposed___delattr__(null, null)));
225         class exposed___hash__ extends PyBuiltinFunctionNarrow {
226
227             private PyObject self;
228
229             public PyObject getSelf() {
230                 return self;
231             }
232
233             exposed___hash__(PyObject self, PyBuiltinFunction.Info info) {
234                 super(info);
235                 this.self = self;
236             }
237
238             public PyBuiltinFunction makeBound(PyObject self) {
239                 return new exposed___hash__((PyObject) self, info);
240             }
241
242             public PyObject __call__() {
243                 return new PyInteger(self.object_hashCode());
244             }
245
246             public PyObject inst_call(PyObject gself) {
247                 PyObject self = (PyObject) gself;
248                 return new PyInteger(self.object_hashCode());
249             }
250
251         }
252         dict.__setitem__("__hash__", new PyMethodDescr("__hash__",
253                 PyObject.class, 0, 0, new exposed___hash__(null, null)));
254         class exposed___repr__ extends PyBuiltinFunctionNarrow {
255
256             private PyObject self;
257
258             public PyObject getSelf() {
259                 return self;
260             }
261
262             exposed___repr__(PyObject self, PyBuiltinFunction.Info info) {
263                 super(info);
264                 this.self = self;
265             }
266
267             public PyBuiltinFunction makeBound(PyObject self) {
268                 return new exposed___repr__((PyObject) self, info);
269             }
270
271             public PyObject __call__() {
272                 return new PyString(self.object_toString());
273             }
274
275             public PyObject inst_call(PyObject gself) {
276                 PyObject self = (PyObject) gself;
277                 return new PyString(self.object_toString());
278             }
279
280         }
281         dict.__setitem__("__repr__", new PyMethodDescr("__repr__",
282                 PyObject.class, 0, 0, new exposed___repr__(null, null)));
283         class exposed___init__ extends PyBuiltinFunctionWide {
284
285             private PyObject self;
286
287             public PyObject getSelf() {
288                 return self;
289             }
290
291             exposed___init__(PyObject self, PyBuiltinFunction.Info info) {
292                 super(info);
293                 this.self = self;
294             }
295
296             public PyBuiltinFunction makeBound(PyObject self) {
297                 return new exposed___init__((PyObject) self, info);
298             }
299
300             public PyObject inst_call(PyObject self, PyObject[] args) {
301                 return inst_call(self, args, Py.NoKeywords);
302             }
303
304             public PyObject __call__(PyObject[] args) {
305                 return __call__(args, Py.NoKeywords);
306             }
307
308             public PyObject __call__(PyObject[] args, String JavaDoc[] keywords) {
309                 self.object_init(args, keywords);
310                 return Py.None;
311             }
312
313             public PyObject inst_call(PyObject gself, PyObject[] args,
314                     String JavaDoc[] keywords) {
315                 PyObject self = (PyObject) gself;
316                 self.object_init(args, keywords);
317                 return Py.None;
318             }
319
320         }
321         dict.__setitem__("__init__", new PyMethodDescr("__init__",
322                 PyObject.class, -1, -1, new exposed___init__(null, null)));
323         dict.__setitem__("__new__", new PyNewWrapper(PyObject.class, "__new__",
324                 -1, -1) {
325
326             public PyObject new_impl(boolean init, PyType subtype,
327                     PyObject[] args, String JavaDoc[] keywords) {
328                 PyObject newobj;
329                 if (for_type == subtype) {
330                     newobj = new PyObject();
331                     if (init)
332                         newobj.object_init(args, keywords);
333                 } else {
334                     newobj = new PyObjectDerived(subtype);
335                 }
336                 return newobj;
337             }
338
339         });
340     }
341
342     final void object_init(PyObject[] args, String JavaDoc[] keywords) {
343         // xxx
344
}
345
346     // xxx this is likely not the final name/approach,
347
// getType may become not necessary
348
private transient PyType objtype;
349
350     public PyType getType() {
351         return objtype;
352     }
353
354     // xxx
355
public PyObject fastGetClass() {
356         return objtype;
357     }
358
359     public PyObject getDoc() {
360         PyObject doc = fastGetDict().__finditem__("__doc__");
361         if(doc == null) {
362             return Py.None;
363         }
364         return doc;
365     }
366
367     /* must instantiate __class__ when de-serializing */
368     private void readObject(java.io.ObjectInputStream JavaDoc in)
369         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
370         in.defaultReadObject();
371         objtype = PyType.fromClass(getClass());
372     }
373
374     public PyObject(PyType objtype) {
375         this.objtype = objtype;
376     }
377
378     // A package private constructor used by PyJavaClass
379
// xxx will need variants for PyType of PyType and still PyJavaClass of PyJavaClass
380
PyObject(boolean dummy) {
381         objtype = (PyType) this;
382     }
383
384     /**
385      * The standard constructor for a <code>PyObject</code>. It will set
386      * the <code>__class__</code> field to correspond to the specific
387      * subclass of <code>PyObject</code> being instantiated.
388      **/

389     public PyObject() {
390         // xxx for now no such caching
391
// PyClass c = getPyClass();
392
// if (c == null)
393
// c = PyJavaClass.lookup(getClass());
394
objtype = PyType.fromClass(getClass());
395     }
396
397     /* xxx will be replaced.
398      * This method is provided to efficiently initialize the __class__
399      * attribute. If the following boilerplate is added to a subclass of
400      * PyObject, the instantiation time for the object will be greatly
401      * reduced.
402      *
403      * <blockquote><pre>
404      * // __class__ boilerplate -- see PyObject for details
405      * public static PyClass __class__;
406      * protected PyClass getPyClass() { return __class__; }
407      * </pre></blockquote>
408      *
409      * With PyIntegers this leads to a 50% faster instantiation time.
410      * This replaces the PyObject(PyClass c) constructor which is now
411      * deprecated.
412      *
413     protected PyClass getPyClass() {
414         return null;
415     } */

416
417     /**
418      * Dispatch __init__ behavior
419      */

420     public void dispatch__init__(PyType type,PyObject[] args,String JavaDoc[] keywords) {
421     }
422
423
424     /**
425      * Equivalent to the standard Python __repr__ method. This method
426      * should not typically need to be overrriden. The easiest way to
427      * configure the string representation of a <code>PyObject</code> is to
428      * override the standard Java <code>toString</code> method.
429      **/

430     public PyString __repr__() {
431         return new PyString(toString());
432     }
433
434     public String JavaDoc toString() {
435         return object_toString();
436     }
437
438     final String JavaDoc object_toString() {
439         if (getType() == null) {
440             return "unknown object";
441         }
442
443         String JavaDoc name = getType().getFullName();
444         if (name == null)
445             return "unknown object";
446
447         return "<"+name+" object "+Py.idstr(this)+">";
448     }
449
450     public String JavaDoc safeRepr() throws PyIgnoreMethodTag {
451         if (getType() == null) {
452             return "unknown object";
453         }
454
455         String JavaDoc name = getType().getFullName();
456         if (name == null)
457             return "unknown object";
458
459         return "'" + name + "' object";
460     }
461
462     /**
463      * Equivalent to the standard Python __str__ method. This method
464      * should not typically need to be overridden. The easiest way to
465      * configure the string representation of a <code>PyObject</code> is to
466      * override the standard Java <code>toString</code> method.
467      **/

468     public PyString __str__() {
469         return __repr__();
470     }
471
472     /**
473      * Equivalent to the standard Python __hash__ method. This method can
474      * not be overridden. Instead, you should override the standard Java
475      * <code>hashCode</code> method to return an appropriate hash code for
476      * the <code>PyObject</code>.
477      **/

478     public final PyInteger __hash__() {
479         return new PyInteger(hashCode());
480     }
481
482     public int hashCode() {
483         return object_hashCode();
484     }
485
486     final int object_hashCode() {
487         return System.identityHashCode(this);
488     }
489
490     /**
491      * Should almost never be overridden.
492      * If overridden, it is the subclasses responsibility to ensure that
493      * <code>a.equals(b) == true</code> iff <code>cmp(a,b) == 0</code>
494      **/

495     public boolean equals(Object JavaDoc ob_other) {
496         return (ob_other instanceof PyObject)
497             && _eq((PyObject) ob_other).__nonzero__();
498     }
499
500     /**
501      * Equivalent to the standard Python __nonzero__ method.
502      * Returns whether of not a given <code>PyObject</code> is
503      * considered true.
504      **/

505     public boolean __nonzero__() {
506         return true;
507     }
508
509     /**
510      * Equivalent to the Jython __tojava__ method.
511      * Tries to coerce this object to an instance of the requested Java class.
512      * Returns the special object <code>Py.NoConversion</code>
513      * if this <code>PyObject</code> can not be converted to the
514      * desired Java class.
515      *
516      * @param c the Class to convert this <code>PyObject</code> to.
517      **/

518     public Object JavaDoc __tojava__(Class JavaDoc c) {
519         if (c.isInstance(this))
520             return this;
521         return Py.NoConversion;
522     }
523
524     /**
525      * The basic method to override when implementing a callable object.
526      *
527      * The first len(args)-len(keywords) members of args[] are plain
528      * arguments. The last len(keywords) arguments are the values of the
529      * keyword arguments.
530      *
531      * @param args all arguments to the function (including
532      * keyword arguments).
533      * @param keywords the keywords used for all keyword arguments.
534      **/

535     public PyObject __call__(PyObject args[], String JavaDoc keywords[]) {
536         throw Py.TypeError("call of non-function (" + safeRepr() + ")");
537     }
538
539     /**
540      * A variant of the __call__ method with one extra initial argument.
541      * This variant is used to allow method invocations to be performed
542      * efficiently.
543      *
544      * The default behavior is to invoke <code>__call__(args,
545      * keywords)</code> with the appropriate arguments. The only reason to
546      * override this function would be for improved performance.
547      *
548      * @param arg1 the first argument to the function.
549      * @param args the last arguments to the function (including
550      * keyword arguments).
551      * @param keywords the keywords used for all keyword arguments.
552      **/

553     public PyObject __call__(
554         PyObject arg1,
555         PyObject args[],
556         String JavaDoc keywords[]) {
557         PyObject[] newArgs = new PyObject[args.length + 1];
558         System.arraycopy(args, 0, newArgs, 1, args.length);
559         newArgs[0] = arg1;
560         return __call__(newArgs, keywords);
561     }
562
563     /**
564      * A variant of the __call__ method when no keywords are passed. The
565      * default behavior is to invoke <code>__call__(args, keywords)</code>
566      * with the appropriate arguments. The only reason to override this
567      * function would be for improved performance.
568      *
569      * @param args all arguments to the function.
570      **/

571     public PyObject __call__(PyObject args[]) {
572         return __call__(args, Py.NoKeywords);
573     }
574
575     /**
576      * A variant of the __call__ method with no arguments. The default
577      * behavior is to invoke <code>__call__(args, keywords)</code> with the
578      * appropriate arguments. The only reason to override this function
579      * would be for improved performance.
580      **/

581     public PyObject __call__() {
582         return __call__(Py.EmptyObjects, Py.NoKeywords);
583     }
584
585     /**
586      * A variant of the __call__ method with one argument. The default
587      * behavior is to invoke <code>__call__(args, keywords)</code> with the
588      * appropriate arguments. The only reason to override this function
589      * would be for improved performance.
590      *
591      * @param arg0 the single argument to the function.
592      **/

593     public PyObject __call__(PyObject arg0) {
594         return __call__(new PyObject[] { arg0 }, Py.NoKeywords);
595     }
596
597     /**
598      * A variant of the __call__ method with two arguments. The default
599      * behavior is to invoke <code>__call__(args, keywords)</code> with the
600      * appropriate arguments. The only reason to override this function
601      * would be for improved performance.
602      *
603      * @param arg0 the first argument to the function.
604      * @param arg1 the second argument to the function.
605      **/

606     public PyObject __call__(PyObject arg0, PyObject arg1) {
607         return __call__(new PyObject[] { arg0, arg1 }, Py.NoKeywords);
608     }
609
610     /**
611      * A variant of the __call__ method with three arguments. The default
612      * behavior is to invoke <code>__call__(args, keywords)</code> with the
613      * appropriate arguments. The only reason to override this function
614      * would be for improved performance.
615      *
616      * @param arg0 the first argument to the function.
617      * @param arg1 the second argument to the function.
618      * @param arg2 the third argument to the function.
619      **/

620     public PyObject __call__(PyObject arg0, PyObject arg1, PyObject arg2) {
621         return __call__(new PyObject[] { arg0, arg1, arg2 }, Py.NoKeywords);
622     }
623
624     /**
625      * A variant of the __call__ method with four arguments. The default
626      * behavior is to invoke <code>__call__(args, keywords)</code> with the
627      * appropriate arguments. The only reason to override this function
628      * would be for improved performance.
629      *
630      * @param arg0 the first argument to the function.
631      * @param arg1 the second argument to the function.
632      * @param arg2 the third argument to the function.
633      * @param arg3 the fourth argument to the function.
634      **/

635     public PyObject __call__(
636         PyObject arg0,
637         PyObject arg1,
638         PyObject arg2,
639         PyObject arg3) {
640         return __call__(
641             new PyObject[] { arg0, arg1, arg2, arg3 },
642             Py.NoKeywords);
643     }
644
645     /** @deprecated **/
646     public PyObject _callextra( // xxx fix, should work with iterators too
647
PyObject[] args,
648         String JavaDoc[] keywords,
649         PyObject starargs,
650         PyObject kwargs) {
651
652         int argslen = args.length;
653         int nstar = 0;
654
655         String JavaDoc name = "";
656         if (this instanceof PyFunction)
657             name = ((PyFunction) this).__name__ + "() ";
658
659         if (kwargs != null) {
660             PyObject keys = kwargs.__findattr__("keys");
661             if (keys == null)
662                 throw Py.TypeError(
663                     name + "argument after ** must be " + "a dictionary");
664             for (int i = 0; i < keywords.length; i++)
665                 if (kwargs.__finditem__(keywords[i]) != null)
666                     throw Py.TypeError(
667                         name
668                             + "got multiple values for "
669                             + "keyword argument '"
670                             + keywords[i]
671                             + "'");
672             argslen += kwargs.__len__();
673         }
674         if (starargs != null) {
675             if (!(starargs instanceof PySequence
676                 || starargs instanceof PyInstance)) // xxx
677
throw Py.TypeError(
678                     name + "argument after * must " + "be a sequence");
679             nstar = starargs.__len__();
680             argslen += nstar;
681         }
682
683         PyObject[] newargs = new PyObject[argslen];
684         int argidx = args.length - keywords.length;
685         System.arraycopy(args, 0, newargs, 0, argidx);
686
687         if (starargs != null) {
688             PyObject a;
689             for (int i = 0;
690                 (a = starargs.__finditem__(i)) != null && i < nstar;
691                 i++) {
692                 newargs[argidx++] = a;
693             }
694         }
695         System.arraycopy(
696             args,
697             args.length - keywords.length,
698             newargs,
699             argidx,
700             keywords.length);
701         argidx += keywords.length;
702
703         if (kwargs != null) {
704             String JavaDoc[] newkeywords =
705                 new String JavaDoc[keywords.length + kwargs.__len__()];
706             System.arraycopy(keywords, 0, newkeywords, 0, keywords.length);
707
708             PyObject keys = kwargs.invoke("keys");
709             PyObject key;
710             for (int i = 0;(key = keys.__finditem__(i)) != null; i++) {
711                 if (!(key instanceof PyString))
712                     throw Py.TypeError(name + "keywords must be strings");
713                 newkeywords[keywords.length + i] =
714                     ((PyString) key).internedString();
715                 newargs[argidx++] = kwargs.__finditem__(key);
716             }
717             keywords = newkeywords;
718         }
719
720         if (newargs.length != argidx) {
721             args = new PyObject[argidx];
722             System.arraycopy(newargs, 0, args, 0, argidx);
723         } else
724             args = newargs;
725         return __call__(args, keywords);
726     }
727
728     /* xxx fix these around */
729
730     public boolean isCallable() {
731         return __findattr__("__call__") != null;
732     }
733     public boolean isMappingType() {
734         return true;
735     }
736     public boolean isNumberType() {
737         return true;
738     }
739     public boolean isSequenceType() {
740         return true;
741     }
742
743     /* . */
744
745     /* The basic functions to implement a mapping */
746
747     /**
748      * Equivalent to the standard Python __len__ method.
749      * Part of the mapping discipline.
750      *
751      * @return the length of the object
752      **/

753     public int __len__() {
754         throw Py.AttributeError("__len__");
755     }
756
757     /**
758      * Very similar to the standard Python __getitem__ method.
759      * Instead of throwing a KeyError if the item isn't found,
760      * this just returns null.
761      *
762      * Classes that wish to implement __getitem__ should
763      * override this method instead (with the appropriate
764      * semantics.
765      *
766      * @param key the key to lookup in this container
767      *
768      * @return the value corresponding to key or null if key is not found
769      **/

770     public PyObject __finditem__(PyObject key) {
771         throw Py.AttributeError("__getitem__");
772     }
773
774     /**
775      * A variant of the __finditem__ method which accepts a primitive
776      * <code>int</code> as the key. By default, this method will call
777      * <code>__finditem__(PyObject key)</code> with the appropriate args.
778      * The only reason to override this method is for performance.
779      *
780      * @param key the key to lookup in this sequence.
781      * @return the value corresponding to key or null if key is not found.
782      *
783      * @see #__finditem__(PyObject)
784      **/

785     public PyObject __finditem__(int key) {
786         return __finditem__(new PyInteger(key));
787     }
788
789     /**
790      * A variant of the __finditem__ method which accepts a Java
791      * <code>String</code> as the key. By default, this method will call
792      * <code>__finditem__(PyObject key)</code> with the appropriate args.
793      * The only reason to override this method is for performance.
794      *
795      * <b>Warning: key must be an interned string!!!!!!!!</b>
796      *
797      * @param key the key to lookup in this sequence -
798      * <b> must be an interned string </b>.
799      * @return the value corresponding to key or null if key is not found.
800      *
801      * @see #__finditem__(PyObject)
802      **/

803     public PyObject __finditem__(String JavaDoc key) {
804         return __finditem__(new PyString(key));
805     }
806
807     /**
808      * Equivalent to the standard Python __getitem__ method.
809      * This variant takes a primitive <code>int</code> as the key.
810      * This method should not be overridden.
811      * Override the <code>__finditem__</code> method instead.
812      *
813      * @param key the key to lookup in this container.
814      * @return the value corresponding to that key.
815      * @exception Py.KeyError if the key is not found.
816      *
817      * @see #__finditem__(int)
818      **/

819     public PyObject __getitem__(int key) {
820         PyObject ret = __finditem__(key);
821         if (ret == null)
822             throw Py.KeyError("" + key);
823         return ret;
824     }
825
826     /**
827      * Equivalent to the standard Python __getitem__ method.
828      * This method should not be overridden.
829      * Override the <code>__finditem__</code> method instead.
830      *
831      * @param key the key to lookup in this container.
832      * @return the value corresponding to that key.
833      * @exception Py.KeyError if the key is not found.
834      *
835      * @see #__finditem__(PyObject)
836      **/

837     public PyObject __getitem__(PyObject key) {
838         PyObject ret = __finditem__(key);
839         if (ret == null)
840             throw Py.KeyError(key.toString());
841         return ret;
842     }
843
844     /**
845      * Equivalent to the standard Python __setitem__ method.
846      *
847      * @param key the key whose value will be set
848      * @param value the value to set this key to
849      **/

850     public void __setitem__(PyObject key, PyObject value) {
851         throw Py.AttributeError("__setitem__");
852     }
853
854     /**
855      * A variant of the __setitem__ method which accepts a String
856      * as the key. <b>This String must be interned</b>.
857      * By default, this will call
858      * <code>__setitem__(PyObject key, PyObject value)</code>
859      * with the appropriate args.
860      * The only reason to override this method is for performance.
861      *
862      * @param key the key whose value will be set -
863      * <b> must be an interned string </b>.
864      * @param value the value to set this key to
865      *
866      * @see #__setitem__(PyObject, PyObject)
867      **/

868     public void __setitem__(String JavaDoc key, PyObject value) {
869         __setitem__(new PyString(key), value);
870     }
871
872     /**
873      * A variant of the __setitem__ method which accepts a primitive
874      * <code>int</code> as the key.
875      * By default, this will call
876      * <code>__setitem__(PyObject key, PyObject value)</code>
877      * with the appropriate args.
878      * The only reason to override this method is for performance.
879      *
880      * @param key the key whose value will be set
881      * @param value the value to set this key to
882      *
883      * @see #__setitem__(PyObject, PyObject)
884      **/

885     public void __setitem__(int key, PyObject value) {
886         __setitem__(new PyInteger(key), value);
887     }
888
889     /**
890      * Equivalent to the standard Python __delitem__ method.
891      *
892      * @param key the key to be removed from the container
893      * @exception Py.KeyError if the key is not found in the container
894      **/

895     public void __delitem__(PyObject key) {
896         throw Py.AttributeError("__delitem__");
897     }
898
899     /**
900      * A variant of the __delitem__ method which accepts a String
901      * as the key. <b>This String must be interned</b>.
902      * By default, this will call
903      * <code>__delitem__(PyObject key)</code>
904      * with the appropriate args.
905      * The only reason to override this method is for performance.
906      *
907      * @param key the key who will be removed -
908      * <b> must be an interned string </b>.
909      * @exception Py.KeyError if the key is not found in the container
910      *
911      * @see #__delitem__(PyObject)
912      **/

913     public void __delitem__(String JavaDoc key) {
914         __delitem__(new PyString(key));
915     }
916
917     public PyObject __getslice__(
918         PyObject s_start,
919         PyObject s_stop,
920         PyObject s_step) {
921         PySlice s = new PySlice(s_start, s_stop, s_step);
922         return __getitem__(s);
923     }
924
925     public void __setslice__(
926         PyObject s_start,
927         PyObject s_stop,
928         PyObject s_step,
929         PyObject value) {
930         PySlice s = new PySlice(s_start, s_stop, s_step);
931         __setitem__(s, value);
932     }
933
934     public void __delslice__(
935         PyObject s_start,
936         PyObject s_stop,
937         PyObject s_step) {
938         PySlice s = new PySlice(s_start, s_stop, s_step);
939         __delitem__(s);
940     }
941
942     public PyObject __getslice__(PyObject start, PyObject stop) {
943         return __getslice__(start, stop, Py.One);
944     }
945
946     public void __setslice__(PyObject start, PyObject stop, PyObject value) {
947         __setslice__(start, stop, Py.One, value);
948     }
949
950     public void __delslice__(PyObject start, PyObject stop) {
951         __delslice__(start, stop, Py.One);
952     }
953
954     /*The basic functions to implement an iterator */
955
956     /**
957      * Return an iterator that is used to iterate the element of this
958      * sequence.
959      * From version 2.2, this method is the primary protocol for looping
960      * over sequences.
961      * <p>
962      * If a PyObject subclass should support iteration based in the
963      * __finditem__() method, it must supply an implementation of __iter__()
964      * like this:
965      * <pre>
966      * public PyObject __iter__() {
967      * return new PySequenceIter(this);
968      * }
969      * </pre>
970      *
971      * When iterating over a python sequence from java code, it should be
972      * done with code like this:
973      * <pre>
974      * PyObject iter = seq.__iter__();
975      * for (PyObject item; (item = iter.__next__()) != null; {
976      * // Do somting with item
977      * }
978      * </pre>
979      *
980      * @since 2.2
981      */

982     public PyObject __iter__() {
983         throw Py.TypeError("iteration over non-sequence");
984     }
985
986     /**
987      * Return the next element of the sequence that this is an iterator
988      * for. Returns null when the end of the sequence is reached.
989      *
990      * @since 2.2
991      */

992     public PyObject __iternext__() {
993         return null;
994     }
995
996     /*The basic functions to implement a namespace*/
997
998     /**
999      * Very similar to the standard Python __getattr__ method.
1000     * Instead of throwing a AttributeError if the item isn't found,
1001     * this just returns null.
1002     *
1003     * Classes that wish to implement __getattr__ should
1004     * override this method instead (with the appropriate
1005     * semantics.
1006     *
1007     * @param name the name to lookup in this namespace
1008     *
1009     * @return the value corresponding to name or null if name is not found
1010     **/

1011    public final PyObject __findattr__(PyString name) {
1012        if (name == null) {
1013            return null;
1014        }
1015        return __findattr__(name.internedString());
1016    }
1017
1018    /**
1019     * A variant of the __findattr__ method which accepts a Java
1020     * <code>String</code> as the name.
1021     *
1022     * By default, this method will call <code>__findattr__(PyString
1023     * name)</code> with the appropriate args. The only reason to override
1024     * this method is for performance.
1025     *
1026     * <b>Warning: name must be an interned string!!!!!!!!</b>
1027     *
1028     * @param name the name to lookup in this namespace
1029     * <b> must be an interned string </b>.
1030     * @return the value corresponding to name or null if name is not found
1031     *
1032     * @see #__findattr__(PyString)
1033     **/

1034    public PyObject __findattr__(String JavaDoc name) { // xxx accelerators/ expose
1035
/*if (getType() == null)
1036            return null;
1037        if (name == "__class__")
1038            return getType();*/

1039        /*PyObject ret = getType().lookup(name, false);
1040        if (ret != null)
1041            return ret._doget(this);
1042        return null;*/

1043
1044        return object___findattr__(name);
1045    }
1046
1047    /**
1048     * Equivalent to the standard Python __getattr__ method.
1049     * This method can not be overridden.
1050     * Override the <code>__findattr__</code> method instead.
1051     *
1052     * @param name the name to lookup in this namespace
1053     * @return the value corresponding to name
1054     * @exception Py.AttributeError if the name is not found.
1055     *
1056     * @see #__findattr__(PyString)
1057     **/

1058    public final PyObject __getattr__(PyString name) {
1059        PyObject ret = __findattr__(name);
1060        if (ret == null)
1061            noAttributeError(name.toString());
1062        return ret;
1063    }
1064
1065    /**
1066     * A variant of the __getattr__ method which accepts a Java
1067     * <code>String</code> as the name.
1068     * This method can not be overridden.
1069     * Override the <code>__findattr__</code> method instead.
1070     *
1071     * <b>Warning: name must be an interned string!!!!!!!!</b>
1072     *
1073     * @param name the name to lookup in this namespace
1074     * <b> must be an interned string </b>.
1075     * @return the value corresponding to name
1076     * @exception Py.AttributeError if the name is not found.
1077     *
1078     * @see #__findattr__(java.lang.String)
1079     **/

1080    public final PyObject __getattr__(String JavaDoc name) {
1081        PyObject ret = __findattr__(name);
1082        if (ret == null)
1083            noAttributeError(name);
1084        return ret;
1085    }
1086
1087    public void noAttributeError(String JavaDoc name) {
1088        throw Py.AttributeError(
1089            safeRepr() + " has no attribute '" + name + "'");
1090    }
1091
1092    public void readonlyAttributeError(String JavaDoc name) {
1093        throw Py.AttributeError(
1094            safeRepr() + " attribute '" + name + "' is read-only");
1095    }
1096
1097    /**
1098     * Equivalent to the standard Python __setattr__ method.
1099     * This method can not be overridden.
1100     *
1101     * @param name the name to lookup in this namespace
1102     * @exception Py.AttributeError if the name is not found.
1103     *
1104     * @see #__setattr__(java.lang.String, PyObject)
1105     **/

1106    public final void __setattr__(PyString name, PyObject value) {
1107        __setattr__(name.internedString(), value);
1108    }
1109
1110    /**
1111     * A variant of the __setattr__ method which accepts a String
1112     * as the key. <b>This String must be interned</b>.
1113     *
1114     * @param name the name whose value will be set -
1115     * <b> must be an interned string </b>.
1116     * @param value the value to set this name to
1117     *
1118     * @see #__setattr__(PyString, PyObject)
1119    **/

1120    public void __setattr__(String JavaDoc name, PyObject value) {
1121        object___setattr__(name, value);
1122    }
1123
1124    /**
1125     * Equivalent to the standard Python __delattr__ method.
1126     * This method can not be overridden.
1127     *
1128     * @param name the name to which will be removed
1129     * @exception Py.AttributeError if the name doesn't exist
1130     *
1131     * @see #__delattr__(java.lang.String)
1132     **/

1133    public final void __delattr__(PyString name) {
1134        __delattr__(name.internedString());
1135    }
1136
1137    /**
1138     * A variant of the __delattr__ method which accepts a String
1139     * as the key. <b>This String must be interned</b>.
1140     * By default, this will call
1141     * <code>__delattr__(PyString name)</code>
1142     * with the appropriate args.
1143     * The only reason to override this method is for performance.
1144     *
1145     * @param name the name which will be removed -
1146     * <b> must be an interned string </b>.
1147     * @exception Py.AttributeError if the name doesn't exist
1148     *
1149     * @see #__delattr__(PyString)
1150     **/

1151    public void __delattr__(String JavaDoc name) {
1152        object___delattr__(name);
1153    }
1154
1155    // Used by import logic.
1156
protected PyObject impAttr(String JavaDoc name) {
1157        return __findattr__(name);
1158    }
1159
1160    protected void addKeys(PyDictionary accum, String JavaDoc attr) {
1161        PyObject obj = __findattr__(attr);
1162        if (obj == null)
1163            return;
1164        if (obj instanceof PyList) {
1165            PyObject lst_iter = obj.__iter__();
1166            PyObject name;
1167            for (; (name = lst_iter.__iternext__())!= null; ) {
1168                accum.__setitem__(name, Py.None);
1169            }
1170        }
1171        accum.update(obj);
1172    }
1173
1174    protected void __rawdir__(PyDictionary accum) {
1175        addKeys(accum, "__dict__");
1176        addKeys(accum, "__methods__");
1177        addKeys(accum, "__members__");
1178        fastGetClass().__rawdir__(accum);
1179    }
1180
1181    /**
1182     * Equivalent to the standard Python __dir__ method.
1183     *
1184     * @return a list of names defined by this object.
1185     **/

1186    public PyObject __dir__() {
1187        PyDictionary accum = new PyDictionary();
1188        __rawdir__(accum);
1189        PyList ret = accum.keys();
1190        ret.sort();
1191        return ret;
1192    }
1193
1194    public PyObject _doget(PyObject container) {
1195        return this;
1196    }
1197
1198    public PyObject _doget(PyObject container, PyObject wherefound) {
1199        return _doget(container);
1200    }
1201
1202    public boolean _doset(PyObject container, PyObject value) {
1203        return false;
1204    }
1205
1206    boolean jtryset(PyObject container, PyObject value) {
1207        return _doset(container, value);
1208    }
1209
1210    boolean jdontdel() {
1211        return false;
1212    }
1213
1214    /* Numeric coercion */
1215
1216    /**
1217     * Implements numeric coercion
1218     *
1219     * @param o the other object involved in the coercion
1220     * @return null if no coercion is possible;
1221     * a single PyObject to use to replace o if this is unchanged;
1222     * or a PyObject[2] consisting of replacements for this and o.
1223     **/

1224    public Object JavaDoc __coerce_ex__(PyObject o) {
1225        return null;
1226    }
1227
1228    /**
1229     * Implements coerce(this,other), result as PyObject[]
1230     * @param other
1231     * @return PyObject[]
1232     */

1233    PyObject[] _coerce(PyObject other) {
1234        Object JavaDoc result;
1235        if (this.getType() == other.getType() &&
1236            !(this instanceof PyInstance)) {
1237            return new PyObject[] {this, other};
1238        }
1239        result = this.__coerce_ex__(other);
1240        if (result != null && result != Py.None) {
1241            if (result instanceof PyObject[]) {
1242                return (PyObject[])result;
1243            } else {
1244                return new PyObject[] {this, (PyObject)result};
1245            }
1246        }
1247        result = other.__coerce_ex__(this);
1248        if (result != null && result != Py.None) {
1249            if (result instanceof PyObject[]) {
1250                return (PyObject[])result;
1251            } else {
1252                return new PyObject[] {(PyObject)result, other};
1253            }
1254        }
1255        return null;
1256
1257    }
1258
1259    /**
1260     * Equivalent to the standard Python __coerce__ method.
1261     *
1262     * This method can not be overridden.
1263     * To implement __coerce__ functionality, override __coerce_ex__ instead.
1264     *
1265     * @param pyo the other object involved in the coercion.
1266     * @return a tuple of this object and pyo coerced to the same type
1267     * or Py.None if no coercion is possible.
1268     * @see org.python.core.PyObject#__coerce_ex__(org.python.core.PyObject)
1269     **/

1270    public final PyObject __coerce__(PyObject pyo) {
1271        Object JavaDoc o = __coerce_ex__(pyo);
1272        if (o == null)
1273            throw Py.AttributeError("__coerce__");
1274        if (o == Py.None)
1275            return (PyObject) o;
1276        if (o instanceof PyObject[])
1277            return new PyTuple((PyObject[]) o);
1278        else
1279            return new PyTuple(new PyObject[] { this, (PyObject) o });
1280    }
1281
1282
1283
1284    /* The basic comparision operations */
1285
1286    /**
1287     * Equivalent to the standard Python __cmp__ method.
1288     *
1289     * @param other the object to compare this with.
1290     * @return -1 if this < 0; 0 if this == o; +1 if this > o; -2 if no
1291     * comparison is implemented
1292     **/

1293    public int __cmp__(PyObject other) {
1294        return -2;
1295    }
1296
1297    /**
1298     * Equivalent to the standard Python __eq__ method.
1299     *
1300     * @param other the object to compare this with.
1301     * @return the result of the comparison.
1302     **/

1303    public PyObject __eq__(PyObject other) {
1304        return null;
1305    }
1306
1307    /**
1308     * Equivalent to the standard Python __ne__ method.
1309     *
1310     * @param other the object to compare this with.
1311     * @return the result of the comparison.
1312     **/

1313    public PyObject __ne__(PyObject other) {
1314        return null;
1315    }
1316
1317    /**
1318     * Equivalent to the standard Python __le__ method.
1319     *
1320     * @param other the object to compare this with.
1321     * @return the result of the comparison.
1322     **/

1323    public PyObject __le__(PyObject other) {
1324        return null;
1325    }
1326
1327    /**
1328     * Equivalent to the standard Python __lt__ method.
1329     *
1330     * @param other the object to compare this with.
1331     * @return the result of the comparison.
1332     **/

1333    public PyObject __lt__(PyObject other) {
1334        return null;
1335    }
1336
1337    /**
1338     * Equivalent to the standard Python __ge__ method.
1339     *
1340     * @param other the object to compare this with.
1341     * @return the result of the comparison.
1342     **/

1343    public PyObject __ge__(PyObject other) {
1344        return null;
1345    }
1346
1347    /**
1348     * Equivalent to the standard Python __gt__ method.
1349     *
1350     * @param other the object to compare this with.
1351     * @return the result of the comparison.
1352     **/

1353    public PyObject __gt__(PyObject other) {
1354        return null;
1355    }
1356
1357    /**
1358     * Implements cmp(this, other)
1359     *
1360     * @param o the object to compare this with.
1361     * @return -1 if this < 0; 0 if this == o; +1 if this > o
1362     **/

1363    public final int _cmp(PyObject o) {
1364        PyObject token = null;
1365        ThreadState ts = Py.getThreadState();
1366        try {
1367            if (++ts.compareStateNesting > 500) {
1368                if ((token = check_recursion(ts, this, o)) == null)
1369                    return 0;
1370            }
1371
1372            PyObject r;
1373            r = __eq__(o);
1374            if (r != null && r.__nonzero__())
1375                return 0;
1376            r = o.__eq__(this);
1377            if (r != null && r.__nonzero__())
1378                return 0;
1379
1380            r = __lt__(o);
1381            if (r != null && r.__nonzero__())
1382                return -1;
1383            r = o.__gt__(this);
1384            if (r != null && r.__nonzero__())
1385                return -1;
1386
1387            r = __gt__(o);
1388            if (r != null && r.__nonzero__())
1389                return 1;
1390            r = o.__lt__(this);
1391            if (r != null && r.__nonzero__())
1392                return 1;
1393
1394            return _cmp_unsafe(o);
1395        } finally {
1396            delete_token(ts, token);
1397            ts.compareStateNesting--;
1398        }
1399    }
1400
1401    private PyObject make_pair(PyObject o) {
1402        if (System.identityHashCode(this) < System.identityHashCode(o))
1403            return new PyIdentityTuple(new PyObject[] { this, o });
1404        else
1405            return new PyIdentityTuple(new PyObject[] { o, this });
1406    }
1407
1408    private final int _default_cmp(PyObject other) {
1409        int result;
1410        if (this._is(other).__nonzero__())
1411            return 0;
1412
1413        /* None is smaller than anything */
1414        if (this == Py.None)
1415            return -1;
1416        if (other == Py.None)
1417            return 1;
1418
1419        // No rational way to compare these, so ask their classes to compare
1420
PyType this_type = this.getType();
1421        PyType other_type = other.getType();
1422        if (this_type == other_type) {
1423            return Py.id(this) < Py.id(other)? -1: 1;
1424        }
1425        result = this_type.fastGetName().compareTo(other_type.fastGetName());
1426        if (result == 0)
1427            return Py.id(this_type)<Py.id(other_type)? -1: 1;
1428        return result < 0? -1: 1;
1429    }
1430
1431    private final int _cmp_unsafe(PyObject other) {
1432        // Shortcut for equal objects
1433
if (this == other)
1434            return 0;
1435
1436        int result;
1437        result = this.__cmp__(other);
1438        if (result != -2)
1439            return result;
1440
1441        if (!(this instanceof PyInstance)) {
1442            result = other.__cmp__(this);
1443            if (result != -2)
1444                return -result;
1445        }
1446
1447        return this._default_cmp(other);
1448    }
1449
1450    /*
1451     * Like _cmp_unsafe but limited to ==/!= as 0/!=0,
1452     * avoids to invoke Py.id
1453     */

1454    private final int _cmpeq_unsafe(PyObject other) {
1455        // Shortcut for equal objects
1456
if (this == other)
1457            return 0;
1458
1459        int result;
1460        result = this.__cmp__(other);
1461        if (result != -2)
1462            return result;
1463
1464        if (!(this instanceof PyInstance)) {
1465            result = other.__cmp__(this);
1466            if (result != -2)
1467                return -result;
1468        }
1469
1470        return this._is(other).__nonzero__()?0:1;
1471    }
1472
1473
1474
1475    private final static PyObject check_recursion(
1476        ThreadState ts,
1477        PyObject o1,
1478        PyObject o2) {
1479        PyDictionary stateDict = ts.getCompareStateDict();
1480
1481        PyObject pair = o1.make_pair(o2);
1482
1483        if (stateDict.__finditem__(pair) != null)
1484            return null;
1485
1486        stateDict.__setitem__(pair, pair);
1487        return pair;
1488    }
1489
1490    private final static void delete_token(ThreadState ts, PyObject token) {
1491        if (token == null)
1492            return;
1493        PyDictionary stateDict = ts.getCompareStateDict();
1494
1495        stateDict.__delitem__(token);
1496    }
1497
1498    /**
1499     * Implements the Python expression <code>this == other</code>.
1500     *
1501     * @param o the object to compare this with.
1502     * @return the result of the comparison
1503     **/

1504    public final PyObject _eq(PyObject o) {
1505        PyObject token = null;
1506
1507        ThreadState ts = Py.getThreadState();
1508        try {
1509            if (++ts.compareStateNesting > 10) {
1510                if ((token = check_recursion(ts, this, o)) == null)
1511                    return Py.One;
1512            }
1513            PyObject res = __eq__(o);
1514            if (res != null)
1515                return res;
1516            res = o.__eq__(this);
1517            if (res != null)
1518                return res;
1519            return _cmpeq_unsafe(o) == 0 ? Py.One : Py.Zero;
1520        } catch (PyException e) {
1521            if (Py.matchException(e, Py.AttributeError)) {
1522                return Py.Zero;
1523            }
1524            throw e;
1525        } finally {
1526            delete_token(ts, token);
1527            ts.compareStateNesting--;
1528        }
1529    }
1530
1531    /**
1532     * Implements the Python expression <code>this != other</code>.
1533     *
1534     * @param o the object to compare this with.
1535     * @return the result of the comparison
1536     **/

1537    public final PyObject _ne(PyObject o) {
1538        PyObject token = null;
1539
1540        ThreadState ts = Py.getThreadState();
1541        try {
1542            if (++ts.compareStateNesting > 10) {
1543                if ((token = check_recursion(ts, this, o)) == null)
1544                    return Py.Zero;
1545            }
1546            PyObject res = __ne__(o);
1547            if (res != null)
1548                return res;
1549            res = o.__ne__(this);
1550            if (res != null)
1551                return res;
1552            return _cmpeq_unsafe(o) != 0 ? Py.One : Py.Zero;
1553        } finally {
1554            delete_token(ts, token);
1555            ts.compareStateNesting--;
1556        }
1557    }
1558
1559    /**
1560     * Implements the Python expression <code>this &lt;= other</code>.
1561     *
1562     * @param o the object to compare this with.
1563     * @return the result of the comparison
1564     **/

1565    public final PyObject _le(PyObject o) {
1566        PyObject token = null;
1567
1568        ThreadState ts = Py.getThreadState();
1569        try {
1570            if (++ts.compareStateNesting > 10) {
1571                if ((token = check_recursion(ts, this, o)) == null)
1572                    throw Py.ValueError("can't order recursive values");
1573            }
1574            PyObject res = __le__(o);
1575            if (res != null)
1576                return res;
1577            res = o.__ge__(this);
1578            if (res != null)
1579                return res;
1580            return _cmp_unsafe(o) <= 0 ? Py.One : Py.Zero;
1581        } finally {
1582            delete_token(ts, token);
1583            ts.compareStateNesting--;
1584        }
1585    }
1586
1587    /**
1588     * Implements the Python expression <code>this &lt; other</code>.
1589     *
1590     * @param o the object to compare this with.
1591     * @return the result of the comparison
1592     **/

1593    public final PyObject _lt(PyObject o) {
1594        PyObject token = null;
1595
1596        ThreadState ts = Py.getThreadState();
1597        try {
1598            if (++ts.compareStateNesting > 10) {
1599                if ((token = check_recursion(ts, this, o)) == null)
1600                    throw Py.ValueError("can't order recursive values");
1601            }
1602            PyObject res = __lt__(o);
1603            if (res != null)
1604                return res;
1605            res = o.__gt__(this);
1606            if (res != null)
1607                return res;
1608            return _cmp_unsafe(o) < 0 ? Py.One : Py.Zero;
1609        } finally {
1610            delete_token(ts, token);
1611            ts.compareStateNesting--;
1612        }
1613    }
1614
1615    /**
1616     * Implements the Python expression <code>this &gt;= other</code>.
1617     *
1618     * @param o the object to compare this with.
1619     * @return the result of the comparison
1620     **/

1621    public final PyObject _ge(PyObject o) {
1622        PyObject token = null;
1623
1624        ThreadState ts = Py.getThreadState();
1625        try {
1626            if (++ts.compareStateNesting > 10) {
1627                if ((token = check_recursion(ts, this, o)) == null)
1628                    throw Py.ValueError("can't order recursive values");
1629            }
1630            PyObject res = __ge__(o);
1631            if (res != null)
1632                return res;
1633            res = o.__le__(this);
1634            if (res != null)
1635                return res;
1636            return _cmp_unsafe(o) >= 0 ? Py.One : Py.Zero;
1637        } finally {
1638            delete_token(ts, token);
1639            ts.compareStateNesting--;
1640        }
1641    }
1642
1643    /**
1644     * Implements the Python expression <code>this &gt; other</code>.
1645     *
1646     * @param o the object to compare this with.
1647     * @return the result of the comparison
1648     **/

1649    public final PyObject _gt(PyObject o) {
1650        PyObject token = null;
1651
1652        ThreadState ts = Py.getThreadState();
1653        try {
1654            if (++ts.compareStateNesting > 10) {
1655                if ((token = check_recursion(ts, this, o)) == null)
1656                    throw Py.ValueError("can't order recursive values");
1657            }
1658            PyObject res = __gt__(o);
1659            if (res != null)
1660                return res;
1661            res = o.__lt__(this);
1662            if (res != null)
1663                return res;
1664            return _cmp_unsafe(o) > 0 ? Py.One : Py.Zero;
1665        } finally {
1666            delete_token(ts, token);
1667            ts.compareStateNesting--;
1668        }
1669
1670    }
1671
1672    /**
1673     * Implements <code>is</code> operator.
1674     *
1675     * @param o the object to compare this with.
1676     * @return the result of the comparison
1677     **/

1678    public PyObject _is(PyObject o) {
1679        return this == o ? Py.One : Py.Zero;
1680    }
1681
1682    /**
1683     * Implements <code>is not</code> operator.
1684     *
1685     * @param o the object to compare this with.
1686     * @return the result of the comparison
1687     **/

1688    public PyObject _isnot(PyObject o) {
1689        return this != o ? Py.One : Py.Zero;
1690    }
1691
1692    /**
1693     * Implements <code>in</code> operator.
1694     *
1695     * @param o the container to search for this element.
1696     * @return the result of the search.
1697     **/

1698    public final PyObject _in(PyObject o) {
1699        return Py.newBoolean(o.__contains__(this));
1700    }
1701
1702    /**
1703     * Implements <code>not in</code> operator.
1704     *
1705     * @param o the container to search for this element.
1706     * @return the result of the search.
1707     **/

1708    public final PyObject _notin(PyObject o) {
1709        return Py.newBoolean(!o.__contains__(this));
1710    }
1711
1712    /**
1713     * Equivalent to the standard Python __contains__ method.
1714     *
1715     * @param o the element to search for in this container.
1716     * @return the result of the search.
1717     **/

1718    public boolean __contains__(PyObject o) {
1719        return object___contains__(o);
1720    }
1721
1722    final boolean object___contains__(PyObject o) {
1723        PyObject iter = __iter__();
1724        for (PyObject item = null;(item = iter.__iternext__()) != null;) {
1725            if (o._eq(item).__nonzero__())
1726                return true;
1727        }
1728        return false;
1729    }
1730
1731    /**
1732     * Implements boolean not
1733     *
1734     * @return not this.
1735     **/

1736    public PyObject __not__() {
1737        return __nonzero__() ? Py.Zero : Py.One;
1738    }
1739
1740    /* The basic numeric operations */
1741
1742    /**
1743     * Equivalent to the standard Python __hex__ method
1744     * Should only be overridden by numeric objects that can be
1745     * reasonably represented as a hexadecimal string.
1746     *
1747     * @return a string representing this object as a hexadecimal number.
1748     **/

1749    public PyString __hex__() {
1750        throw Py.AttributeError("__hex__");
1751    }
1752
1753    /**
1754     * Equivalent to the standard Python __oct__ method.
1755     * Should only be overridden by numeric objects that can be
1756     * reasonably represented as an octal string.
1757     *
1758     * @return a string representing this object as an octal number.
1759     **/

1760    public PyString __oct__() {
1761        throw Py.AttributeError("__oct__");
1762    }
1763
1764    /**
1765     * Equivalent to the standard Python __int__ method.
1766     * Should only be overridden by numeric objects that can be
1767     * reasonably coerced into an integer.
1768     *
1769     * @return an integer corresponding to the value of this object.
1770     **/

1771    public PyObject __int__() {
1772        throw Py.AttributeError("__int__");
1773    }
1774
1775    /**
1776     * Equivalent to the standard Python __long__ method.
1777     * Should only be overridden by numeric objects that can be
1778     * reasonably coerced into a python long.
1779     *
1780     * @return a PyLong corresponding to the value of this object.
1781     **/

1782    public PyLong __long__() {
1783        throw Py.AttributeError("__long__");
1784    }
1785
1786    /**
1787     * Equivalent to the standard Python __float__ method.
1788     * Should only be overridden by numeric objects that can be
1789     * reasonably coerced into a python float.
1790     *
1791     * @return a float corresponding to the value of this object.
1792     **/

1793    public PyFloat __float__() {
1794        throw Py.AttributeError("__float__");
1795    }
1796
1797    /**
1798     * Equivalent to the standard Python __complex__ method.
1799     * Should only be overridden by numeric objects that can be
1800     * reasonably coerced into a python complex number.
1801     *
1802     * @return a complex number corresponding to the value of this object.
1803     **/

1804    public PyComplex __complex__() {
1805        throw Py.AttributeError("__complex__");
1806    }
1807
1808    /**
1809     * Equivalent to the standard Python __pos__ method.
1810     *
1811     * @return +this.
1812     **/

1813    public PyObject __pos__() {
1814        throw Py.AttributeError("__pos__");
1815    }
1816
1817    /**
1818     * Equivalent to the standard Python __neg__ method.
1819     *
1820     * @return -this.
1821     **/

1822    public PyObject __neg__() {
1823        throw Py.AttributeError("__neg__");
1824    }
1825
1826    /**
1827     * Equivalent to the standard Python __abs__ method.
1828     *
1829     * @return abs(this).
1830     **/

1831    public PyObject __abs__() {
1832        throw Py.AttributeError("__abs__");
1833    }
1834
1835    /**
1836     * Equivalent to the standard Python __invert__ method.
1837     *
1838     * @return ~this.
1839     **/

1840    public PyObject __invert__() {
1841        throw Py.AttributeError("__invert__");
1842    }
1843
1844    /**
1845     * Implements the three argument power function.
1846     *
1847     * @param o2 the power to raise this number to.
1848     * @param o3 the modulus to perform this operation in or null if no
1849     * modulo is to be used
1850     * @return this object raised to the given power in the given modulus
1851     **/

1852    public PyObject __pow__(PyObject o2, PyObject o3) {
1853        return null;
1854    }
1855
1856    // Generated by make_binops.py (Begin)
1857

1858    /**
1859     * Equivalent to the standard Python __add__ method
1860     * @param other the object to perform this binary operation with
1861     * (the right-hand operand).
1862     * @return the result of the add, or null if this operation
1863     * is not defined
1864     **/

1865    public PyObject __add__(PyObject other) {
1866        return null;
1867    }
1868
1869    /**
1870     * Equivalent to the standard Python __radd__ method
1871     * @param other the object to perform this binary operation with
1872     * (the left-hand operand).
1873     * @return the result of the add, or null if this operation
1874     * is not defined.
1875     **/

1876    public PyObject __radd__(PyObject other) {
1877        return null;
1878    }
1879
1880    /**
1881     * Equivalent to the standard Python __iadd__ method
1882     * @param other the object to perform this binary operation with
1883     * (the right-hand operand).
1884     * @return the result of the add, or null if this operation
1885     * is not defined
1886     **/

1887    public PyObject __iadd__(PyObject other) {
1888        return _add(other);
1889    }
1890
1891    /**
1892     * Implements the Python expression <code>this + other</code>
1893     * @param o2 the object to perform this binary operation with.
1894     * @return the result of the add.
1895     * @exception Py.TypeError if this operation can't be performed
1896     * with these operands.
1897     **/

1898    public final PyObject _add(PyObject o2) {
1899        PyObject x = __add__(o2);
1900        if (x != null) {
1901            return x;
1902        }
1903        x = o2.__radd__(this);
1904        if (x != null) {
1905            return x;
1906        }
1907        throw Py.TypeError(_unsupportedop("+", o2));
1908    }
1909
1910    /**
1911     * @param op the String form of the op (e.g. "+")
1912     * @param o2 the right operand
1913     */

1914    protected final String JavaDoc _unsupportedop(String JavaDoc op, PyObject o2) {
1915        Object JavaDoc[] args = {op, getType().fastGetName(), o2.getType().fastGetName()};
1916        String JavaDoc msg = unsupportedopMessage(op, o2);
1917        if (msg == null) {
1918            msg = o2.runsupportedopMessage(op, o2);
1919        }
1920        if (msg == null) {
1921            msg = "unsupported operand type(s) for {0}: ''{1}'' and ''{2}''";
1922        }
1923        return MessageFormat.format(msg, args);
1924    }
1925
1926    /**
1927     * Should return an error message suitable for substitution where.
1928     *
1929     * {0} is the op name.
1930     * {1} is the left operand type.
1931     * {2} is the right operand type.
1932     */

1933    protected String JavaDoc unsupportedopMessage(String JavaDoc op, PyObject o2) {
1934        return null;
1935    }
1936    
1937    /**
1938     * Should return an error message suitable for substitution where.
1939     *
1940     * {0} is the op name.
1941     * {1} is the left operand type.
1942     * {2} is the right operand type.
1943     */

1944    protected String JavaDoc runsupportedopMessage(String JavaDoc op, PyObject o2) {
1945        return null;
1946    }
1947
1948
1949    /**
1950     * Equivalent to the standard Python __sub__ method
1951     * @param other the object to perform this binary operation with
1952     * (the right-hand operand).
1953     * @return the result of the sub, or null if this operation
1954     * is not defined
1955     **/

1956    public PyObject __sub__(PyObject other) {
1957        return null;
1958    }
1959
1960    /**
1961     * Equivalent to the standard Python __rsub__ method
1962     * @param other the object to perform this binary operation with
1963     * (the left-hand operand).
1964     * @return the result of the sub, or null if this operation
1965     * is not defined.
1966     **/

1967    public PyObject __rsub__(PyObject other) {
1968        return null;
1969    }
1970
1971    /**
1972     * Equivalent to the standard Python __isub__ method
1973     * @param other the object to perform this binary operation with
1974     * (the right-hand operand).
1975     * @return the result of the sub, or null if this operation
1976     * is not defined
1977     **/

1978    public PyObject __isub__(PyObject other) {
1979        return _sub(other);
1980    }
1981
1982    /**
1983     * Implements the Python expression <code>this - other</code>
1984     * @param o2 the object to perform this binary operation with.
1985     * @return the result of the sub.
1986     * @exception Py.TypeError if this operation can't be performed
1987     * with these operands.
1988     **/

1989    public final PyObject _sub(PyObject o2) {
1990        PyObject x = __sub__(o2);
1991        if (x != null)
1992            return x;
1993        x = o2.__rsub__(this);
1994        if (x != null)
1995            return x;
1996        throw Py.TypeError(_unsupportedop("-", o2));
1997    }
1998
1999    /**
2000     * Equivalent to the standard Python __mul__ method
2001     * @param other the object to perform this binary operation with
2002     * (the right-hand operand).
2003     * @return the result of the mul, or null if this operation
2004     * is not defined
2005     **/

2006    public PyObject __mul__(PyObject other) {
2007        return null;
2008    }
2009
2010    /**
2011     * Equivalent to the standard Python __rmul__ method
2012     * @param other the object to perform this binary operation with
2013     * (the left-hand operand).
2014     * @return the result of the mul, or null if this operation
2015     * is not defined.
2016     **/

2017    public PyObject __rmul__(PyObject other) {
2018        return null;
2019    }
2020
2021    /**
2022     * Equivalent to the standard Python __imul__ method
2023     * @param other the object to perform this binary operation with
2024     * (the right-hand operand).
2025     * @return the result of the mul, or null if this operation
2026     * is not defined
2027     **/

2028    public PyObject __imul__(PyObject other) {
2029        return _mul(other);
2030    }
2031
2032    /**
2033     * Implements the Python expression <code>this * other</code>
2034     * @param o2 the object to perform this binary operation with.
2035     * @return the result of the mul.
2036     * @exception Py.TypeError if this operation can't be performed
2037     * with these operands.
2038     **/

2039    public final PyObject _mul(PyObject o2) {
2040        PyObject x = __mul__(o2);
2041        if (x != null)
2042            return x;
2043        x = o2.__rmul__(this);
2044        if (x != null)
2045            return x;
2046        throw Py.TypeError(_unsupportedop("*", o2));
2047    }
2048
2049    /**
2050     * Equivalent to the standard Python __div__ method
2051     * @param other the object to perform this binary operation with
2052     * (the right-hand operand).
2053     * @return the result of the div, or null if this operation
2054     * is not defined
2055     **/

2056    public PyObject __div__(PyObject other) {
2057        return null;
2058    }
2059
2060    /**
2061     * Equivalent to the standard Python __rdiv__ method
2062     * @param other the object to perform this binary operation with
2063     * (the left-hand operand).
2064     * @return the result of the div, or null if this operation
2065     * is not defined.
2066     **/

2067    public PyObject __rdiv__(PyObject other) {
2068        return null;
2069    }
2070
2071    /**
2072     * Equivalent to the standard Python __idiv__ method
2073     * @param other the object to perform this binary operation with
2074     * (the right-hand operand).
2075     * @return the result of the div, or null if this operation
2076     * is not defined
2077     **/

2078    public PyObject __idiv__(PyObject other) {
2079        return _div(other);
2080    }
2081
2082    /**
2083     * Implements the Python expression <code>this / other</code>
2084     * @param o2 the object to perform this binary operation with.
2085     * @return the result of the div.
2086     * @exception Py.TypeError if this operation can't be performed
2087     * with these operands.
2088     **/

2089    public final PyObject _div(PyObject o2) {
2090        if (Options.Qnew)
2091            return _truediv(o2);
2092        PyObject x = __div__(o2);
2093        if (x != null)
2094            return x;
2095        x = o2.__rdiv__(this);
2096        if (x != null)
2097            return x;
2098        throw Py.TypeError(_unsupportedop("/", o2));
2099    }
2100
2101    /**
2102     * Equivalent to the standard Python __floordiv__ method
2103     * @param other the object to perform this binary operation with
2104     * (the right-hand operand).
2105     * @return the result of the floordiv, or null if this operation
2106     * is not defined
2107     **/

2108    public PyObject __floordiv__(PyObject other) {
2109        return null;
2110    }
2111
2112    /**
2113     * Equivalent to the standard Python __rfloordiv__ method
2114     * @param other the object to perform this binary operation with
2115     * (the left-hand operand).
2116     * @return the result of the floordiv, or null if this operation
2117     * is not defined.
2118     **/

2119    public PyObject __rfloordiv__(PyObject other) {
2120        return null;
2121    }
2122
2123    /**
2124     * Equivalent to the standard Python __ifloordiv__ method
2125     * @param other the object to perform this binary operation with
2126     * (the right-hand operand).
2127     * @return the result of the floordiv, or null if this operation
2128     * is not defined
2129     **/

2130    public PyObject __ifloordiv__(PyObject other) {
2131        return _floordiv(other);
2132    }
2133
2134    /**
2135     * Implements the Python expression <code>this // other</code>
2136     * @param o2 the object to perform this binary operation with.
2137     * @return the result of the floordiv.
2138     * @exception Py.TypeError if this operation can't be performed
2139     * with these operands.
2140     **/

2141    public final PyObject _floordiv(PyObject o2) {
2142        PyObject x = __floordiv__(o2);
2143        if (x != null)
2144            return x;
2145        x = o2.__rfloordiv__(this);
2146        if (x != null)
2147            return x;
2148        throw Py.TypeError(_unsupportedop("//", o2));
2149    }
2150
2151    /**
2152     * Equivalent to the standard Python __truediv__ method
2153     * @param other the object to perform this binary operation with
2154     * (the right-hand operand).
2155     * @return the result of the truediv, or null if this operation
2156     * is not defined
2157     **/

2158    public PyObject __truediv__(PyObject other) {
2159        return null;
2160    }
2161
2162    /**
2163     * Equivalent to the standard Python __rtruediv__ method
2164     * @param other the object to perform this binary operation with
2165     * (the left-hand operand).
2166     * @return the result of the truediv, or null if this operation
2167     * is not defined.
2168     **/

2169    public PyObject __rtruediv__(PyObject other) {
2170        return null;
2171    }
2172
2173    /**
2174     * Equivalent to the standard Python __itruediv__ method
2175     * @param other the object to perform this binary operation with
2176     * (the right-hand operand).
2177     * @return the result of the truediv, or null if this operation
2178     * is not defined
2179     **/

2180    public PyObject __itruediv__(PyObject other) {
2181        return _truediv(other);
2182    }
2183
2184    /**
2185     * Implements the Python expression <code>this / other</code>
2186     * @param o2 the object to perform this binary operation with.
2187     * @return the result of the truediv.
2188     * @exception Py.TypeError if this operation can't be performed
2189     * with these operands.
2190     **/

2191    public final PyObject _truediv(PyObject o2) {
2192        PyObject x = __truediv__(o2);
2193        if (x != null)
2194            return x;
2195        x = o2.__rtruediv__(this);
2196        if (x != null)
2197            return x;
2198        throw Py.TypeError(_unsupportedop("/", o2));
2199    }
2200
2201    /**
2202     * Equivalent to the standard Python __mod__ method
2203     * @param other the object to perform this binary operation with
2204     * (the right-hand operand).
2205     * @return the result of the mod, or null if this operation
2206     * is not defined
2207     **/

2208    public PyObject __mod__(PyObject other) {
2209        return null;
2210    }
2211
2212    /**
2213     * Equivalent to the standard Python __rmod__ method
2214     * @param other the object to perform this binary operation with
2215     * (the left-hand operand).
2216     * @return the result of the mod, or null if this operation
2217     * is not defined.
2218     **/

2219    public PyObject __rmod__(PyObject other) {
2220        return null;
2221    }
2222
2223    /**
2224     * Equivalent to the standard Python __imod__ method
2225     * @param other the object to perform this binary operation with
2226     * (the right-hand operand).
2227     * @return the result of the mod, or null if this operation
2228     * is not defined
2229     **/

2230    public PyObject __imod__(PyObject other) {
2231        return _mod(other);
2232    }
2233
2234    /**
2235     * Implements the Python expression <code>this % other</code>
2236     * @param o2 the object to perform this binary operation with.
2237     * @return the result of the mod.
2238     * @exception Py.TypeError if this operation can't be performed
2239     * with these operands.
2240     **/

2241    public final PyObject _mod(PyObject o2) {
2242        PyObject x = __mod__(o2);
2243        if (x != null)
2244            return x;
2245        x = o2.__rmod__(this);
2246        if (x != null)
2247            return x;
2248        throw Py.TypeError(_unsupportedop("%", o2));
2249    }
2250
2251    /**
2252     * Equivalent to the standard Python __divmod__ method
2253     * @param other the object to perform this binary operation with
2254     * (the right-hand operand).
2255     * @return the result of the divmod, or null if this operation
2256     * is not defined
2257     **/

2258    public PyObject __divmod__(PyObject other) {
2259        return null;
2260    }
2261
2262    /**
2263     * Equivalent to the standard Python __rdivmod__ method
2264     * @param other the object to perform this binary operation with
2265     * (the left-hand operand).
2266     * @return the result of the divmod, or null if this operation
2267     * is not defined.
2268     **/

2269    public PyObject __rdivmod__(PyObject other) {
2270        return null;
2271    }
2272
2273    /**
2274     * Equivalent to the standard Python __idivmod__ method
2275     * @param other the object to perform this binary operation with
2276     * (the right-hand operand).
2277     * @return the result of the divmod, or null if this operation
2278     * is not defined
2279     **/

2280    public PyObject __idivmod__(PyObject other) {
2281        return _divmod(other);
2282    }
2283
2284    /**
2285     * Implements the Python expression <code>this divmod other</code>
2286     * @param o2 the object to perform this binary operation with.
2287     * @return the result of the divmod.
2288     * @exception Py.TypeError if this operation can't be performed
2289     * with these operands.
2290     **/

2291    public final PyObject _divmod(PyObject o2) {
2292        PyObject x = __divmod__(o2);
2293        if (x != null)
2294            return x;
2295        x = o2.__rdivmod__(this);
2296        if (x != null)
2297            return x;
2298        //FIXME:
2299
throw Py.TypeError(_unsupportedop("divmod", o2));
2300    }
2301
2302    /**
2303     * Equivalent to the standard Python __pow__ method
2304     * @param other the object to perform this binary operation with
2305     * (the right-hand operand).
2306     * @return the result of the pow, or null if this operation
2307     * is not defined
2308     **/

2309    public PyObject __pow__(PyObject other) {
2310        return __pow__(other, null);
2311    }
2312
2313    /**
2314     * Equivalent to the standard Python __rpow__ method
2315     * @param other the object to perform this binary operation with
2316     * (the left-hand operand).
2317     * @return the result of the pow, or null if this operation
2318     * is not defined.
2319     **/

2320    public PyObject __rpow__(PyObject other) {
2321        return null;
2322    }
2323
2324    /**
2325     * Equivalent to the standard Python __ipow__ method
2326     * @param other the object to perform this binary operation with
2327     * (the right-hand operand).
2328     * @return the result of the pow, or null if this operation
2329     * is not defined
2330     **/

2331    public PyObject __ipow__(PyObject other) {
2332        return _pow(other);
2333    }
2334
2335    /**
2336     * Implements the Python expression <code>this ** other</code>
2337     * @param o2 the object to perform this binary operation with.
2338     * @return the result of the pow.
2339     * @exception Py.TypeError if this operation can't be performed
2340     * with these operands.
2341     **/

2342    public final PyObject _pow(PyObject o2) {
2343        PyObject x = __pow__(o2);
2344        if (x != null)
2345            return x;
2346        x = o2.__rpow__(this);
2347        if (x != null)
2348            return x;
2349        throw Py.TypeError(_unsupportedop("**", o2));
2350    }
2351
2352    /**
2353     * Equivalent to the standard Python __lshift__ method
2354     * @param other the object to perform this binary operation with
2355     * (the right-hand operand).
2356     * @return the result of the lshift, or null if this operation
2357     * is not defined
2358     **/

2359    public PyObject __lshift__(PyObject other) {
2360        return null;
2361    }
2362
2363    /**
2364     * Equivalent to the standard Python __rlshift__ method
2365     * @param other the object to perform this binary operation with
2366     * (the left-hand operand).
2367     * @return the result of the lshift, or null if this operation
2368     * is not defined.
2369     **/

2370    public PyObject __rlshift__(PyObject other) {
2371        return null;
2372    }
2373
2374    /**
2375     * Equivalent to the standard Python __ilshift__ method
2376     * @param other the object to perform this binary operation with
2377     * (the right-hand operand).
2378     * @return the result of the lshift, or null if this operation
2379     * is not defined
2380     **/

2381    public PyObject __ilshift__(PyObject other) {
2382        return _lshift(other);
2383    }
2384
2385    /**
2386     * Implements the Python expression <code>this << other</code>
2387     * @param o2 the object to perform this binary operation with.
2388     * @return the result of the lshift.
2389     * @exception Py.TypeError if this operation can't be performed
2390     * with these operands.
2391     **/

2392    public final PyObject _lshift(PyObject o2) {
2393        PyObject x = __lshift__(o2);
2394        if (x != null)
2395            return x;
2396        x = o2.__rlshift__(this);
2397        if (x != null)
2398            return x;
2399        throw Py.TypeError(_unsupportedop("<<", o2));
2400    }
2401
2402    /**
2403     * Equivalent to the standard Python __rshift__ method
2404     * @param other the object to perform this binary operation with
2405     * (the right-hand operand).
2406     * @return the result of the rshift, or null if this operation
2407     * is not defined
2408     **/

2409    public PyObject __rshift__(PyObject other) {
2410        return null;
2411    }
2412
2413    /**
2414     * Equivalent to the standard Python __rrshift__ method
2415     * @param other the object to perform this binary operation with
2416     * (the left-hand operand).
2417     * @return the result of the rshift, or null if this operation
2418     * is not defined.
2419     **/

2420    public PyObject __rrshift__(PyObject other) {
2421        return null;
2422    }
2423
2424    /**
2425     * Equivalent to the standard Python __irshift__ method
2426     * @param other the object to perform this binary operation with
2427     * (the right-hand operand).
2428     * @return the result of the rshift, or null if this operation
2429     * is not defined
2430     **/

2431    public PyObject __irshift__(PyObject other) {
2432        return _rshift(other);
2433    }
2434
2435    /**
2436     * Implements the Python expression <code>this >> other</code>
2437     * @param o2 the object to perform this binary operation with.
2438     * @return the result of the rshift.
2439     * @exception Py.TypeError if this operation can't be performed
2440     * with these operands.
2441     **/

2442    public final PyObject _rshift(PyObject o2) {
2443        PyObject x = __rshift__(o2);
2444        if (x != null)
2445            return x;
2446        x = o2.__rrshift__(this);
2447        if (x != null)
2448            return x;
2449        throw Py.TypeError(_unsupportedop(">>", o2));
2450    }
2451
2452    /**
2453     * Equivalent to the standard Python __and__ method
2454     * @param other the object to perform this binary operation with
2455     * (the right-hand operand).
2456     * @return the result of the and, or null if this operation
2457     * is not defined
2458     **/

2459    public PyObject __and__(PyObject other) {
2460        return null;
2461    }
2462
2463    /**
2464     * Equivalent to the standard Python __rand__ method
2465     * @param other the object to perform this binary operation with
2466     * (the left-hand operand).
2467     * @return the result of the and, or null if this operation
2468     * is not defined.
2469     **/

2470    public PyObject __rand__(PyObject other) {
2471        return null;
2472    }
2473
2474    /**
2475     * Equivalent to the standard Python __iand__ method
2476     * @param other the object to perform this binary operation with
2477     * (the right-hand operand).
2478     * @return the result of the and, or null if this operation
2479     * is not defined
2480     **/

2481    public PyObject __iand__(PyObject other) {
2482        return _and(other);
2483    }
2484
2485    /**
2486     * Implements the Python expression <code>this & other</code>
2487     * @param o2 the object to perform this binary operation with.
2488     * @return the result of the and.
2489     * @exception Py.TypeError if this operation can't be performed
2490     * with these operands.
2491     **/

2492    public final PyObject _and(PyObject o2) {
2493        PyObject x = __and__(o2);
2494        if (x != null)
2495            return x;
2496        x = o2.__rand__(this);
2497        if (x != null)
2498            return x;
2499        throw Py.TypeError(_unsupportedop("&", o2));
2500    }
2501
2502    /**
2503     * Equivalent to the standard Python __or__ method
2504     * @param other the object to perform this binary operation with
2505     * (the right-hand operand).
2506     * @return the result of the or, or null if this operation
2507     * is not defined
2508     **/

2509    public PyObject __or__(PyObject other) {
2510        return null;
2511    }
2512
2513    /**
2514     * Equivalent to the standard Python __ror__ method
2515     * @param other the object to perform this binary operation with
2516     * (the left-hand operand).
2517     * @return the result of the or, or null if this operation
2518     * is not defined.
2519     **/

2520    public PyObject __ror__(PyObject other) {
2521        return null;
2522    }
2523
2524    /**
2525     * Equivalent to the standard Python __ior__ method
2526     * @param other the object to perform this binary operation with
2527     * (the right-hand operand).
2528     * @return the result of the or, or null if this operation
2529     * is not defined
2530     **/

2531    public PyObject __ior__(PyObject other) {
2532        return _or(other);
2533    }
2534
2535    /**
2536     * Implements the Python expression <code>this | other</code>
2537     * @param o2 the object to perform this binary operation with.
2538     * @return the result of the or.
2539     * @exception Py.TypeError if this operation can't be performed
2540     * with these operands.
2541     **/

2542    public final PyObject _or(PyObject o2) {
2543        PyObject x = __or__(o2);
2544        if (x != null)
2545            return x;
2546        x = o2.__ror__(this);
2547        if (x != null)
2548            return x;
2549        throw Py.TypeError(_unsupportedop("|", o2));
2550    }
2551
2552    /**
2553     * Equivalent to the standard Python __xor__ method
2554     * @param other the object to perform this binary operation with
2555     * (the right-hand operand).
2556     * @return the result of the xor, or null if this operation
2557     * is not defined
2558     **/

2559    public PyObject __xor__(PyObject other) {
2560        return null;
2561    }
2562
2563    /**
2564     * Equivalent to the standard Python __rxor__ method
2565     * @param other the object to perform this binary operation with
2566     * (the left-hand operand).
2567     * @return the result of the xor, or null if this operation
2568     * is not defined.
2569     **/

2570    public PyObject __rxor__(PyObject other) {
2571        return null;
2572    }
2573
2574    /**
2575     * Equivalent to the standard Python __ixor__ method
2576     * @param other the object to perform this binary operation with
2577     * (the right-hand operand).
2578     * @return the result of the xor, or null if this operation
2579     * is not defined
2580     **/

2581    public PyObject __ixor__(PyObject other) {
2582        return _xor(other);
2583    }
2584
2585    /**
2586     * Implements the Python expression <code>this ^ other</code>
2587     * @param o2 the object to perform this binary operation with.
2588     * @return the result of the xor.
2589     * @exception Py.TypeError if this operation can't be performed
2590     * with these operands.
2591     **/

2592    public final PyObject _xor(PyObject o2) {
2593        PyObject x = __xor__(o2);
2594        if (x != null)
2595            return x;
2596        x = o2.__rxor__(this);
2597        if (x != null)
2598            return x;
2599        throw Py.TypeError(_unsupportedop("^", o2));
2600    }
2601
2602    // Generated by make_binops.py (End)
2603

2604    /* A convenience function for PyProxy's */
2605    // Possibly add _jcall(), _jcall(Object, ...) as future optimization
2606
/**
2607     * A convenience function for PyProxy's.
2608     * @param args call arguments.
2609     * @exception Throwable
2610     */

2611    public PyObject _jcallexc(Object JavaDoc[] args) throws Throwable JavaDoc {
2612        PyObject[] pargs = new PyObject[args.length];
2613        try {
2614            int n = args.length;
2615            for (int i = 0; i < n; i++)
2616                pargs[i] = Py.java2py(args[i]);
2617            return __call__(pargs);
2618        } catch (PyException e) {
2619            if (e.value instanceof PyJavaInstance) {
2620                Object JavaDoc t = e.value.__tojava__(Throwable JavaDoc.class);
2621                if (t != null && t != Py.NoConversion) {
2622                    throw (Throwable JavaDoc) t;
2623                }
2624            } else {
2625                ThreadState ts = Py.getThreadState();
2626                if (ts.frame == null) {
2627                    Py.maybeSystemExit(e);
2628                }
2629                if (Options.showPythonProxyExceptions) {
2630                    Py.stderr.println(
2631                        "Exception in Python proxy returning to Java:");
2632                    Py.printException(e);
2633                }
2634            }
2635            throw e;
2636        }
2637    }
2638
2639    public void _jthrow(Throwable JavaDoc t) {
2640        if (t instanceof RuntimeException JavaDoc)
2641            throw (RuntimeException JavaDoc) t;
2642        if (t instanceof Error JavaDoc)
2643            throw (Error JavaDoc) t;
2644        throw Py.JavaError(t);
2645    }
2646
2647    public PyObject _jcall(Object JavaDoc[] args) {
2648        try {
2649            return _jcallexc(args);
2650        } catch (Throwable JavaDoc t) {
2651            _jthrow(t);
2652            return null;
2653        }
2654    }
2655
2656    /* Shortcut methods for calling methods from Java */
2657
2658    /**
2659     * Shortcut for calling a method on a PyObject from Java.
2660     * This form is equivalent to o.__getattr__(name).__call__(args, keywords)
2661     *
2662     * @param name the name of the method to call. This must be an
2663     * interned string!
2664     * @param args an array of the arguments to the call.
2665     * @param keywords the keywords to use in the call.
2666     * @return the result of calling the method name with args and keywords.
2667     **/

2668    public PyObject invoke(String JavaDoc name, PyObject[] args, String JavaDoc[] keywords) {
2669        PyObject f = __getattr__(name);
2670        return f.__call__(args, keywords);
2671    }
2672
2673    public PyObject invoke(String JavaDoc name, PyObject[] args) {
2674        PyObject f = __getattr__(name);
2675        return f.__call__(args);
2676    }
2677
2678    /**
2679     * Shortcut for calling a method on a PyObject with no args.
2680     *
2681     * @param name the name of the method to call. This must be an
2682     * interned string!
2683     * @return the result of calling the method name with no args
2684     **/

2685    public PyObject invoke(String JavaDoc name) {
2686        PyObject f = __getattr__(name);
2687        return f.__call__();
2688    }
2689
2690    /**
2691     * Shortcut for calling a method on a PyObject with one arg.
2692     *
2693     * @param name the name of the method to call. This must be an
2694     * interned string!
2695     * @param arg1 the one argument of the method.
2696     * @return the result of calling the method name with arg1
2697     **/

2698    public PyObject invoke(String JavaDoc name, PyObject arg1) {
2699        PyObject f = __getattr__(name);
2700        return f.__call__(arg1);
2701    }
2702
2703    /**
2704     * Shortcut for calling a method on a PyObject with two args.
2705     *
2706     * @param name the name of the method to call. This must be an
2707     * interned string!
2708     * @param arg1 the first argument of the method.
2709     * @param arg2 the second argument of the method.
2710     * @return the result of calling the method name with arg1 and arg2
2711     **/

2712    public PyObject invoke(String JavaDoc name, PyObject arg1, PyObject arg2) {
2713        PyObject f = __getattr__(name);
2714        return f.__call__(arg1, arg2);
2715    }
2716
2717    /* descriptors and lookup protocols */
2718
2719    /** xxx implements where meaningful
2720     * @return internal object per instance dict or null
2721     */

2722    public PyObject fastGetDict() {
2723        return null;
2724    }
2725
2726    /** xxx implements where meaningful
2727     * @return internal object __dict__ or null
2728     */

2729    public PyObject getDict() {
2730        return null;
2731    }
2732
2733
2734    public boolean implementsDescrSet() {
2735        return objtype.has_set;
2736    }
2737
2738    public boolean implementsDescrDelete() {
2739        return objtype.has_delete;
2740    }
2741
2742    public boolean isDataDescr() { // implements either __set__ or __delete__
2743
return objtype.has_set || objtype.has_delete;
2744    }
2745
2746    // doc & xxx ok this way?
2747
// can return null meaning set-only or throw exception
2748

2749    // backward comp impls.
2750
public PyObject __get__(PyObject obj, PyObject type) {
2751        return _doget(obj, type);
2752    }
2753
2754    public void __set__(PyObject obj, PyObject value) {
2755        throw Py.RuntimeError("object internal __set__ impl is abstract");
2756    }
2757
2758    public void __delete__(PyObject obj) {
2759        throw Py.RuntimeError("object internal __delete__ impl is abstract");
2760    }
2761
2762    // name must be interned
2763
final PyObject object___findattr__(String JavaDoc name) {
2764
2765        PyObject descr = objtype.lookup(name);
2766        PyObject res;
2767
2768        if (descr != null) {
2769            if (descr.isDataDescr()) {
2770                res = descr.__get__(this, objtype);
2771                if (res != null)
2772                    return res;
2773            }
2774        }
2775
2776        PyObject obj_dict = fastGetDict();
2777        if (obj_dict != null) {
2778            res = obj_dict.__finditem__(name);
2779            if (res != null)
2780                return res;
2781        }
2782
2783        if (descr != null) {
2784            return descr.__get__(this, objtype);
2785        }
2786
2787        return null;
2788    }
2789
2790    final void object___setattr__(String JavaDoc name, PyObject value) {
2791        PyObject descr = objtype.lookup(name);
2792
2793        boolean set = false;
2794
2795        if (descr != null) {
2796            set = descr.implementsDescrSet();
2797            if (set && descr.isDataDescr()) {
2798                descr.__set__(this, value);
2799                return;
2800            }
2801        }
2802
2803        PyObject obj_dict = fastGetDict();
2804        if (obj_dict != null) {
2805            obj_dict.__setitem__(name, value);
2806            return;
2807        }
2808
2809        if (set) {
2810            descr.__set__(this, value);
2811        }
2812
2813        if (descr != null)
2814            readonlyAttributeError(name);
2815
2816        noAttributeError(name);
2817    }
2818
2819    final void object___delattr__(String JavaDoc name) {
2820        PyObject descr = objtype.lookup(name);
2821
2822        boolean delete = false;
2823
2824        if (descr != null) {
2825            delete = descr.implementsDescrDelete();
2826            if (delete && descr.isDataDescr()) {
2827                descr.__delete__(this);
2828                return;
2829            }
2830        }
2831
2832        PyObject obj_dict = fastGetDict();
2833        if (obj_dict != null) {
2834            try {
2835                obj_dict.__delitem__(name);
2836            } catch (PyException exc) {
2837                if (Py.matchException(exc, Py.KeyError))
2838                    noAttributeError(name);
2839                else
2840                    throw exc;
2841            }
2842            return;
2843        }
2844
2845        if (delete) {
2846            descr.__delete__(this);
2847        }
2848
2849        if (descr != null)
2850            readonlyAttributeError(name);
2851
2852        noAttributeError(name);
2853    }
2854
2855    /* arguments' conversion helpers */
2856
2857    public static class ConversionException extends Exception JavaDoc {
2858
2859        public int index;
2860
2861        public ConversionException(int index) {
2862            this.index = index;
2863        }
2864
2865    }
2866
2867    public String JavaDoc asString(int index) throws ConversionException {
2868        throw new ConversionException(index);
2869    }
2870
2871    public String JavaDoc asStringOrNull(int index) throws ConversionException {
2872       return asString(index);
2873    }
2874
2875    public String JavaDoc asName(int index) throws ConversionException {
2876        throw new ConversionException(index);
2877    }
2878
2879    public int asInt(int index) throws ConversionException {
2880        throw new ConversionException(index);
2881    }
2882
2883    public long asLong(int index) throws ConversionException {
2884        throw new ConversionException(index);
2885    }
2886
2887}
2888
2889/*
2890 * A very specialized tuple-like class used when detecting cycles during
2891 * object comparisons. This classes is different from an normal tuple
2892 * by hashing and comparing its elements by identity.
2893 */

2894
2895class PyIdentityTuple extends PyObject {
2896
2897    PyObject[] list;
2898
2899    public PyIdentityTuple(PyObject elements[]) {
2900        list = elements;
2901    }
2902
2903    public int hashCode() {
2904        int x, y;
2905        int len = list.length;
2906        x = 0x345678;
2907
2908        for (len--; len >= 0; len--) {
2909            y = System.identityHashCode(list[len]);
2910            x = (x + x + x) ^ y;
2911        }
2912        x ^= list.length;
2913        return x;
2914    }
2915
2916    public boolean equals(Object JavaDoc o) {
2917        if (!(o instanceof PyIdentityTuple))
2918            return false;
2919        PyIdentityTuple that = (PyIdentityTuple) o;
2920        if (list.length != that.list.length)
2921            return false;
2922        for (int i = 0; i < list.length; i++) {
2923            if (list[i] != that.list[i])
2924                return false;
2925        }
2926        return true;
2927    }
2928
2929}
2930
Popular Tags