Progress towards dual Python 2 & 3

This commit is contained in:
Sei Lisa 2020-11-09 02:28:57 +01:00
parent dde9577cea
commit f8cf78dfac
10 changed files with 100 additions and 80 deletions

View file

@ -19,6 +19,7 @@
from lslopt import lslfuncs
from lslopt.lslcommon import nr
from strutil import xrange
class deadcode(object):
@ -530,7 +531,7 @@ class deadcode(object):
self.MarkReferences(statedef)
# Track removal of global lines, to reasign locations later.
LocMap = range(len(self.tree))
LocMap = list(range(len(self.tree)))
GlobalDeletions = []

View file

@ -23,6 +23,7 @@ from lslopt import lslfuncs
from lslopt.lslfuncs import ZERO_VECTOR, ZERO_ROTATION
import math
from lslopt.lslfuncopt import OptimizeFunc, OptimizeArgs, FuncOptSetup
from strutil import xrange, unicode
# TODO: Remove special handling of @ within IF,WHILE,FOR,DO

View file

@ -25,6 +25,7 @@ from lslopt.lslcommon import nr
#import math
#from lslparse import warning
#from lslfuncopt import OptimizeFunc, OptimizeArgs, FuncOptSetup
from strutil import xrange
class rec:
def __init__(self, **init):

View file

@ -20,6 +20,7 @@
import sys, re
from lslopt.lslcommon import types, warning, Vector, Quaternion
from lslopt import lslcommon, lslfuncs
from strutil import *
def LoadLibrary(builtins = None, fndata = None):
"""Load builtins.txt and fndata.txt (or the given filenames) and return
@ -40,27 +41,27 @@ def LoadLibrary(builtins = None, fndata = None):
# Library read code
parse_lin_re = re.compile(
br'^\s*([a-z]+)\s+'
br'([a-zA-Z_][a-zA-Z0-9_]*)\s*\(\s*('
br'[a-z]+\s+[a-zA-Z_][a-zA-Z0-9_]*'
br'(?:\s*,\s*[a-z]+\s+[a-zA-Z_][a-zA-Z0-9_]*)*'
br')?\s*\)\s*$'
br'|'
br'^\s*const\s+([a-z]+)'
br'\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.*?)\s*$'
br'|'
br'^\s*(?:#.*|//.*)?$')
parse_arg_re = re.compile(br'^\s*([a-z]+)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*$')
parse_fp_re = re.compile(br'^\s*(-?(?=[0-9]|\.[0-9])[0-9]*'
br'((?:\.[0-9]*)?(?:[Ee][+-]?[0-9]+)?))\s*$')
parse_int_re = re.compile(br'^\s*(-?0x[0-9A-Fa-f]+|-?[0-9]+)\s*$')
r'^\s*([a-z]+)\s+'
r'([a-zA-Z_][a-zA-Z0-9_]*)\s*\(\s*('
r'[a-z]+\s+[a-zA-Z_][a-zA-Z0-9_]*'
r'(?:\s*,\s*[a-z]+\s+[a-zA-Z_][a-zA-Z0-9_]*)*'
r')?\s*\)\s*$'
r'|'
r'^\s*const\s+([a-z]+)'
r'\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.*?)\s*$'
r'|'
r'^\s*(?:#.*|//.*)?$')
parse_arg_re = re.compile(r'^\s*([a-z]+)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*$')
parse_fp_re = re.compile(r'^\s*(-?(?=[0-9]|\.[0-9])[0-9]*'
r'((?:\.[0-9]*)?(?:[Ee][+-]?[0-9]+)?))\s*$')
parse_int_re = re.compile(r'^\s*(-?0x[0-9A-Fa-f]+|-?[0-9]+)\s*$')
parse_str_re = re.compile(u'^"((?:[^"\\\\]|\\\\.)*)"$')
f = open(builtins, 'rb')
f = open(builtins, 'r')
try:
linenum = 0
try:
ubuiltins = builtins.decode(sys.getfilesystemencoding())
ubuiltins = str2u(builtins, sys.getfilesystemencoding())
except UnicodeDecodeError:
# This is just a guess at the filename encoding.
ubuiltins = builtins.decode('iso-8859-15')
@ -70,7 +71,7 @@ def LoadLibrary(builtins = None, fndata = None):
if not line: break
if line[-1] == '\n': line = line[:-1]
try:
uline = line.decode('utf8')
uline = str2u(line, 'utf8')
except UnicodeDecodeError:
warning(u"Bad Unicode in %s line %d" % (ubuiltins, linenum))
continue
@ -153,7 +154,7 @@ def LoadLibrary(builtins = None, fndata = None):
elif typ == 'float':
value = lslfuncs.F32(float(value))
elif typ == 'string':
value = value.decode('utf8')
value = str2u(value, 'utf8')
if parse_str_re.search(value):
esc = False
tmp = value[1:-1]
@ -242,14 +243,14 @@ def LoadLibrary(builtins = None, fndata = None):
# TODO: "quaternion" doesn't compare equal to "rotation" even if they are
# equivalent. Canonicalize it before comparison, to avoid false
# reports of mismatches.
f = open(fndata, 'rb')
f = open(fndata, 'r')
try:
linenum = 0
curr_fn = None
curr_ty = None
skipping = False
try:
ufndata = fndata.decode(sys.getfilesystemencoding())
ufndata = str2u(fndata, sys.getfilesystemencoding())
except UnicodeDecodeError:
# This is just a guess at the filename encoding.
ufndata = fndata.decode('iso-8859-15')
@ -259,7 +260,7 @@ def LoadLibrary(builtins = None, fndata = None):
if not line: break
if line[-1] == '\n': line = line[:-1]
try:
uline = line.decode('utf8')
uline = str2u(line, 'utf8')
except UnicodeDecodeError:
warning(u"Bad Unicode in %s line %d" % (ufndata, linenum))
continue
@ -272,7 +273,7 @@ def LoadLibrary(builtins = None, fndata = None):
if match_fn and (rettype in ('void', 'event') or rettype in types):
skipping = True # until proven otherwise
name = match_fn.group(2)
uname = name.decode('utf8')
uname = str2u(name, 'utf8')
if (rettype == 'event' and name not in events
or rettype != 'event' and name not in functions
):
@ -347,7 +348,7 @@ def LoadLibrary(builtins = None, fndata = None):
skipping = True
continue
if not skipping:
ucurr_fn = curr_fn.decode('utf8')
ucurr_fn = str2u(curr_fn, 'utf8')
if match_flag.group(1):
# SEF
# We don't handle conditions yet. Take the
@ -438,7 +439,7 @@ def LoadLibrary(builtins = None, fndata = None):
# Post-checks
for i in functions:
ui = i.decode('utf8')
ui = str2u(i, 'utf8')
if 'NeedsData' in functions[i]:
del functions[i]['NeedsData']
warning(u"Library data, file %s: Function %s has no data."
@ -455,7 +456,7 @@ def LoadLibrary(builtins = None, fndata = None):
u" delay. Removing SEF." % ui)
del functions[i]['SEF']
for i in events:
ui = i.decode('utf8')
ui = str2u(i, 'utf8')
if 'NeedsData' in events[i]:
del events[i]['NeedsData']
warning(u"Library data, file %s: Event %s has no data."

View file

@ -21,6 +21,7 @@ from lslopt import lslfuncs
from lslopt import lslcommon
from lslopt.lslcommon import Key, Vector, Quaternion, warning
from math import copysign
from strutil import *
debugScopes = False
@ -62,7 +63,7 @@ class outscript(object):
" spaces by the viewer when copy-pasting the code"
" (disable this warning by disabling the 'warntabs'"
" option).")
return pfx + '"' + value.encode('utf8').replace('\\','\\\\') \
return pfx + '"' + any2str(value, 'utf8').replace('\\','\\\\') \
.replace('"','\\"').replace('\n','\\n') + '"' + sfx
if tvalue == int:
if value < 0 and not self.globalmode and self.optsigns:

View file

@ -29,6 +29,10 @@ import re
# Note this module was basically written from bottom to top, which may help
# reading it.
WHITESPACE_CHARS = frozenset({' ', '\r', '\n', '\x0B', '\x0C'})
SINGLE_SYMBOLS = frozenset({'.', ';', '{', '}', ',', '=', '(', ')', '-', '+',
'*', '/', '%', '@', ':', '<', '>', '[', ']', '&', '|', '^', '~', '!'})
def isdigit(c):
return '0' <= c <= '9'
@ -48,7 +52,7 @@ def GetErrLineCol(parser):
# Find start of current line
lstart = parser.script.rfind('\n', 0, errorpos) + 1
# Find zero-based column number in characters
cno = len(parser.script[lstart:errorpos].decode('utf8'))
cno = len(any2u(parser.script[lstart:errorpos], 'utf8'))
# Find in #line directives list
i = len(parser.linedir)
filename = '<stdin>' # value to return if there's no #line before lno
@ -75,7 +79,7 @@ class EParse(Exception):
if parser.emap and filename == '<stdin>':
filename = parser.filename
filename = (filename.decode('utf8', 'replace')
filename = (str2u(filename, 'utf8')
.replace(u'\\', u'\\\\')
.replace(u'"', u'\\"')
)
@ -543,7 +547,7 @@ class parser(object):
# self.linestart is related to the preprocessor, therefore we
# check the characters that are relevant for standard C.
if c not in ' \n\r\x0B\x0C':
if c not in WHITESPACE_CHARS:
self.linestart = False
# Process strings
@ -584,7 +588,7 @@ class parser(object):
if is_string:
self.pos += 1
return ('STRING_VALUE', lslfuncs.zstr(strliteral.decode('utf8')))
return ('STRING_VALUE', lslfuncs.zstr(str2u(strliteral, 'utf8')))
# fall through (to consider the L or to ignore the ")
if isalpha_(c):
@ -705,7 +709,7 @@ class parser(object):
return (self.script[self.pos-3:self.pos],)
return (self.script[self.pos-2:self.pos],)
if c in '.;{},=()-+*/%@:<>[]&|^~!' and c != '':
if c in SINGLE_SYMBOLS:
return (c,)
if c == '\n':
@ -2801,8 +2805,7 @@ list lazy_list_set(list L, integer i, list v)
self.filename = filename
if type(script) is unicode:
script = script.encode('utf8')
script = any2str(script, 'utf8')
self.script = script
self.length = len(script)

View file

@ -23,6 +23,8 @@
#
# A side effect of this change is that the script becomes unreadable gibberish.
from strutil import xrange
class renamer(object):
CharSet1 = '_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
CharSet2 = '0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'