From 7bfe947b313dde98bc7bd37d1e471ee51c1080f8 Mon Sep 17 00:00:00 2001 From: Harald Klimach Date: Mon, 11 Mar 2013 09:35:54 +0100 Subject: [PATCH] Do not try to tokenize non-python files when using Python 3: Issue 1265. --- wscript | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/wscript b/wscript index 09e57868..24053999 100644 --- a/wscript +++ b/wscript @@ -204,28 +204,29 @@ def process_decorators(body): return "\n".join(accu+all_deco) def sfilter(path): - if sys.version_info[0] >= 3 and Options.options.strip_comments: - f = open(path, "rb") - try: - tk = tokenize.tokenize(f.readline) - next(tk) # the first one is always tokenize.ENCODING for Python 3, ignore it - cnt = process_tokens(tk) - finally: - f.close() - elif Options.options.strip_comments and path.endswith('.py'): - f = open(path, "r") - try: - cnt = process_tokens(tokenize.generate_tokens(f.readline)) - finally: - f.close() - else: - f = open(path, "r") - try: - cnt = f.read() - finally: - f.close() if path.endswith('.py') : + if Options.options.strip_comments: + if sys.version_info[0] >= 3: + f = open(path, "rb") + try: + tk = tokenize.tokenize(f.readline) + next(tk) # the first one is always tokenize.ENCODING for Python 3, ignore it + cnt = process_tokens(tk) + finally: + f.close() + else: + f = open(path, "r") + try: + cnt = process_tokens(tokenize.generate_tokens(f.readline)) + finally: + f.close() + else: + f = open(path, "r") + try: + cnt = f.read() + finally: + f.close() # WARNING: since we now require python 2.4, we do not process the decorators anymore # if you need such a thing, uncomment the code below: #cnt = process_decorators(cnt) @@ -233,6 +234,13 @@ def sfilter(path): # cnt = 'import sys\nif sys.hexversion < 0x020400f0: from sets import Set as set\n' + cnt cnt = '#! /usr/bin/env python\n# encoding: utf-8\n# WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file\n\n' + cnt + else: + f = open(path, "r") + try: + cnt = f.read() + finally: + f.close() + if sys.hexversion > 0x030000f0: return (io.BytesIO(cnt.encode('utf-8')), len(cnt), cnt) return (io.BytesIO(cnt), len(cnt), cnt)