Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Cyborg)
  • No Skin
Collapse
Brand Logo

CIRCLE WITH A DOT

  1. Home
  2. Uncategorized
  3. #Python pop quiz:

#Python pop quiz:

Scheduled Pinned Locked Moved Uncategorized
python
4 Posts 2 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • varx@infosec.exchangeV This user is from outside of this forum
    varx@infosec.exchangeV This user is from outside of this forum
    varx@infosec.exchange
    wrote on last edited by
    #1

    #Python pop quiz:

    What does the following Python3 script output in a standard Linux environment?

    import subprocess
    print("a")
    subprocess.run(["echo", "b"])
    print("c")

    Assume it is run as `python3 quiz.py | tr \n " "` to turn newlines into spaces.

    See followup reply for solution and explanation.

    varx@infosec.exchangeV 1 Reply Last reply
    0
    • varx@infosec.exchangeV varx@infosec.exchange

      #Python pop quiz:

      What does the following Python3 script output in a standard Linux environment?

      import subprocess
      print("a")
      subprocess.run(["echo", "b"])
      print("c")

      Assume it is run as `python3 quiz.py | tr \n " "` to turn newlines into spaces.

      See followup reply for solution and explanation.

      varx@infosec.exchangeV This user is from outside of this forum
      varx@infosec.exchangeV This user is from outside of this forum
      varx@infosec.exchange
      wrote on last edited by
      #2

      It's `b a c`, because `print` writes into the sys.stdout *buffer* that only flushes when it goddamn feels like it, whereas `subprocess.run` plucks the file handle out of sys.stdout and hands it to the child process.

      So the child process writes "b" to stdout right away, while the "a" and "c" writes happen later, at parent process shutdown.

      If you instead run from a terminal *without* piping to `tr`, or you set `PYTHONUNBUFFERED=1`, then the output is instead in the `a b c` order.

      yayyyyy 😭

      joshbuddy@sfba.socialJ 1 Reply Last reply
      0
      • varx@infosec.exchangeV varx@infosec.exchange

        It's `b a c`, because `print` writes into the sys.stdout *buffer* that only flushes when it goddamn feels like it, whereas `subprocess.run` plucks the file handle out of sys.stdout and hands it to the child process.

        So the child process writes "b" to stdout right away, while the "a" and "c" writes happen later, at parent process shutdown.

        If you instead run from a terminal *without* piping to `tr`, or you set `PYTHONUNBUFFERED=1`, then the output is instead in the `a b c` order.

        yayyyyy 😭

        joshbuddy@sfba.socialJ This user is from outside of this forum
        joshbuddy@sfba.socialJ This user is from outside of this forum
        joshbuddy@sfba.social
        wrote last edited by
        #3

        @varx man, this has hurt me a few times in the past. you should mark this one with #trauma

        varx@infosec.exchangeV 1 Reply Last reply
        0
        • joshbuddy@sfba.socialJ joshbuddy@sfba.social

          @varx man, this has hurt me a few times in the past. you should mark this one with #trauma

          varx@infosec.exchangeV This user is from outside of this forum
          varx@infosec.exchangeV This user is from outside of this forum
          varx@infosec.exchange
          wrote last edited by
          #4

          @joshbuddy I definitely had one of those "am I crazy or is the world crazy" moments while debugging this.

          1 Reply Last reply
          1
          0
          • R relay@relay.infosec.exchange shared this topic
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups