Instantiating datetime object with a month of 08 or 09 fails with "SyntaxError: invalid token"
Code to reproduce
import datetime
# These work
datetime.datetime(2018, 07, 01, 01, 01, 01)
datetime.datetime(2018, 8, 01, 01, 01, 01)
datetime.datetime(2018, 9, 01, 01, 01, 01)
datetime.datetime(2018, 10, 01, 01, 01, 01)
# These fail
datetime.datetime(2018, 08, 01, 01, 01, 01)
datetime.datetime(2018, 09, 01, 01, 01, 01)
Stack trace
C:\Python27\python2.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 54726 --file C:/x/scripts/pycharm_issue/pycharm_issue.py
pydev debugger: process 11300 is connecting
Connected to pydev debugger (build 182.3911.33)
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1664, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:/x/scripts/pycharm_issue/pycharm_issue.py", line 10
datetime.datetime(2018, 08, 01, 01, 01, 01)
^
SyntaxError: invalid token
Process finished with exit code -1
Software versions being used
PyCharm 2018.2.1 (Community Edition)
Build #PC-182.3911.33, built on August 5, 2018
JRE: 1.8.0_152-release-1248-b8 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
Project Interpreter: Python 2.7
Please sign in to leave a comment.
Hi,
sorry to say, but this has absolutely nothing to do with PyCharm :-)
In Python 2, integer literals with leading zeroes are treated as octal, so 08 is invalid, because 07 is the biggest one digit number in the octal system - decimal 8 is octal 010
So the behaviour above is perfectly fine. Please also see https://bugs.python.org/issue1715302
In python 3, this specification was changed -> https://www.python.org/dev/peps/pep-3127/#specification
Looks like I'm a little late to the party on this one! ;)
Thanks for the detailed clarification!
Ely