Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/pythonparser
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 15ad1e88f1e8
Choose a base ref
...
head repository: m-labs/pythonparser
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 776188b42cf7
Choose a head ref
  • 2 commits
  • 14 files changed
  • 1 contributor

Commits on Jun 5, 2015

  1. Copy the full SHA
    56e5b7f View commit details
  2. Replace single-quoted strings with double-quoted.

    whitequark committed Jun 5, 2015
    Copy the full SHA
    776188b View commit details
6 changes: 3 additions & 3 deletions doc/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
master_doc = 'index'
master_doc = "index"

extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
]
15 changes: 8 additions & 7 deletions examples/quot_to_dquot.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import sys, re
from pythonparser import source, lexer
from pythonparser import source, lexer, diagnostic

buf = None
with open(sys.argv[1]) as f:
buf = source.Buffer(f.read(), f.name)

engine = diagnostic.Engine()
rewriter = source.Rewriter(buf)
in_quot = False
replace = { "'": '"', "'''": '"""' }
for token in lexer.Lexer(buf, (3, 4)):
replace = { "'": "\"", "'''": "\"\"\"" }
for token in lexer.Lexer(buf, (3, 4), engine):
source = token.loc.source()
if token.kind == 'strbegin' and source in replace.keys():
if token.kind == "strbegin" and source in replace.keys():
rewriter.replace(token.loc, replace[source])
in_quot = True
elif token.kind == 'strdata' and in_quot:
elif token.kind == "strdata" and in_quot:
rewriter.replace(token.loc, re.sub(r'([^\\"]|)"', r'\1\\"', source))
elif token.kind == 'strend' and in_quot:
elif token.kind == "strend" and in_quot:
rewriter.replace(token.loc, replace[source])
in_quot = False

buf = rewriter.rewrite()

with open(sys.argv[1], 'w') as f:
with open(sys.argv[1], "w") as f:
f.write(buf.source)
12 changes: 6 additions & 6 deletions pythonparser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import sys, pythonparser.source, pythonparser.lexer, pythonparser.parser, pythonparser.diagnostic

def parse_buffer(buffer, mode='exec', flags=[], version=None, engine=None):
def parse_buffer(buffer, mode="exec", flags=[], version=None, engine=None):
"""
Like :meth:`parse`, but accepts a :class:`source.Buffer` instead of
source and filename."""
@@ -13,20 +13,20 @@ def parse_buffer(buffer, mode='exec', flags=[], version=None, engine=None):
engine = pythonparser.diagnostic.Engine()

lexer = pythonparser.lexer.Lexer(buffer, version, engine)
if mode in ('single', 'eval'):
if mode in ("single", "eval"):
lexer.interactive = True

parser = pythonparser.parser.Parser(lexer, version, engine)
parser.add_flags(flags)

if mode == 'exec':
if mode == "exec":
return parser.file_input()
elif mode == 'single':
elif mode == "single":
return parser.single_input()
elif mode == 'eval':
elif mode == "eval":
return parser.eval_input()

def parse(source, filename='<unknown>', mode='exec',
def parse(source, filename="<unknown>", mode="exec",
flags=[], version=None, engine=None):
"""
Parse a string into an abstract syntax tree.
2 changes: 1 addition & 1 deletion pythonparser/__main__.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
import sys, time, codecs

for filename in sys.argv[1:]:
with codecs.open(filename, encoding='utf-8') as f:
with codecs.open(filename, encoding="utf-8") as f:
input = f.read()
try:
start = time.time()
4 changes: 2 additions & 2 deletions pythonparser/algorithm.py
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ def generic_visit(self, node):
self.visit(getattr(node, field_name))

def _visit_one(self, node):
visit_attr = 'visit_' + type(node).__name__
visit_attr = "visit_" + type(node).__name__
if hasattr(self, visit_attr):
getattr(self, visit_attr)(node)
else:
@@ -70,7 +70,7 @@ def generic_visit(self, node):
return node

def _visit_one(self, node):
visit_attr = 'visit_' + type(node).__name__
visit_attr = "visit_" + type(node).__name__
if hasattr(self, visit_attr):
return getattr(self, visit_attr)(node)
else:
Loading