Issue2511

classification
Title: Percent operator calls __getattr__('__getitem__')
Type: behaviour Severity: normal
Components: Versions: Jython 2.7
Milestone: Jython 2.7.1
process
Status: pending Resolution: accepted
Dependencies: Superseder:
Assigned To: stefan.richthofer Nosy List: progval, stefan.richthofer
Priority: normal Keywords:

Created on 2016-07-30.09:53:57 by progval, last changed 2017-01-31.23:57:26 by stefan.richthofer.

Messages
msg10884 (view) Author: (progval) Date: 2016-07-30.09:53:57
Hi,

I have the following script:

class Foo(object):
    def __str__(self):
        return 'foo'
    def __getattr__(self, name):
        print(name)

f = Foo()
print('%s' % f)


With CPython and Pypy, it prints:

foo


With Jython 2.7.1b3, it prints:

__getitem__
foo
msg11041 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-01-28.16:42:56
This one boils down to the following code snippet in PyString.java:

// Not a tuple, but possibly still some kind of container: use special argIndex values.
            argIndex = -1;
            if (args instanceof AbstractDict
                    || (!(args instanceof PySequence) && args.__findattr__("__getitem__") != null)) {
                dict = args;
                argIndex = -3;
            }

Here

args.__findattr__("__getitem__")

is used for mapping-detection. A possible solution would be to change it into

args.object___findattr__("__getitem__")

I don't think this fix would break anything. Overriding __getattr__ to sneak in a custom __getitem__ sounds insane. And it doesn't look like CPython would fall for this kind of manipulation in %-string-format-sense.

This fix also passes regrtests. So far it looks good to me. Opinions?
msg11042 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-01-28.16:54:33
Actually object___findattr__ should be called with interned String, i.e.:

args.object___findattr__("__getitem__".intern())
msg11049 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-01-31.23:57:08
Fixed as of https://github.com/jythontools/jython/commit/fd84c10f7fc93076c8552159c4c71c7a9582f354
History
Date User Action Args
2017-01-31 23:57:26stefan.richthofersetstatus: open -> pending
messages: + msg11049
2017-01-28 16:54:33stefan.richthofersetmessages: + msg11042
2017-01-28 16:42:57stefan.richthofersetnosy: + stefan.richthofer
messages: + msg11041
priority: normal
assignee: stefan.richthofer
milestone: Jython 2.7.2 -> Jython 2.7.1
type: behaviour
2016-09-30 16:19:20zyasoftsetmilestone: Jython 2.7.2
2016-09-19 04:36:26zyasoftsetresolution: accepted
2016-07-30 09:53:57progvalcreate