1

As a trivial demonstration of what I mean, subprocess.communicate()'s stdout is always a bytes string.

But:

>>> assert stdout == bytes('{}'.format(stdin), "utf-8"), \
        "{} != {}".format(stdout, stdin)
AssertionError: b'\x00' != b'0'

Moreover, if I follow Convert bytes to a Python string,

>>> assert \
    stdout.decode("utf-8") == \
    bytes('{}'.format(stdin), "utf-8").decode("utf-8"), \
    "{} != {}".format(stdout, stdin)
AssertionError: b'\x00' != b'0'

It's trying to convert and compare byte strings, but I can't figure out how to either

  1. turn them into proper bytes

  2. make the strings look the same

In this case, I happen to have the flexibility to read the ord of the singular char and compare int:0 with int:0. What else can solve this?

Community
  • 1
  • 1
cat
  • 3,888
  • 5
  • 32
  • 61
  • I'm having a hard time understanding what you're going for here. . . Maybe it's because I don't have any understanding of why `stdout` and `stdin` should be the same? Clearly `b'0'` (the 0 character) isn't equal to `b'\x00'` (the null terminator) in any encoding. . . – mgilson Feb 22 '16 at 00:22

1 Answers1

3

Your second byte string, b'0', is indeed not the same string as b'\x00'. The former is the character 0, which is ASCII codepoint 0x30. You'll notice that:

>>> b'0' == b'\x30'
True

b'\x00' represents the NUL character.

See the Binary Sequence Types and String and Bytes literals specs, which describes the b'' syntax:

Only ASCII characters are permitted in bytes literals (regardless of the declared source code encoding). Any binary values over 127 must be entered into bytes literals using the appropriate escape sequence.

dimo414
  • 47,227
  • 18
  • 148
  • 244