0001: // Copyright (c) Corporation for National Research Initiatives
0002: package org.python.core;
0003:
0004: import java.util.Hashtable;
0005: import java.util.Enumeration;
0006:
0007: /**
0008: * A builtin python dictionary.
0009: */
0010:
0011: public class PyDictionary extends PyObject {
0012:
0013: //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py
0014: /* type info */
0015:
0016: public static final String exposed_name = "dict";
0017:
0018: public static void typeSetup(PyObject dict, PyType.Newstyle marker) {
0019: class exposed___ne__ extends PyBuiltinMethodNarrow {
0020:
0021: exposed___ne__(PyObject self, PyBuiltinFunction.Info info) {
0022: super (self, info);
0023: }
0024:
0025: public PyBuiltinFunction bind(PyObject self) {
0026: return new exposed___ne__(self, info);
0027: }
0028:
0029: public PyObject __call__(PyObject arg0) {
0030: PyObject ret = ((PyDictionary) self).dict___ne__(arg0);
0031: if (ret == null)
0032: return Py.NotImplemented;
0033: return ret;
0034: }
0035:
0036: }
0037: dict.__setitem__("__ne__", new PyMethodDescr("__ne__",
0038: PyDictionary.class, 1, 1,
0039: new exposed___ne__(null, null)));
0040: class exposed___eq__ extends PyBuiltinMethodNarrow {
0041:
0042: exposed___eq__(PyObject self, PyBuiltinFunction.Info info) {
0043: super (self, info);
0044: }
0045:
0046: public PyBuiltinFunction bind(PyObject self) {
0047: return new exposed___eq__(self, info);
0048: }
0049:
0050: public PyObject __call__(PyObject arg0) {
0051: PyObject ret = ((PyDictionary) self).dict___eq__(arg0);
0052: if (ret == null)
0053: return Py.NotImplemented;
0054: return ret;
0055: }
0056:
0057: }
0058: dict.__setitem__("__eq__", new PyMethodDescr("__eq__",
0059: PyDictionary.class, 1, 1,
0060: new exposed___eq__(null, null)));
0061: class exposed___lt__ extends PyBuiltinMethodNarrow {
0062:
0063: exposed___lt__(PyObject self, PyBuiltinFunction.Info info) {
0064: super (self, info);
0065: }
0066:
0067: public PyBuiltinFunction bind(PyObject self) {
0068: return new exposed___lt__(self, info);
0069: }
0070:
0071: public PyObject __call__(PyObject arg0) {
0072: PyObject ret = ((PyDictionary) self).dict___lt__(arg0);
0073: if (ret == null)
0074: return Py.NotImplemented;
0075: return ret;
0076: }
0077:
0078: }
0079: dict.__setitem__("__lt__", new PyMethodDescr("__lt__",
0080: PyDictionary.class, 1, 1,
0081: new exposed___lt__(null, null)));
0082: class exposed___gt__ extends PyBuiltinMethodNarrow {
0083:
0084: exposed___gt__(PyObject self, PyBuiltinFunction.Info info) {
0085: super (self, info);
0086: }
0087:
0088: public PyBuiltinFunction bind(PyObject self) {
0089: return new exposed___gt__(self, info);
0090: }
0091:
0092: public PyObject __call__(PyObject arg0) {
0093: PyObject ret = ((PyDictionary) self).dict___gt__(arg0);
0094: if (ret == null)
0095: return Py.NotImplemented;
0096: return ret;
0097: }
0098:
0099: }
0100: dict.__setitem__("__gt__", new PyMethodDescr("__gt__",
0101: PyDictionary.class, 1, 1,
0102: new exposed___gt__(null, null)));
0103: class exposed___ge__ extends PyBuiltinMethodNarrow {
0104:
0105: exposed___ge__(PyObject self, PyBuiltinFunction.Info info) {
0106: super (self, info);
0107: }
0108:
0109: public PyBuiltinFunction bind(PyObject self) {
0110: return new exposed___ge__(self, info);
0111: }
0112:
0113: public PyObject __call__(PyObject arg0) {
0114: PyObject ret = ((PyDictionary) self).dict___ge__(arg0);
0115: if (ret == null)
0116: return Py.NotImplemented;
0117: return ret;
0118: }
0119:
0120: }
0121: dict.__setitem__("__ge__", new PyMethodDescr("__ge__",
0122: PyDictionary.class, 1, 1,
0123: new exposed___ge__(null, null)));
0124: class exposed___le__ extends PyBuiltinMethodNarrow {
0125:
0126: exposed___le__(PyObject self, PyBuiltinFunction.Info info) {
0127: super (self, info);
0128: }
0129:
0130: public PyBuiltinFunction bind(PyObject self) {
0131: return new exposed___le__(self, info);
0132: }
0133:
0134: public PyObject __call__(PyObject arg0) {
0135: PyObject ret = ((PyDictionary) self).dict___le__(arg0);
0136: if (ret == null)
0137: return Py.NotImplemented;
0138: return ret;
0139: }
0140:
0141: }
0142: dict.__setitem__("__le__", new PyMethodDescr("__le__",
0143: PyDictionary.class, 1, 1,
0144: new exposed___le__(null, null)));
0145: class exposed___cmp__ extends PyBuiltinMethodNarrow {
0146:
0147: exposed___cmp__(PyObject self, PyBuiltinFunction.Info info) {
0148: super (self, info);
0149: }
0150:
0151: public PyBuiltinFunction bind(PyObject self) {
0152: return new exposed___cmp__(self, info);
0153: }
0154:
0155: public PyObject __call__(PyObject arg0) {
0156: int ret = ((PyDictionary) self).dict___cmp__(arg0);
0157: if (ret == -2) {
0158: throw Py.TypeError("dict"
0159: + ".__cmp__(x,y) requires y to be '"
0160: + "dict" + "', not a '"
0161: + (arg0).getType().fastGetName() + "'");
0162: }
0163: return Py.newInteger(ret);
0164: }
0165:
0166: }
0167: dict.__setitem__("__cmp__", new PyMethodDescr("__cmp__",
0168: PyDictionary.class, 1, 1, new exposed___cmp__(null,
0169: null)));
0170: class exposed___getitem__ extends PyBuiltinMethodNarrow {
0171:
0172: exposed___getitem__(PyObject self,
0173: PyBuiltinFunction.Info info) {
0174: super (self, info);
0175: }
0176:
0177: public PyBuiltinFunction bind(PyObject self) {
0178: return new exposed___getitem__(self, info);
0179: }
0180:
0181: public PyObject __call__(PyObject arg0) {
0182: PyObject ret = ((PyDictionary) self)
0183: .dict___finditem__(arg0);
0184: if (ret == null)
0185: throw Py.KeyError(arg0.toString());
0186: return ret;
0187: }
0188:
0189: }
0190: dict.__setitem__("__getitem__", new PyMethodDescr(
0191: "__getitem__", PyDictionary.class, 1, 1,
0192: new exposed___getitem__(null, null)));
0193: class exposed_fromkeys extends PyBuiltinMethodNarrow {
0194:
0195: exposed_fromkeys(PyObject self, PyBuiltinFunction.Info info) {
0196: super (self, info);
0197: }
0198:
0199: public PyBuiltinFunction bind(PyObject self) {
0200: return new exposed_fromkeys(self, info);
0201: }
0202:
0203: public PyObject __call__(PyObject arg0, PyObject arg1) {
0204: return dict_fromkeys((PyType) getSelf(), arg0, arg1);
0205: }
0206:
0207: public PyObject __call__(PyObject arg0) {
0208: return dict_fromkeys((PyType) getSelf(), arg0);
0209: }
0210:
0211: }
0212: dict.__setitem__("fromkeys", new PyClassMethodDescr("fromkeys",
0213: PyDictionary.class, 1, 2, new exposed_fromkeys(null,
0214: null)));
0215: class exposed_get extends PyBuiltinMethodNarrow {
0216:
0217: exposed_get(PyObject self, PyBuiltinFunction.Info info) {
0218: super (self, info);
0219: }
0220:
0221: public PyBuiltinFunction bind(PyObject self) {
0222: return new exposed_get(self, info);
0223: }
0224:
0225: public PyObject __call__(PyObject arg0, PyObject arg1) {
0226: return ((PyDictionary) self).dict_get(arg0, arg1);
0227: }
0228:
0229: public PyObject __call__(PyObject arg0) {
0230: return ((PyDictionary) self).dict_get(arg0);
0231: }
0232:
0233: }
0234: dict.__setitem__("get", new PyMethodDescr("get",
0235: PyDictionary.class, 1, 2, new exposed_get(null, null)));
0236: class exposed_setdefault extends PyBuiltinMethodNarrow {
0237:
0238: exposed_setdefault(PyObject self,
0239: PyBuiltinFunction.Info info) {
0240: super (self, info);
0241: }
0242:
0243: public PyBuiltinFunction bind(PyObject self) {
0244: return new exposed_setdefault(self, info);
0245: }
0246:
0247: public PyObject __call__(PyObject arg0, PyObject arg1) {
0248: return ((PyDictionary) self)
0249: .dict_setdefault(arg0, arg1);
0250: }
0251:
0252: public PyObject __call__(PyObject arg0) {
0253: return ((PyDictionary) self).dict_setdefault(arg0);
0254: }
0255:
0256: }
0257: dict.__setitem__("setdefault", new PyMethodDescr("setdefault",
0258: PyDictionary.class, 1, 2, new exposed_setdefault(null,
0259: null)));
0260: class exposed_pop extends PyBuiltinMethodNarrow {
0261:
0262: exposed_pop(PyObject self, PyBuiltinFunction.Info info) {
0263: super (self, info);
0264: }
0265:
0266: public PyBuiltinFunction bind(PyObject self) {
0267: return new exposed_pop(self, info);
0268: }
0269:
0270: public PyObject __call__(PyObject arg0, PyObject arg1) {
0271: return ((PyDictionary) self).dict_pop(arg0, arg1);
0272: }
0273:
0274: public PyObject __call__(PyObject arg0) {
0275: return ((PyDictionary) self).dict_pop(arg0);
0276: }
0277:
0278: }
0279: dict.__setitem__("pop", new PyMethodDescr("pop",
0280: PyDictionary.class, 1, 2, new exposed_pop(null, null)));
0281: class exposed_popitem extends PyBuiltinMethodNarrow {
0282:
0283: exposed_popitem(PyObject self, PyBuiltinFunction.Info info) {
0284: super (self, info);
0285: }
0286:
0287: public PyBuiltinFunction bind(PyObject self) {
0288: return new exposed_popitem(self, info);
0289: }
0290:
0291: public PyObject __call__() {
0292: return ((PyDictionary) self).dict_popitem();
0293: }
0294:
0295: }
0296: dict.__setitem__("popitem", new PyMethodDescr("popitem",
0297: PyDictionary.class, 0, 0, new exposed_popitem(null,
0298: null)));
0299: class exposed_has_key extends PyBuiltinMethodNarrow {
0300:
0301: exposed_has_key(PyObject self, PyBuiltinFunction.Info info) {
0302: super (self, info);
0303: }
0304:
0305: public PyBuiltinFunction bind(PyObject self) {
0306: return new exposed_has_key(self, info);
0307: }
0308:
0309: public PyObject __call__(PyObject arg0) {
0310: return Py.newBoolean(((PyDictionary) self)
0311: .dict_has_key(arg0));
0312: }
0313:
0314: }
0315: dict.__setitem__("has_key", new PyMethodDescr("has_key",
0316: PyDictionary.class, 1, 1, new exposed_has_key(null,
0317: null)));
0318: class exposed___contains__ extends PyBuiltinMethodNarrow {
0319:
0320: exposed___contains__(PyObject self,
0321: PyBuiltinFunction.Info info) {
0322: super (self, info);
0323: }
0324:
0325: public PyBuiltinFunction bind(PyObject self) {
0326: return new exposed___contains__(self, info);
0327: }
0328:
0329: public PyObject __call__(PyObject arg0) {
0330: return Py.newBoolean(((PyDictionary) self)
0331: .dict___contains__(arg0));
0332: }
0333:
0334: }
0335: dict.__setitem__("__contains__", new PyMethodDescr(
0336: "__contains__", PyDictionary.class, 1, 1,
0337: new exposed___contains__(null, null)));
0338: class exposed___len__ extends PyBuiltinMethodNarrow {
0339:
0340: exposed___len__(PyObject self, PyBuiltinFunction.Info info) {
0341: super (self, info);
0342: }
0343:
0344: public PyBuiltinFunction bind(PyObject self) {
0345: return new exposed___len__(self, info);
0346: }
0347:
0348: public PyObject __call__() {
0349: return Py.newInteger(((PyDictionary) self)
0350: .dict___len__());
0351: }
0352:
0353: }
0354: dict.__setitem__("__len__", new PyMethodDescr("__len__",
0355: PyDictionary.class, 0, 0, new exposed___len__(null,
0356: null)));
0357: class exposed___setitem__ extends PyBuiltinMethodNarrow {
0358:
0359: exposed___setitem__(PyObject self,
0360: PyBuiltinFunction.Info info) {
0361: super (self, info);
0362: }
0363:
0364: public PyBuiltinFunction bind(PyObject self) {
0365: return new exposed___setitem__(self, info);
0366: }
0367:
0368: public PyObject __call__(PyObject arg0, PyObject arg1) {
0369: ((PyDictionary) self).dict___setitem__(arg0, arg1);
0370: return Py.None;
0371: }
0372:
0373: }
0374: dict.__setitem__("__setitem__", new PyMethodDescr(
0375: "__setitem__", PyDictionary.class, 2, 2,
0376: new exposed___setitem__(null, null)));
0377: class exposed___delitem__ extends PyBuiltinMethodNarrow {
0378:
0379: exposed___delitem__(PyObject self,
0380: PyBuiltinFunction.Info info) {
0381: super (self, info);
0382: }
0383:
0384: public PyBuiltinFunction bind(PyObject self) {
0385: return new exposed___delitem__(self, info);
0386: }
0387:
0388: public PyObject __call__(PyObject arg0) {
0389: ((PyDictionary) self).dict___delitem__(arg0);
0390: return Py.None;
0391: }
0392:
0393: }
0394: dict.__setitem__("__delitem__", new PyMethodDescr(
0395: "__delitem__", PyDictionary.class, 1, 1,
0396: new exposed___delitem__(null, null)));
0397: class exposed_keys extends PyBuiltinMethodNarrow {
0398:
0399: exposed_keys(PyObject self, PyBuiltinFunction.Info info) {
0400: super (self, info);
0401: }
0402:
0403: public PyBuiltinFunction bind(PyObject self) {
0404: return new exposed_keys(self, info);
0405: }
0406:
0407: public PyObject __call__() {
0408: return ((PyDictionary) self).dict_keys();
0409: }
0410:
0411: }
0412: dict
0413: .__setitem__("keys", new PyMethodDescr("keys",
0414: PyDictionary.class, 0, 0, new exposed_keys(
0415: null, null)));
0416: class exposed_update extends PyBuiltinMethodNarrow {
0417:
0418: exposed_update(PyObject self, PyBuiltinFunction.Info info) {
0419: super (self, info);
0420: }
0421:
0422: public PyBuiltinFunction bind(PyObject self) {
0423: return new exposed_update(self, info);
0424: }
0425:
0426: public PyObject __call__(PyObject arg0) {
0427: ((PyDictionary) self).dict_update(arg0);
0428: return Py.None;
0429: }
0430:
0431: }
0432: dict.__setitem__("update", new PyMethodDescr("update",
0433: PyDictionary.class, 1, 1,
0434: new exposed_update(null, null)));
0435: class exposed_itervalues extends PyBuiltinMethodNarrow {
0436:
0437: exposed_itervalues(PyObject self,
0438: PyBuiltinFunction.Info info) {
0439: super (self, info);
0440: }
0441:
0442: public PyBuiltinFunction bind(PyObject self) {
0443: return new exposed_itervalues(self, info);
0444: }
0445:
0446: public PyObject __call__() {
0447: return ((PyDictionary) self).dict_itervalues();
0448: }
0449:
0450: }
0451: dict.__setitem__("itervalues", new PyMethodDescr("itervalues",
0452: PyDictionary.class, 0, 0, new exposed_itervalues(null,
0453: null)));
0454: class exposed_iteritems extends PyBuiltinMethodNarrow {
0455:
0456: exposed_iteritems(PyObject self, PyBuiltinFunction.Info info) {
0457: super (self, info);
0458: }
0459:
0460: public PyBuiltinFunction bind(PyObject self) {
0461: return new exposed_iteritems(self, info);
0462: }
0463:
0464: public PyObject __call__() {
0465: return ((PyDictionary) self).dict_iteritems();
0466: }
0467:
0468: }
0469: dict.__setitem__("iteritems", new PyMethodDescr("iteritems",
0470: PyDictionary.class, 0, 0, new exposed_iteritems(null,
0471: null)));
0472: class exposed_iterkeys extends PyBuiltinMethodNarrow {
0473:
0474: exposed_iterkeys(PyObject self, PyBuiltinFunction.Info info) {
0475: super (self, info);
0476: }
0477:
0478: public PyBuiltinFunction bind(PyObject self) {
0479: return new exposed_iterkeys(self, info);
0480: }
0481:
0482: public PyObject __call__() {
0483: return ((PyDictionary) self).dict_iterkeys();
0484: }
0485:
0486: }
0487: dict.__setitem__("iterkeys", new PyMethodDescr("iterkeys",
0488: PyDictionary.class, 0, 0, new exposed_iterkeys(null,
0489: null)));
0490: class exposed_items extends PyBuiltinMethodNarrow {
0491:
0492: exposed_items(PyObject self, PyBuiltinFunction.Info info) {
0493: super (self, info);
0494: }
0495:
0496: public PyBuiltinFunction bind(PyObject self) {
0497: return new exposed_items(self, info);
0498: }
0499:
0500: public PyObject __call__() {
0501: return ((PyDictionary) self).dict_items();
0502: }
0503:
0504: }
0505: dict.__setitem__("items",
0506: new PyMethodDescr("items", PyDictionary.class, 0, 0,
0507: new exposed_items(null, null)));
0508: class exposed_values extends PyBuiltinMethodNarrow {
0509:
0510: exposed_values(PyObject self, PyBuiltinFunction.Info info) {
0511: super (self, info);
0512: }
0513:
0514: public PyBuiltinFunction bind(PyObject self) {
0515: return new exposed_values(self, info);
0516: }
0517:
0518: public PyObject __call__() {
0519: return ((PyDictionary) self).dict_values();
0520: }
0521:
0522: }
0523: dict.__setitem__("values", new PyMethodDescr("values",
0524: PyDictionary.class, 0, 0,
0525: new exposed_values(null, null)));
0526: class exposed_clear extends PyBuiltinMethodNarrow {
0527:
0528: exposed_clear(PyObject self, PyBuiltinFunction.Info info) {
0529: super (self, info);
0530: }
0531:
0532: public PyBuiltinFunction bind(PyObject self) {
0533: return new exposed_clear(self, info);
0534: }
0535:
0536: public PyObject __call__() {
0537: ((PyDictionary) self).dict_clear();
0538: return Py.None;
0539: }
0540:
0541: }
0542: dict.__setitem__("clear",
0543: new PyMethodDescr("clear", PyDictionary.class, 0, 0,
0544: new exposed_clear(null, null)));
0545: class exposed_copy extends PyBuiltinMethodNarrow {
0546:
0547: exposed_copy(PyObject self, PyBuiltinFunction.Info info) {
0548: super (self, info);
0549: }
0550:
0551: public PyBuiltinFunction bind(PyObject self) {
0552: return new exposed_copy(self, info);
0553: }
0554:
0555: public PyObject __call__() {
0556: return ((PyDictionary) self).dict_copy();
0557: }
0558:
0559: }
0560: dict
0561: .__setitem__("copy", new PyMethodDescr("copy",
0562: PyDictionary.class, 0, 0, new exposed_copy(
0563: null, null)));
0564: class exposed___iter__ extends PyBuiltinMethodNarrow {
0565:
0566: exposed___iter__(PyObject self, PyBuiltinFunction.Info info) {
0567: super (self, info);
0568: }
0569:
0570: public PyBuiltinFunction bind(PyObject self) {
0571: return new exposed___iter__(self, info);
0572: }
0573:
0574: public PyObject __call__() {
0575: return ((PyDictionary) self).dict___iter__();
0576: }
0577:
0578: }
0579: dict.__setitem__("__iter__", new PyMethodDescr("__iter__",
0580: PyDictionary.class, 0, 0, new exposed___iter__(null,
0581: null)));
0582: class exposed___hash__ extends PyBuiltinMethodNarrow {
0583:
0584: exposed___hash__(PyObject self, PyBuiltinFunction.Info info) {
0585: super (self, info);
0586: }
0587:
0588: public PyBuiltinFunction bind(PyObject self) {
0589: return new exposed___hash__(self, info);
0590: }
0591:
0592: public PyObject __call__() {
0593: return Py.newInteger(((PyDictionary) self)
0594: .dict_hashCode());
0595: }
0596:
0597: }
0598: dict.__setitem__("__hash__", new PyMethodDescr("__hash__",
0599: PyDictionary.class, 0, 0, new exposed___hash__(null,
0600: null)));
0601: class exposed___repr__ extends PyBuiltinMethodNarrow {
0602:
0603: exposed___repr__(PyObject self, PyBuiltinFunction.Info info) {
0604: super (self, info);
0605: }
0606:
0607: public PyBuiltinFunction bind(PyObject self) {
0608: return new exposed___repr__(self, info);
0609: }
0610:
0611: public PyObject __call__() {
0612: return new PyString(((PyDictionary) self)
0613: .dict_toString());
0614: }
0615:
0616: }
0617: dict.__setitem__("__repr__", new PyMethodDescr("__repr__",
0618: PyDictionary.class, 0, 0, new exposed___repr__(null,
0619: null)));
0620: class exposed___init__ extends PyBuiltinMethod {
0621:
0622: exposed___init__(PyObject self, PyBuiltinFunction.Info info) {
0623: super (self, info);
0624: }
0625:
0626: public PyBuiltinFunction bind(PyObject self) {
0627: return new exposed___init__(self, info);
0628: }
0629:
0630: public PyObject __call__(PyObject[] args) {
0631: return __call__(args, Py.NoKeywords);
0632: }
0633:
0634: public PyObject __call__(PyObject[] args, String[] keywords) {
0635: ((PyDictionary) self).dict_init(args, keywords);
0636: return Py.None;
0637: }
0638:
0639: }
0640: dict.__setitem__("__init__", new PyMethodDescr("__init__",
0641: PyDictionary.class, -1, -1, new exposed___init__(null,
0642: null)));
0643: dict.__setitem__("__new__", new PyNewWrapper(
0644: PyDictionary.class, "__new__", -1, -1) {
0645:
0646: public PyObject new_impl(boolean init, PyType subtype,
0647: PyObject[] args, String[] keywords) {
0648: PyDictionary newobj;
0649: if (for_type == subtype) {
0650: newobj = new PyDictionary();
0651: if (init)
0652: newobj.dict_init(args, keywords);
0653: } else {
0654: newobj = new PyDictionaryDerived(subtype);
0655: }
0656: return newobj;
0657: }
0658:
0659: });
0660: }
0661:
0662: //~ END GENERATED REGION -- DO NOT EDIT SEE gexpose.py
0663:
0664: protected Hashtable table;
0665:
0666: /**
0667: * Create an empty dictionary.
0668: */
0669: public PyDictionary() {
0670: this (new Hashtable());
0671: }
0672:
0673: /**
0674: * For derived types
0675: * @param subtype
0676: */
0677: public PyDictionary(PyType subtype) {
0678: super (subtype);
0679: table = new Hashtable();
0680: }
0681:
0682: /**
0683: * Create an new dictionary which is based on the hashtable.
0684: * @param t the hashtable used. The supplied hashtable is used as
0685: * is and must only contain PyObject key:value pairs.
0686: */
0687: public PyDictionary(Hashtable t) {
0688: table = t;
0689: }
0690:
0691: /**
0692: * Create a new dictionary with the element as content.
0693: * @param elements The initial elements that is inserted in the
0694: * dictionary. Even numbered elements are keys,
0695: * odd numbered elements are values.
0696: */
0697: public PyDictionary(PyObject elements[]) {
0698: this ();
0699: for (int i = 0; i < elements.length; i += 2) {
0700: table.put(elements[i], elements[i + 1]);
0701: }
0702: }
0703:
0704: final void dict_init(PyObject[] args, String[] kwds) {
0705: int nargs = args.length - kwds.length;
0706: if (nargs > 1)
0707: throw PyBuiltinFunction.DefaultInfo.unexpectedCall(nargs,
0708: false, exposed_name, 0, 1);
0709: if (nargs == 1) {
0710: PyObject src = args[0];
0711: if (src.__findattr__("keys") != null)
0712: this .update(src);
0713: else {
0714: PyObject pairs = Py.iter(src,
0715: "iteration over non-sequence");
0716: PyObject pair;
0717: int cnt = 0;
0718: for (; (pair = pairs.__iternext__()) != null; cnt++) {
0719: try {
0720: pair = PySequence.fastSequence(pair, "");
0721: } catch (PyException e) {
0722: if (Py.matchException(e, Py.TypeError))
0723: throw Py
0724: .TypeError("cannot convert dictionary update "
0725: + "sequence element #"
0726: + cnt + " to a sequence");
0727: throw e;
0728: }
0729: int n;
0730: if ((n = pair.__len__()) != 2) {
0731: throw Py
0732: .ValueError("dictionary update sequence element #"
0733: + cnt
0734: + " has length "
0735: + n
0736: + "; 2 is required");
0737: }
0738: this .__setitem__(pair.__getitem__(0), pair
0739: .__getitem__(1));
0740: }
0741: }
0742: }
0743: for (int i = 0; i < kwds.length; i++) {
0744: this .__setitem__(kwds[i], args[nargs + i]);
0745: }
0746: }
0747:
0748: public static PyObject fromkeys(PyObject keys) {
0749: return fromkeys(keys, null);
0750: }
0751:
0752: public static PyObject fromkeys(PyObject keys, PyObject value) {
0753: return dict_fromkeys(PyType.fromClass(PyDictionary.class),
0754: keys, value);
0755: }
0756:
0757: final static PyObject dict_fromkeys(PyType type, PyObject keys) {
0758: return dict_fromkeys(type, keys, null);
0759: }
0760:
0761: final static PyObject dict_fromkeys(PyType type, PyObject keys,
0762: PyObject value) {
0763: if (value == null) {
0764: value = Py.None;
0765: }
0766: PyObject d = type.__call__();
0767: PyIterator iter = (PyIterator) keys.__iter__();
0768: for (PyObject o = iter.__iternext__(); o != null; o = iter
0769: .__iternext__()) {
0770: d.__setitem__(o, value);
0771: }
0772: return d;
0773: }
0774:
0775: /* commenting this out -- PyObject.safeRepr() does the same thing, and this one
0776: messes up subclasses of dict. XXX: delete all of this if this turns out okay.
0777:
0778: public String safeRepr() throws PyIgnoreMethodTag {
0779: return "'dict' object";
0780: }
0781: */
0782:
0783: public int __len__() {
0784: return dict___len__();
0785: }
0786:
0787: final int dict___len__() {
0788: return table.size();
0789: }
0790:
0791: public boolean __nonzero__() {
0792: return dict___nonzero__();
0793: }
0794:
0795: final boolean dict___nonzero__() {
0796: return table.size() != 0;
0797: }
0798:
0799: public PyObject __finditem__(int index) {
0800: throw Py.TypeError("loop over non-sequence");
0801: }
0802:
0803: public PyObject __finditem__(PyObject key) {
0804: return dict___finditem__(key);
0805: }
0806:
0807: final PyObject dict___finditem__(PyObject key) {
0808: return (PyObject) table.get(key);
0809: }
0810:
0811: public void __setitem__(PyObject key, PyObject value) {
0812: dict___setitem__(key, value);
0813: }
0814:
0815: final void dict___setitem__(PyObject key, PyObject value) {
0816: table.put(key, value);
0817: }
0818:
0819: public void __delitem__(PyObject key) {
0820: dict___delitem__(key);
0821: }
0822:
0823: final void dict___delitem__(PyObject key) {
0824: Object ret = table.remove(key);
0825: if (ret == null)
0826: throw Py.KeyError(key.toString());
0827: }
0828:
0829: public PyObject __iter__() {
0830: return dict___iter__();
0831: }
0832:
0833: final PyObject dict___iter__() {
0834: return new PyDictionaryIter(this , table.keys(),
0835: PyDictionaryIter.KEYS);
0836: }
0837:
0838: public String toString() {
0839: return dict_toString();
0840: }
0841:
0842: final String dict_toString() {
0843: ThreadState ts = Py.getThreadState();
0844: if (!ts.enterRepr(this )) {
0845: return "{...}";
0846: }
0847:
0848: java.util.Enumeration ek = table.keys();
0849: java.util.Enumeration ev = table.elements();
0850: StringBuffer buf = new StringBuffer("{");
0851: while (ek.hasMoreElements() && ev.hasMoreElements()) {
0852: buf.append(((PyObject) ek.nextElement()).__repr__()
0853: .toString());
0854: buf.append(": ");
0855: buf.append(((PyObject) ev.nextElement()).__repr__()
0856: .toString());
0857: buf.append(", ");
0858: }
0859: if (buf.length() > 1) {
0860: buf.delete(buf.length() - 2, buf.length());
0861: }
0862: buf.append("}");
0863:
0864: ts.exitRepr(this );
0865: return buf.toString();
0866: }
0867:
0868: public PyObject __eq__(PyObject ob_other) {
0869: return dict___eq__(ob_other);
0870: }
0871:
0872: final PyObject dict___eq__(PyObject ob_other) {
0873: if (ob_other.getType() != getType())
0874: return null;
0875:
0876: PyDictionary other = (PyDictionary) ob_other;
0877: int an = table.size();
0878: int bn = other.table.size();
0879: if (an != bn)
0880: return Py.Zero;
0881:
0882: PyList akeys = keys();
0883: for (int i = 0; i < an; i++) {
0884: PyObject akey = akeys.pyget(i);
0885: PyObject bvalue = other.__finditem__(akey);
0886: if (bvalue == null)
0887: return Py.Zero;
0888: PyObject avalue = __finditem__(akey);
0889: if (!avalue._eq(bvalue).__nonzero__())
0890: return Py.Zero;
0891: }
0892: return Py.One;
0893: }
0894:
0895: public PyObject __ne__(PyObject ob_other) {
0896: return dict___ne__(ob_other);
0897: }
0898:
0899: final PyObject dict___ne__(PyObject ob_other) {
0900: PyObject eq_result = __eq__(ob_other);
0901: if (eq_result == null)
0902: return null;
0903: return eq_result == Py.One ? Py.Zero : Py.One;
0904: }
0905:
0906: final PyObject dict___lt__(PyObject ob_other) {
0907: int result = __cmp__(ob_other);
0908: if (result == -2) {
0909: return null;
0910: }
0911: return result < 0 ? Py.One : Py.Zero;
0912: }
0913:
0914: final PyObject dict___gt__(PyObject ob_other) {
0915: int result = __cmp__(ob_other);
0916: if (result == -2) {
0917: return null;
0918: }
0919: return result > 0 ? Py.One : Py.Zero;
0920: }
0921:
0922: final PyObject dict___le__(PyObject ob_other) {
0923: int result = __cmp__(ob_other);
0924: if (result == -2) {
0925: return null;
0926: }
0927: return result <= 0 ? Py.One : Py.Zero;
0928: }
0929:
0930: final PyObject dict___ge__(PyObject ob_other) {
0931: int result = __cmp__(ob_other);
0932: if (result == -2) {
0933: return null;
0934: }
0935: return result >= 0 ? Py.One : Py.Zero;
0936: }
0937:
0938: public int __cmp__(PyObject ob_other) {
0939: return dict___cmp__(ob_other);
0940: }
0941:
0942: final int dict___cmp__(PyObject ob_other) {
0943: if (ob_other.getType() != getType())
0944: return -2;
0945:
0946: PyDictionary other = (PyDictionary) ob_other;
0947: int an = table.size();
0948: int bn = other.table.size();
0949: if (an < bn)
0950: return -1;
0951: if (an > bn)
0952: return 1;
0953:
0954: PyList akeys = keys();
0955: PyList bkeys = other.keys();
0956:
0957: akeys.sort();
0958: bkeys.sort();
0959:
0960: for (int i = 0; i < bn; i++) {
0961: PyObject akey = akeys.pyget(i);
0962: PyObject bkey = bkeys.pyget(i);
0963: int c = akey._cmp(bkey);
0964: if (c != 0)
0965: return c;
0966:
0967: PyObject avalue = __finditem__(akey);
0968: PyObject bvalue = other.__finditem__(bkey);
0969: if (avalue == null) {
0970: if (bvalue == null) {
0971: continue;
0972: }
0973: return -3;
0974: } else if (bvalue == null) {
0975: return -3;
0976: }
0977: c = avalue._cmp(bvalue);
0978: if (c != 0)
0979: return c;
0980: }
0981: return 0;
0982: }
0983:
0984: /**
0985: * Return true if the key exist in the dictionary.
0986: */
0987: public boolean has_key(PyObject key) {
0988: return dict_has_key(key);
0989: }
0990:
0991: final boolean dict_has_key(PyObject key) {
0992: return table.containsKey(key);
0993: }
0994:
0995: final boolean dict___contains__(PyObject o) {
0996: return dict_has_key(o);
0997: }
0998:
0999: /**
1000: * Return this[key] if the key exists in the mapping, default_object
1001: * is returned otherwise.
1002: *
1003: * @param key the key to lookup in the dictionary.
1004: * @param default_object the value to return if the key does not
1005: * exists in the mapping.
1006: */
1007: public PyObject get(PyObject key, PyObject default_object) {
1008: return dict_get(key, default_object);
1009: }
1010:
1011: final PyObject dict_get(PyObject key, PyObject default_object) {
1012: PyObject o = dict___finditem__(key);
1013: if (o == null)
1014: return default_object;
1015: else
1016: return o;
1017: }
1018:
1019: /**
1020: * Return this[key] if the key exists in the mapping, None
1021: * is returned otherwise.
1022: *
1023: * @param key the key to lookup in the dictionary.
1024: */
1025: public PyObject get(PyObject key) {
1026: return dict_get(key);
1027: }
1028:
1029: final PyObject dict_get(PyObject key) {
1030: return get(key, Py.None);
1031: }
1032:
1033: /**
1034: * Return a shallow copy of the dictionary.
1035: */
1036: public PyDictionary copy() {
1037: return dict_copy();
1038: }
1039:
1040: final PyDictionary dict_copy() {
1041: return new PyDictionary((Hashtable) table.clone());
1042: }
1043:
1044: /**
1045: * Remove all items from the dictionary.
1046: */
1047: public void clear() {
1048: dict_clear();
1049: }
1050:
1051: final void dict_clear() {
1052: table.clear();
1053: }
1054:
1055: /**
1056: * Insert all the key:value pairs from <code>d</code> into
1057: * this dictionary.
1058: */
1059: public void update(PyObject d) {
1060: dict_update(d);
1061: }
1062:
1063: final void dict_update(PyObject d) {
1064: if (d instanceof PyDictionary) {
1065: do_update((PyDictionary) d);
1066: } else if (d instanceof PyStringMap) {
1067: do_update(d, ((PyStringMap) d).keys());
1068: } else {
1069: do_update(d, d.invoke("keys"));
1070: }
1071:
1072: }
1073:
1074: private void do_update(PyDictionary d) {
1075: Hashtable otable = d.table;
1076:
1077: java.util.Enumeration ek = otable.keys();
1078: java.util.Enumeration ev = otable.elements();
1079: int n = otable.size();
1080:
1081: for (int i = 0; i < n; i++)
1082: table.put(ek.nextElement(), ev.nextElement());
1083: }
1084:
1085: private void do_update(PyObject d, PyObject keys) {
1086: PyObject iter = keys.__iter__();
1087: for (PyObject key; (key = iter.__iternext__()) != null;)
1088: __setitem__(key, d.__getitem__(key));
1089: }
1090:
1091: /**
1092: * Return this[key] if the key exist, otherwise insert key with
1093: * a None value and return None.
1094: *
1095: * @param key the key to lookup in the dictionary.
1096: */
1097: public PyObject setdefault(PyObject key) {
1098: return dict_setdefault(key);
1099: }
1100:
1101: final PyObject dict_setdefault(PyObject key) {
1102: return setdefault(key, Py.None);
1103: }
1104:
1105: /**
1106: * Return this[key] if the key exist, otherwise insert key with
1107: * the value of failobj and return failobj
1108: *
1109: * @param key the key to lookup in the dictionary.
1110: * @param failobj the default value to insert in the dictionary
1111: * if key does not already exist.
1112: */
1113: public PyObject setdefault(PyObject key, PyObject failobj) {
1114: return dict_setdefault(key, failobj);
1115: }
1116:
1117: final PyObject dict_setdefault(PyObject key, PyObject failobj) {
1118: PyObject o = __finditem__(key);
1119: if (o == null)
1120: __setitem__(key, o = failobj);
1121: return o;
1122: }
1123:
1124: /**
1125: * Return a value based on key
1126: * from the dictionary.
1127: */
1128: public PyObject pop(PyObject key) {
1129: return dict_pop(key);
1130: }
1131:
1132: final PyObject dict_pop(PyObject key) {
1133: if (!table.containsKey(key))
1134: throw Py.KeyError("popitem(): dictionary is empty");
1135: PyObject val = (PyObject) table.get(key);
1136: table.remove(key);
1137: return val;
1138: }
1139:
1140: /**
1141: * Return a value based on key
1142: * from the dictionary or default if that key is not found.
1143: */
1144: public PyObject pop(PyObject key, PyObject defaultValue) {
1145: return dict_pop(key, defaultValue);
1146: }
1147:
1148: final PyObject dict_pop(PyObject key, PyObject defaultValue) {
1149: if (!table.containsKey(key))
1150: return defaultValue;
1151: PyObject val = (PyObject) table.get(key);
1152: table.remove(key);
1153: return val;
1154: }
1155:
1156: /**
1157: * Return a random (key, value) tuple pair and remove the pair
1158: * from the dictionary.
1159: */
1160: public PyObject popitem() {
1161: return dict_popitem();
1162: }
1163:
1164: final PyObject dict_popitem() {
1165: java.util.Enumeration keys = table.keys();
1166: if (!keys.hasMoreElements())
1167: throw Py.KeyError("popitem(): dictionary is empty");
1168: PyObject key = (PyObject) keys.nextElement();
1169: PyObject val = (PyObject) table.get(key);
1170: table.remove(key);
1171: return new PyTuple(new PyObject[] { key, val });
1172: }
1173:
1174: /**
1175: * Return a copy of the dictionarys list of (key, value) tuple
1176: * pairs.
1177: */
1178: public PyList items() {
1179: return dict_items();
1180: }
1181:
1182: final PyList dict_items() {
1183: java.util.Enumeration ek = table.keys();
1184: java.util.Enumeration ev = table.elements();
1185: int n = table.size();
1186: java.util.Vector l = new java.util.Vector(n);
1187:
1188: for (int i = 0; i < n; i++)
1189: l.addElement(new PyTuple(new PyObject[] {
1190: (PyObject) ek.nextElement(),
1191: (PyObject) ev.nextElement() }));
1192: return new PyList(l);
1193: }
1194:
1195: /**
1196: * Return a copy of the dictionarys list of keys.
1197: */
1198: public PyList keys() {
1199: return dict_keys();
1200: }
1201:
1202: final PyList dict_keys() {
1203: java.util.Enumeration e = table.keys();
1204: int n = table.size();
1205: java.util.Vector l = new java.util.Vector(n);
1206:
1207: for (int i = 0; i < n; i++)
1208: l.addElement(e.nextElement());
1209: return new PyList(l);
1210: }
1211:
1212: /**
1213: * Return a copy of the dictionarys list of values.
1214: */
1215: public PyList values() {
1216: return dict_values();
1217: }
1218:
1219: final PyList dict_values() {
1220: java.util.Enumeration e = table.elements();
1221: int n = table.size();
1222: java.util.Vector l = new java.util.Vector(n);
1223:
1224: for (int i = 0; i < n; i++)
1225: l.addElement(e.nextElement());
1226: return new PyList(l);
1227: }
1228:
1229: /**
1230: * Return an interator over (key, value) pairs.
1231: */
1232: public PyObject iteritems() {
1233: return dict_iteritems();
1234: }
1235:
1236: final PyObject dict_iteritems() {
1237: return new PyDictionaryIter(this , table.keys(),
1238: PyDictionaryIter.ITEMS);
1239: }
1240:
1241: /**
1242: * Return an interator over (key, value) pairs.
1243: */
1244: public PyObject iterkeys() {
1245: return dict_iterkeys();
1246: }
1247:
1248: final PyObject dict_iterkeys() {
1249: return new PyDictionaryIter(this , table.keys(),
1250: PyDictionaryIter.KEYS);
1251: }
1252:
1253: /**
1254: * Return an interator over (key, value) pairs.
1255: */
1256: public PyObject itervalues() {
1257: return dict_itervalues();
1258: }
1259:
1260: final PyObject dict_itervalues() {
1261: return new PyDictionaryIter(this , table.keys(),
1262: PyDictionaryIter.VALUES);
1263: }
1264:
1265: public int hashCode() {
1266: return dict_hashCode();
1267: }
1268:
1269: final int dict_hashCode() {
1270: throw Py.TypeError("unhashable type");
1271: }
1272:
1273: public boolean isSequenceType() {
1274: return false;
1275: }
1276:
1277: }
1278:
1279: class PyDictionaryIter extends PyIterator {
1280: public static final int KEYS = 0;
1281: public static final int VALUES = 1;
1282: public static final int ITEMS = 2;
1283:
1284: private PyObject dict;
1285: private Enumeration enumeration;
1286: private int type;
1287:
1288: public PyDictionaryIter(PyObject dict, Enumeration e, int type) {
1289: this .dict = dict;
1290: this .enumeration = e;
1291: this .type = type;
1292: }
1293:
1294: public PyObject __iternext__() {
1295: if (!enumeration.hasMoreElements())
1296: return null;
1297: PyObject key = (PyObject) enumeration.nextElement();
1298: switch (type) {
1299: case VALUES:
1300: return dict.__finditem__(key);
1301: case ITEMS:
1302: return new PyTuple(new PyObject[] { key,
1303: dict.__finditem__(key) });
1304: default: // KEYS
1305: return key;
1306: }
1307: }
1308: }
|