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

Py3 compatibility for C preprocessor #83

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions ply/cpp.py
Expand Up @@ -9,6 +9,13 @@
# -----------------------------------------------------------------------------
from __future__ import generators

import sys
if sys.version_info >= (3,):
_string_types = (str,)
else:
_string_types = (str, unicode)
range = xrange

# -----------------------------------------------------------------------------
# Default preprocessor lexer definitions. These tokens are enough to get
# a basic preprocessor working. Other modules may import these if they want
Expand Down Expand Up @@ -271,7 +278,7 @@ def add_path(self,path):
def group_lines(self,input):
lex = self.lexer.clone()
lines = [x.rstrip() for x in input.splitlines()]
for i in xrange(len(lines)):
for i in range(len(lines)):
j = i+1
while lines[i].endswith('\\') and (j < len(lines)):
lines[i] = lines[i][:-1]+lines[j]
Expand Down Expand Up @@ -760,7 +767,9 @@ def include(self,tokens):
for p in path:
iname = os.path.join(p,filename)
try:
data = open(iname,"r").read()
with open(iname,"r") as infile:
data = infile.read()

dname = os.path.dirname(iname)
if dname:
self.temp_path.insert(0,dname)
Expand All @@ -781,7 +790,7 @@ def include(self,tokens):
# ----------------------------------------------------------------------

def define(self,tokens):
if isinstance(tokens,(str,unicode)):
if isinstance(tokens, _string_types):
tokens = self.tokenize(tokens)

linetok = tokens
Expand Down