GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add operator.methodcaller. Thanks Jezreel Ng! · jython/jython@cb90a3d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • CPythonLib.includes
  • CoreExposed.includes
  • Lib/test
    • test_operator.py
  • NEWS
  • src/org/python/modules
    • operator.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎CPythonLib.includes‎

Copy file name to clipboardExpand all lines: CPythonLib.includes
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ posixfile.py
115115
pprint.py
116116
profile.py
117117
pstats.py
118+
pty.py
118119
pyclbr.py
119120
pydoc_topics.py
120121
Queue.py
@@ -154,6 +155,7 @@ token.py
154155
tokenize.py
155156
trace.py
156157
traceback.py
158+
tty.py
157159
tzparse.py
158160
urllib2.py
159161
urlparse.py
Collapse file

‎CoreExposed.includes‎

Copy file name to clipboardExpand all lines: CoreExposed.includes
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ org/python/modules/_weakref/ProxyType.class
9292
org/python/modules/_weakref/ReferenceType.class
9393
org/python/modules/operator$PyAttrGetter.class
9494
org/python/modules/operator$PyItemGetter.class
95+
org/python/modules/operator$PyMethodCaller.class
9596
org/python/modules/posix/PyStatResult.class
9697
org/python/modules/random/PyRandom.class
9798
org/python/modules/thread/PyLocal.class
Collapse file

‎Lib/test/test_operator.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_operator.py
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ def __getitem__(self, name):
447447
self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5'))
448448
self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data)
449449

450-
@unittest.skip("FIXME: broken")
451450
def test_methodcaller(self):
452451
self.assertRaises(TypeError, operator.methodcaller)
453452
class A:
Collapse file

‎NEWS‎

Copy file name to clipboardExpand all lines: NEWS
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ Jython NEWS
22

33
Jython 2.7a3
44
Bugs Fixed
5+
- [ 1909 ] attrgetter does not parse dotted attributes
6+
- [ 1924 ] Implement operator.methodcaller
57
- [ 1934 ] Break itertools.compress into a separate class
6-
- [ 1932 ] Make check for iterability in chain() arguments lazy
78
- [ 1933 ] Break itertools.cycle into a separate class
9+
- [ 1932 ] Make check for iterability in chain() arguments lazy
810
- [ 1931 ] Check that there are exactly 2 filter args
911
- [ 1913 ] Support short -W options
1012
- [ 1897 ] 2.7.0ax only has partial ssl support
Collapse file

‎src/org/python/modules/operator.java‎

Copy file name to clipboardExpand all lines: src/org/python/modules/operator.java
+73-9Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import org.python.core.Py;
77
import org.python.core.PyBuiltinFunctionSet;
88
import org.python.core.PyIgnoreMethodTag;
9+
import org.python.core.PyMethod;
910
import org.python.core.PyNewWrapper;
1011
import org.python.core.PyObject;
1112
import org.python.core.PyString;
1213
import org.python.core.PyTuple;
1314
import org.python.core.PyType;
1415
import org.python.core.PyUnicode;
16+
import org.python.expose.ExposedGet;
1517
import org.python.expose.ExposedMethod;
1618
import org.python.expose.ExposedNew;
1719
import org.python.expose.ExposedType;
@@ -243,6 +245,7 @@ public static void classDictInit(PyObject dict) throws PyIgnoreMethodTag {
243245

244246
dict.__setitem__("attrgetter", PyAttrGetter.TYPE);
245247
dict.__setitem__("itemgetter", PyItemGetter.TYPE);
248+
dict.__setitem__("methodcaller", PyMethodCaller.TYPE);
246249
}
247250

248251
public static int countOf(PyObject seq, PyObject item) {
@@ -316,15 +319,7 @@ private PyObject getattr(PyObject obj, PyObject name) {
316319
// XXX: We should probably have a PyObject.__getattr__(PyObject) that does
317320
// this. This is different than __builtin__.getattr (in how it handles
318321
// exceptions)
319-
String nameStr;
320-
if (name instanceof PyUnicode) {
321-
nameStr = ((PyUnicode)name).encode();
322-
} else if (name instanceof PyString) {
323-
nameStr = name.asString();
324-
} else {
325-
throw Py.TypeError(String.format("attribute name must be string, not '%.200s'",
326-
name.getType().fastGetName()));
327-
}
322+
String nameStr = ensureStringAttribute(name);
328323
String[] components = nameStr.split("\\.");
329324
for (String component : components) {
330325
obj = obj.__getattr__(component.intern());
@@ -379,4 +374,73 @@ final PyObject itemgetter___call__(PyObject[] args, String[] keywords) {
379374
return new PyTuple(result);
380375
}
381376
}
377+
378+
/**
379+
* The methodcaller type.
380+
*/
381+
@ExposedType(name = "operator.methodcaller", isBaseType = false)
382+
static class PyMethodCaller extends PyObject {
383+
384+
public static final PyType TYPE = PyType.fromClass(PyMethodCaller.class);
385+
386+
public String name;
387+
public PyObject[] args;
388+
public String[] keywords;
389+
390+
@ExposedGet
391+
public static PyString __doc__ = new PyString(
392+
"methodcaller(name, ...) --> methodcaller object\n\n"
393+
+ "Return a callable object that calls the given method on its operand.\n"
394+
+ "After, f = methodcaller('name'), the call f(r) returns r.name().\n"
395+
+ "After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n"
396+
+ "r.name('date', foo=1)");
397+
398+
public PyMethodCaller(String name, PyObject[] args, String[] keywords) {
399+
this.name = name;
400+
this.args = args;
401+
this.keywords = keywords;
402+
}
403+
404+
@ExposedNew
405+
final static PyObject methodcaller___new__(PyNewWrapper new_, boolean init,
406+
PyType subtype, PyObject[] args,
407+
String[] keywords) {
408+
409+
if (args.length == 0) {
410+
throw Py.TypeError("methodcaller needs at least one argument, the method name");
411+
}
412+
String nameStr = ensureStringAttribute(args[0]);
413+
PyObject[] newArgs = new PyObject[args.length-1];
414+
System.arraycopy(args, 1, newArgs, 0, args.length-1);
415+
return new PyMethodCaller(nameStr, newArgs, keywords);
416+
}
417+
418+
@Override
419+
public PyObject __call__(PyObject[] args, String[] keywords) {
420+
return methodcaller___call__(args, keywords);
421+
}
422+
423+
@ExposedMethod
424+
final PyObject methodcaller___call__(PyObject[] args, String[] keywords) {
425+
if (args.length > 1) {
426+
throw Py.TypeError("methodcaller expected 1 arguments, got " + args.length);
427+
}
428+
ArgParser ap = new ArgParser("methodcaller", args, Py.NoKeywords, "obj");
429+
PyObject obj = ap.getPyObject(0);
430+
return obj.invoke(name, this.args, this.keywords);
431+
}
432+
}
433+
434+
private static String ensureStringAttribute(PyObject name) {
435+
String nameStr;
436+
if (name instanceof PyUnicode) {
437+
nameStr = ((PyUnicode)name).encode();
438+
} else if (name instanceof PyString) {
439+
nameStr = name.asString();
440+
} else {
441+
throw Py.TypeError(String.format("attribute name must be string, not '%.200s'",
442+
name.getType().fastGetName()));
443+
}
444+
return nameStr;
445+
}
382446
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.