Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RHS not consulted in str % subclass_of_str case. #72784

Closed
mjpieters mannequin opened this issue Nov 3, 2016 · 9 comments
Closed

RHS not consulted in str % subclass_of_str case. #72784

mjpieters mannequin opened this issue Nov 3, 2016 · 9 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@mjpieters
Copy link
Mannequin

mjpieters mannequin commented Nov 3, 2016

BPO 28598
Nosy @mjpieters, @berkerpeksag, @serhiy-storchaka, @zhangyangyu
PRs
  • bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations #51
  • [3.5] bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations #94
  • [backport to 3.6] bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations #95
  • [2.7] bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations #366
  • [backport to 2.7] bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations #382
  • [Do Not Merge] Sample of CPython life with blurb. #703
  • [Do Not Merge] Convert Misc/NEWS so that it is managed by towncrier #552
  • Files
  • issue28598.patch: Proposed patch, v1
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-03-01.06:29:40.530>
    created_at = <Date 2016-11-03.13:36:04.717>
    labels = ['interpreter-core', 'type-bug']
    title = 'RHS not consulted in `str % subclass_of_str` case.'
    updated_at = <Date 2017-03-31.16:36:38.525>
    user = 'https://github.com/mjpieters'

    bugs.python.org fields:

    activity = <Date 2017-03-31.16:36:38.525>
    actor = 'dstufft'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-03-01.06:29:40.530>
    closer = 'xiang.zhang'
    components = ['Interpreter Core']
    creation = <Date 2016-11-03.13:36:04.717>
    creator = 'mjpieters'
    dependencies = []
    files = ['45338']
    hgrepos = []
    issue_num = 28598
    keywords = ['patch']
    message_count = 9.0
    messages = ['279993', '280001', '287638', '288695', '288717', '290356', '290364', '290365', '290428']
    nosy_count = 4.0
    nosy_names = ['mjpieters', 'berker.peksag', 'serhiy.storchaka', 'xiang.zhang']
    pr_nums = ['51', '94', '95', '366', '382', '703', '552']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28598'
    versions = ['Python 2.7']

    @mjpieters
    Copy link
    Mannequin Author

    mjpieters mannequin commented Nov 3, 2016

    The BINARY_MODULO operator hardcodes a test for PyUnicode:

            TARGET(BINARY_MODULO) {
                PyObject *divisor = POP();
                PyObject *dividend = TOP();
                PyObject *res = PyUnicode_CheckExact(dividend) ?
                    PyUnicode_Format(dividend, divisor) :
                    PyNumber_Remainder(dividend, divisor);

    This means that a RHS subclass of str can't override the operator:

    >>> class Foo(str):
    ...     def __rmod__(self, other):
    ...         return self % other
    ...
    >>> "Bar: %s" % Foo("Foo: %s")
    'Bar: Foo %s'

    The expected output there is "Foo: Bar %s".

    This works correctly for bytes:

    >>> class FooBytes(bytes):
    ...     def __rmod__(self, other):
    ...         return self % other
    ...
    >>> b"Bar: %s" % FooBytes(b"Foo: %s")
    b'Foo: Bar: %s'

    and for all other types where the RHS is a subclass.

    Perhaps there should be a test to see if divisor is a subclass, and in that case take the slow path?

    @mjpieters mjpieters mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Nov 3, 2016
    @mjpieters
    Copy link
    Mannequin Author

    mjpieters mannequin commented Nov 3, 2016

    Here's a proposed patch for tip; what versions would it be worth backporting this to?

    (Note, there's no NEWS update in this patch).

    @mjpieters
    Copy link
    Mannequin Author

    mjpieters mannequin commented Feb 12, 2017

    I'm not sure if issues are linked automatically yet. I put the patch up as a pull request on GitHub: #51

    @serhiy-storchaka serhiy-storchaka added the 3.7 (EOL) end of life label Feb 12, 2017
    @serhiy-storchaka
    Copy link
    Member

    Is 2.7 free from this bug?

    @mjpieters
    Copy link
    Mannequin Author

    mjpieters mannequin commented Feb 28, 2017

    Is 2.7 free from this bug?

    No, 2.7 is affected too:

    >>> class SubclassedStr(str):
    ...     def __rmod__(self, other):
    ...         return 'Success, self.__rmod__({!r}) was called'.format(other)
    ...
    >>> 'lhs %% %r' % SubclassedStr('rhs')
    "lhs % 'rhs'"

    Expected output is "Success, self.__rmod__('lhs %% %r') was called"

    On the plus side, unicode is not affected:

    >>> class SubclassedUnicode(unicode):
    ...     def __rmod__(self, other):
    ...         return u'Success, self.__rmod__({!r}) was called'.format(other)
    ...
    >>> u'lhs %% %r' % SubclassedUnicode(u'rhs')
    u"Success, self.__rmod__(u'lhs %% %r') was called"

    @serhiy-storchaka serhiy-storchaka removed the 3.7 (EOL) end of life label Feb 28, 2017
    @zhangyangyu
    Copy link
    Member

    New changeset b4f0e98 by Xiang Zhang in branch '2.7':
    bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations (GH-366)
    b4f0e98

    @berkerpeksag
    Copy link
    Member

    New changeset bc144f0 by Berker Peksag (Martijn Pieters) in branch '3.5':
    bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations (#94)
    bc144f0

    @berkerpeksag
    Copy link
    Member

    New changeset 53039ad by Berker Peksag (Martijn Pieters) in branch '3.6':
    bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations (#95)
    53039ad

    @serhiy-storchaka
    Copy link
    Member

    New changeset d7e6433 by Serhiy Storchaka (Martijn Pieters) in branch 'master':
    bpo-28598: Support __rmod__ for RHS subclasses of str in % string formatting operations (#51)
    d7e6433

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants