Search code examples
pythonoctalpython-2to3

Python 2to3 tool adds a vowel to my integer


I was running the 2to3 tool on various scripts I'd written to get an idea of what I will need to change to port these to Python 3 (though I will be doing it by hand in the end).

While doing so, I ran into an odd change 2to3 made in one of my scripts:

-def open_pipe(pipe, perms=0644):
+def open_pipe(pipe, perms=0o644):

Um... Why did 2to3 add a "o" in the middle of my "perms" integer?

That's line 41 from the original source found here: https://github.com/ksoviero/Public/blob/master/tempus.py


Solution

  • Try typing 0644 in your python2 shell. It will give you a different number because it is octal. In python3, the 0o signifies an octal number.

    python2:

    >>> 0644
    420
    >>> 
    

    python3:

    >>> 0644
      File "<stdin>", line 1
        0644
           ^
    SyntaxError: invalid token
    >>> 0o644
    420
    >>> 
    

    New in python3:

    Octal literals are no longer of the form 0720; use 0o720 instead.