New submission from Neil Schemenauer: This is a very rough, proof of concept patch that implements %-style formatting for bytes objects. Currently it calls __format__ with a bytes argument and expects a bytes result. I've only implemented support for bytes formatting for the 'long' object.
Expected behavior: >>> b'%s' % b'hello' b'hello' >>> b'%s' % 'hello' TypeError is raised >>> b'%s' % 123 b'123' >>> b'%d' % 123 b'123' Some issues: - %s support is incomplete, needs to handle width and padding. I think it should be done in formatbytes(). - PyBytes_Format function very likely has bugs, I copied it mostly from Python 2 and quickly made it run, I did very little testing. - long__format__ with a bytes argument is inefficient. It creates temporary Python objects that could be avoided. %-style formatting on bytes will be much less efficient than str in Python 2. We could inline the handling of certain types in PyBytes_Format, maybe longs only would be sufficent. - I'm not sure overloading __format__ is the best approach. Maybe we should introduce a new method, say __ascii__ instead and have PyBytes_Format call that. ---------- components: Interpreter Core files: bytes_mod.patch keywords: patch messages: 208313 nosy: nascheme priority: normal severity: normal stage: patch review status: open title: proof for concept patch for bytes formatting methods type: enhancement versions: Python 3.5 Added file: http://bugs.python.org/file33501/bytes_mod.patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue20284> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com