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

Python 2-3 compatible utility scripts #530

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
12 changes: 6 additions & 6 deletions test/language/ignore_carriage_returns.wren
@@ -1,6 +1,6 @@
// Sprinkle some carriage returns to ensure they are ignored anywhere:
System.print("one")
System.print("two")
// Generate a compile error so we can test that the line number is correct.
+ * // expect error
// Sprinkle some carriage returns to ensure they are ignored anywhere:
System.print("one")
System.print("two")

// Generate a compile error so we can test that the line number is correct.
+ * // expect error

Choose a reason for hiding this comment

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

I think this file has been commited by mistake.

Copy link
Contributor Author

@jclc jclc May 4, 2018

Choose a reason for hiding this comment

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

I believe it used to have carriage returns right before newlines, which confused Python into thinking they were CRLF-newlines in utils/test.py. Since then I made it open the files as binary to circumvent that, so I suppose it doesn't really make much of a difference.

Looking at the original file now, my editor may have converted the CRs to CRLFs on its own before I checked it with a hex editor.

Expand Down
1 change: 0 additions & 1 deletion util/generate_docs.py
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

import codecs
import glob
import fnmatch
import os
import posixpath
Expand Down
5 changes: 3 additions & 2 deletions util/metrics.py
Expand Up @@ -2,9 +2,9 @@

from __future__ import print_function

import codecs
import glob
import fnmatch
import itertools
import os
import re

Expand Down Expand Up @@ -86,7 +86,8 @@ def wren_metrics(label, directories):
for file_name in fnmatch.filter(file_names, "*.wren"):
num_files += 1

with open(os.path.join(dir_path, file_name), "r") as input:
# NOTE: In the old Python 2 version of the script, files were opened as ISO-8859-1
with codecs.open(os.path.join(dir_path, file_name), "r", encoding="utf-8", errors="ignore") as input:
for line in input:
if line.strip() == "":
num_empty += 1
Expand Down
23 changes: 16 additions & 7 deletions util/test.py
Expand Up @@ -2,10 +2,11 @@

from __future__ import print_function

from io import open
from argparse import ArgumentParser
from collections import defaultdict
from os import listdir
from os.path import abspath, basename, dirname, isdir, isfile, join, realpath, relpath, splitext
from os.path import abspath, dirname, isdir, join, realpath, relpath, splitext
import re
from subprocess import Popen, PIPE
import sys
Expand Down Expand Up @@ -63,8 +64,16 @@ def parse(self):

input_lines = []
line_num = 1
with open(self.path, 'r') as file:
for line in file:
with open(self.path, 'rb') as file:

# NOTE: Using Unix-newlines only. Python 3 will otherwise interpret \r as a
# newline, causing test/language/ignore_carriage_returns.wren to fail.
for line in file.read().split(b'\n'):
try:
line = line.decode("utf-8")
except:
line = line.decode("latin-1")

match = EXPECT_PATTERN.search(line)
if match:
self.output.append((match.group(1), line_num))
Expand Down Expand Up @@ -112,7 +121,6 @@ def parse(self):

line_num += 1


# If any input is fed to the test in stdin, concatenate it into one string.
if input_lines:
self.input_bytes = "\n".join(input_lines).encode("utf-8")
Expand Down Expand Up @@ -249,8 +257,6 @@ def validate_output(self, out):

index = 0
for line in out_lines:
if sys.version_info < (3, 0):
line = line.encode('utf-8')

if index >= len(self.output):
self.fail('Got output "{0}" when none was expected.', line)
Expand All @@ -267,7 +273,10 @@ def validate_output(self, out):

def fail(self, message, *args):
if args:
message = message.format(*args)
try:
message = (message).format(*args)
except:
print("Message:", message, "Args:", args)
self.failures.append(message)


Expand Down
2 changes: 0 additions & 2 deletions util/util.py
@@ -1,7 +1,5 @@
# Utility functions used by other Python files in this directory.

from __future__ import print_function

import os
import os.path
import platform
Expand Down
2 changes: 0 additions & 2 deletions util/wren_to_c_string.py
Expand Up @@ -2,9 +2,7 @@
# coding: utf-8

import argparse
import glob
import os.path
import re

# The source for the Wren modules that are built into the VM or CLI are turned
# include C string literals. This way they can be compiled directly into the
Expand Down