Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Add support for numeric literals with underscores highlighting in Python 3.6 #7505

Merged
merged 3 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spyder/py3compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

PY2 = sys.version[0] == '2'
PY3 = sys.version[0] == '3'
PY36 = sys.version_info[:2] == (3, 6)

#==============================================================================
# Data types
Expand Down
32 changes: 25 additions & 7 deletions spyder/utils/syntaxhighlighters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from spyder import dependencies
from spyder.config.base import _
from spyder.config.main import CONF
from spyder.py3compat import builtins, is_text_string, to_text_string
from spyder.py3compat import builtins, is_text_string, to_text_string, PY36
from spyder.utils.sourcecode import CELL_LANGUAGES
from spyder.utils.workers import WorkerManager

Expand Down Expand Up @@ -294,12 +294,30 @@ def make_python_patterns(additional_keywords=[], additional_builtins=[]):
r"\bcls\b",
(r"^\s*@([a-zA-Z_][a-zA-Z0-9_]*)"
r"(\.[a-zA-Z_][a-zA-Z0-9_]*)*")])
number = any("number",
[r"\b[+-]?[0-9]+[lLjJ]?\b",
r"\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b",
r"\b[+-]?0[oO][0-7]+[lL]?\b",
r"\b[+-]?0[bB][01]+[lL]?\b",
r"\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?[jJ]?\b"])
number_regex = [r"\b[+-]?[0-9]+[lLjJ]?\b",
r"\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b",
r"\b[+-]?0[oO][0-7]+[lL]?\b",
r"\b[+-]?0[bB][01]+[lL]?\b",
r"\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?[jJ]?\b"]
# Needed to achieve correct highlighting in Python 3.6
# See issue 7324
if PY36:
Copy link
Member

@goanpeca goanpeca Jul 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be larger than and not tied to 36 right?

# Based on
# https://github.com/python/cpython/blob/
# 81950495ba2c36056e0ce48fd37d514816c26747/Lib/tokenize.py#L117
# In order: Hexnumber, Binnumber, Octnumber, Decnumber,
# Pointfloat + Exponent, Expfloat, Imagnumber
number_regex = [
r"\b[+-]?0[xX](?:_?[0-9A-Fa-f])+[lL]?\b",
r"\b[+-]?0[bB](?:_?[01])+[lL]?\b",
r"\b[+-]?0[oO](?:_?[0-7])+[lL]?\b",
r"\b[+-]?(?:0(?:_?0)*|[1-9](?:_?[0-9])*)[lL]?\b",
r"\b((\.[0-9](?:_?[0-9])*')|\.[0-9](?:_?[0-9])*)"
"([eE][+-]?[0-9](?:_?[0-9])*)?[jJ]?\b",
r"\b[0-9](?:_?[0-9])*([eE][+-]?[0-9](?:_?[0-9])*)?[jJ]?\b",
r"\b[0-9](?:_?[0-9])*[jJ]\b"]
number = any("number", number_regex)

sqstring = r"(\b[rRuU])?'[^'\\\n]*(\\.[^'\\\n]*)*'?"
dqstring = r'(\b[rRuU])?"[^"\\\n]*(\\.[^"\\\n]*)*"?'
uf_sqstring = r"(\b[rRuU])?'[^'\\\n]*(\\.[^'\\\n]*)*(\\)$(?!')$"
Expand Down