From: <pj...@us...> - 2009-10-01 01:06:45
|
Revision: 6815 http://jython.svn.sourceforge.net/jython/?rev=6815&view=rev Author: pjenvey Date: 2009-10-01 01:06:20 +0000 (Thu, 01 Oct 2009) Log Message: ----------- allow del of default_factory, small cleanup Modified Paths: -------------- trunk/jython/src/org/python/modules/_collections/PyDefaultDict.java Modified: trunk/jython/src/org/python/modules/_collections/PyDefaultDict.java =================================================================== --- trunk/jython/src/org/python/modules/_collections/PyDefaultDict.java 2009-09-27 01:34:11 UTC (rev 6814) +++ trunk/jython/src/org/python/modules/_collections/PyDefaultDict.java 2009-10-01 01:06:20 UTC (rev 6815) @@ -1,3 +1,4 @@ +/* Copyright (c) Jython Developers */ package org.python.modules._collections; import java.util.Map; @@ -7,6 +8,7 @@ import org.python.core.PyObject; import org.python.core.PyTuple; import org.python.core.PyType; +import org.python.expose.ExposedDelete; import org.python.expose.ExposedGet; import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; @@ -14,25 +16,25 @@ import org.python.expose.ExposedType; /** - * PyDefaultDict - This is a subclass of the builtin dict(PyDictionary) class. It supports one - * additional method __missing__ and adds one writable instance variable default_factory. The - * remaining functionality is the same as for the dict class. - * - * collections.defaultdict([default_factory[, ...]]) - returns a new dictionary-like object. The - * first argument provides the initial value for the default_factory attribute; it defaults to None. - * All remaining arguments are treated the same as if they were passed to the dict constructor, - * including keyword arguments. + * PyDefaultDict - This is a subclass of the builtin dict(PyDictionary) class. It supports + * one additional method __missing__ and adds one writable instance variable + * defaultFactory. The remaining functionality is the same as for the dict class. + * + * collections.defaultdict([defaultFactory[, ...]]) - returns a new dictionary-like + * object. The first argument provides the initial value for the defaultFactory attribute; + * it defaults to None. All remaining arguments are treated the same as if they were + * passed to the dict constructor, including keyword arguments. */ @ExposedType(name = "collections.defaultdict") public class PyDefaultDict extends PyDictionary { - + public static final PyType TYPE = PyType.fromClass(PyDefaultDict.class); - + /** - * This attribute is used by the __missing__ method; it is initialized from - * the first argument to the constructor, if present, or to None, if absent. + * This attribute is used by the __missing__ method; it is initialized from the first + * argument to the constructor, if present, or to None, if absent. */ - private PyObject default_factory = Py.None; + private PyObject defaultFactory = Py.None; public PyDefaultDict() { this(TYPE); @@ -41,18 +43,18 @@ public PyDefaultDict(PyType subtype) { super(subtype); } - + public PyDefaultDict(PyType subtype, Map<PyObject, PyObject> map) { super(subtype, map); } - + @ExposedMethod @ExposedNew final void defaultdict___init__(PyObject[] args, String[] kwds) { int nargs = args.length - kwds.length; - if (nargs != 0) { - default_factory = args[0]; - if (default_factory.__findattr__("__call__") == null) { + if (nargs != 0) { + defaultFactory = args[0]; + if (defaultFactory.__findattr__("__call__") == null) { throw Py.TypeError("first argument must be callable"); } PyObject newargs[] = new PyObject[args.length - 1]; @@ -61,28 +63,30 @@ } } + @Override public PyObject __finditem__(PyObject key) { return dict___getitem__(key); } /** - * This method is called by the __getitem__ method of the dict class when - * the requested key is not found; whatever it returns or raises is then - * returned or raised by __getitem__. + * This method is called by the __getitem__ method of the dict class when the + * requested key is not found; whatever it returns or raises is then returned or + * raised by __getitem__. */ @ExposedMethod final PyObject defaultdict___missing__(PyObject key) { - if (default_factory == Py.None) { + if (defaultFactory == Py.None) { throw Py.KeyError(key); - } - PyObject value = default_factory.__call__(); + } + PyObject value = defaultFactory.__call__(); if (value == null) { return value; - } + } __setitem__(key, value); return value; } + @Override public PyObject __reduce__() { return defaultdict___reduce__(); } @@ -90,47 +94,49 @@ @ExposedMethod final PyObject defaultdict___reduce__() { PyTuple args = null; - if (default_factory == Py.None) { + if (defaultFactory == Py.None) { args = new PyTuple(); - } else { - PyObject[] ob = {default_factory}; + } else { + PyObject[] ob = {defaultFactory}; args = new PyTuple(ob); - } + } return new PyTuple(getType(), args, Py.None, Py.None, items()); } + @Override public PyDictionary copy() { return defaultdict_copy(); } - @ExposedMethod - final PyDefaultDict defaultdict_copy() { - return defaultdict___copy__(); - } - - @ExposedMethod - final PyDefaultDict defaultdict___copy__() { + @ExposedMethod(names = {"copy", "__copy__"}) + final PyDefaultDict defaultdict_copy() { PyDefaultDict ob = new PyDefaultDict(TYPE, table); - ob.default_factory = default_factory; + ob.defaultFactory = defaultFactory; return ob; } + @Override public String toString() { return defaultdict_toString(); } @ExposedMethod(names = "__repr__") final String defaultdict_toString() { - return String.format("defaultdict(%s, %s)", default_factory, super.toString()); + return String.format("defaultdict(%s, %s)", defaultFactory, super.toString()); } @ExposedGet(name = "default_factory") public PyObject getDefaultFactory() { - return default_factory; + return defaultFactory; } @ExposedSet(name = "default_factory") public void setDefaultFactory(PyObject value) { - default_factory = value; + defaultFactory = value; } + + @ExposedDelete(name = "default_factory") + public void delDefaultFactory() { + defaultFactory = Py.None; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |