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

bytes class extension with slices #91022

Closed
Sec42 mannequin opened this issue Feb 26, 2022 · 2 comments
Closed

bytes class extension with slices #91022

Sec42 mannequin opened this issue Feb 26, 2022 · 2 comments
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@Sec42
Copy link
Mannequin

Sec42 mannequin commented Feb 26, 2022

BPO 46866
Nosy @serhiy-storchaka, @Sec42
Files
  • bytes_test.py: test case
  • 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 2022-02-26.12:08:24.490>
    created_at = <Date 2022-02-26.11:37:49.763>
    labels = ['interpreter-core', 'type-bug', 'invalid', '3.9']
    title = 'bytes class extension with slices'
    updated_at = <Date 2022-02-26.12:08:24.489>
    user = 'https://github.com/Sec42'

    bugs.python.org fields:

    activity = <Date 2022-02-26.12:08:24.489>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-02-26.12:08:24.490>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2022-02-26.11:37:49.763>
    creator = 'Sec42'
    dependencies = []
    files = ['50646']
    hgrepos = []
    issue_num = 46866
    keywords = []
    message_count = 2.0
    messages = ['414092', '414095']
    nosy_count = 2.0
    nosy_names = ['serhiy.storchaka', 'Sec42']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46866'
    versions = ['Python 3.9']

    @Sec42
    Copy link
    Mannequin Author

    Sec42 mannequin commented Feb 26, 2022

    When trying to extend the builtin bytes class, slices fall back to the builtin class.

    class my_bytes(bytes):
      def dummy(self):
        print("dummy called")
    
    
    x=my_bytes.fromhex("c0de c0de")
    print(x.__class__)
    
    print(x[1:].__class__)
    
    

    x.__class__ returns <class '__main__.my_bytes'> as expected.

    But x[1:].__class__ returns <class 'bytes'>

    @Sec42 Sec42 mannequin added 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Feb 26, 2022
    @serhiy-storchaka
    Copy link
    Member

    Yes, it is consistent with all of builtin types. If you want to return a different type, override __getitem__().

    @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
    3.9 only security fixes 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

    1 participant