|
6 | 6 | import org.python.core.Py; |
7 | 7 | import org.python.core.PyBuiltinFunctionSet; |
8 | 8 | import org.python.core.PyIgnoreMethodTag; |
| 9 | +import org.python.core.PyMethod; |
9 | 10 | import org.python.core.PyNewWrapper; |
10 | 11 | import org.python.core.PyObject; |
11 | 12 | import org.python.core.PyString; |
12 | 13 | import org.python.core.PyTuple; |
13 | 14 | import org.python.core.PyType; |
14 | 15 | import org.python.core.PyUnicode; |
| 16 | +import org.python.expose.ExposedGet; |
15 | 17 | import org.python.expose.ExposedMethod; |
16 | 18 | import org.python.expose.ExposedNew; |
17 | 19 | import org.python.expose.ExposedType; |
@@ -243,6 +245,7 @@ public static void classDictInit(PyObject dict) throws PyIgnoreMethodTag { |
243 | 245 |
|
244 | 246 | dict.__setitem__("attrgetter", PyAttrGetter.TYPE); |
245 | 247 | dict.__setitem__("itemgetter", PyItemGetter.TYPE); |
| 248 | + dict.__setitem__("methodcaller", PyMethodCaller.TYPE); |
246 | 249 | } |
247 | 250 |
|
248 | 251 | public static int countOf(PyObject seq, PyObject item) { |
@@ -316,15 +319,7 @@ private PyObject getattr(PyObject obj, PyObject name) { |
316 | 319 | // XXX: We should probably have a PyObject.__getattr__(PyObject) that does |
317 | 320 | // this. This is different than __builtin__.getattr (in how it handles |
318 | 321 | // 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); |
328 | 323 | String[] components = nameStr.split("\\."); |
329 | 324 | for (String component : components) { |
330 | 325 | obj = obj.__getattr__(component.intern()); |
@@ -379,4 +374,73 @@ final PyObject itemgetter___call__(PyObject[] args, String[] keywords) { |
379 | 374 | return new PyTuple(result); |
380 | 375 | } |
381 | 376 | } |
| 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 | + } |
382 | 446 | } |