[New-bugs-announce] [issue32078] string result of str(bytes()) in Python3

Arno-Can Uestuensoez report at bugs.python.org
Sun Nov 19 20:26:16 EST 2017


New submission from Arno-Can Uestuensoez <acue.opensource at gmail.com>:

Hello,
I am currently writing some dual-version libraries and have
to deal with str/unicode.
The attached code example contains the str/unicode handling.

The Python3.6.2 release behaves as I did not expected for
all of the following the conversions:

  unicode = str  # @ReservedAssignment # it is intentional


  mystring = "abc"
  u0 = unicode(bytes(mystring.encode()))  # == str(mystring)

  mystring = "abc"
  u0 = unicode(bytes(mystring.encode('utf-8')))  # == str(mystring)

  mystring = "abc"
  u0 = unicode(bytes(mystring.encode('ascii')))  # == str(mystring)

  mystring = b"abc"
  u0 = unicode(mystring)  # == str(mystring)

results for Python3 in:

  type: <class 'str'>
  len:  6
  b'abc'

while in Python2:

  type: <type 'unicode'>
  len:  3
  abc

I am  not sure whether this is the intended behavior because the manual
could eventually be misinterpreted:


  4.8.1. Bytes Objects

  Bytes objects are immutable sequences of single bytes. 
  Since many major binary protocols are based on the ASCII text 
  encoding, bytes objects offer several methods that are only 
  valid when working with ASCII compatible data and are closely
  related to string objects in a variety of other ways.

    class bytes([source[, encoding[, errors]]])

  Firstly, the syntax for bytes literals is largely the same as 
  that for string literals, except that a b prefix is added:

I expected the 'b'-prefix to be added to the input only, but I
expect the output without a type-prefix, because it is just an
attribute/property.

The result for Python3 should be similar to Python2:

  type: <type 'str'>
  len:  3
  abc

Regards
Arno

----------
components: Unicode
files: source_and_output.tar.gz
messages: 306521
nosy: acue, ezio.melotti, vstinner
priority: normal
severity: normal
status: open
title: string result of str(bytes()) in Python3
type: behavior
versions: Python 3.6
Added file: https://bugs.python.org/file47274/source_and_output.tar.gz

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32078>
_______________________________________


More information about the New-bugs-announce mailing list