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

Add Python 3 support for WebIDL parser #23723

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add Python 3 support for WebIDL parser

  • Loading branch information
saschanaz committed Jul 6, 2019
commit fb54e18318b3779eb3e04a54425c3bd16750385c
@@ -10,7 +10,7 @@
import traceback
import math
import string
from collections import defaultdict
from collections import defaultdict, OrderedDict

# Machinery

@@ -40,32 +40,22 @@ def parseInt(literal):
return value * sign


# Magic for creating enums
def M_add_class_attribs(attribs, start):
def foo(name, bases, dict_):
for v, k in enumerate(attribs):
dict_[k] = start + v
assert 'length' not in dict_
dict_['length'] = start + len(attribs)
return type(name, bases, dict_)
return foo


def enum(*names, **kw):
if len(kw) == 1:
base = kw['base'].__class__
start = base.length
else:
assert len(kw) == 0
base = object
start = 0

class Foo(base):
__metaclass__ = M_add_class_attribs(names, start)

class Foo(object):
attrs = OrderedDict()
def __init__(self, names):
for v, k in enumerate(names):
self.attrs[k] = v
def __getattr__(self, attr):
if attr in self.attrs:
return self.attrs[attr]
return getattr(self, attr)
def __setattr__(self, name, value): # this makes it read-only
raise NotImplementedError
return Foo()

if "base" not in kw:
return Foo(names)
return Foo(list(kw["base"].attrs.keys()) + list(names))


class WebIDLError(Exception):
@@ -962,7 +952,6 @@ def finish(self, scope):
# self.members. Sort our consequential interfaces by name
# just so we have a consistent order.
for iface in sorted(self.getConsequentialInterfaces(),
cmp=cmp,
key=lambda x: x.identifier.name):
# Flag the interface as being someone's consequential interface
iface.setIsConsequentialInterfaceOf(self)
@@ -1877,7 +1866,7 @@ def finish(self, scope):
assert member.type.isComplete()

# Members of a dictionary are sorted in lexicographic order
self.members.sort(cmp=cmp, key=lambda x: x.identifier.name)
self.members.sort(key=lambda x: x.identifier.name)

inheritedMembers = []
ancestor = self.parent
@@ -2232,7 +2221,7 @@ def complete(self, scope):

assert obj
if obj.isType():
print obj
print(obj)
assert not obj.isType()
if obj.isTypedef():
assert self.name.name == obj.identifier.name
@@ -3689,7 +3678,7 @@ def getExtendedAttribute(self, name):
def finish(self, scope):
# We better be exposed _somewhere_.
if (len(self._exposureGlobalNames) == 0):
print self.identifier.name
print(self.identifier.name)
assert len(self._exposureGlobalNames) != 0
IDLExposureMixins.finish(self, scope)

@@ -5637,7 +5626,7 @@ def handleNonPartialObject(self, location, identifier, constructor,
[location, existingObj.location])
existingObj.setNonPartial(*nonPartialArgs)
return existingObj
except Exception, ex:
except Exception as ex:
if isinstance(ex, WebIDLError):
raise ex
pass
@@ -5675,7 +5664,7 @@ def p_InterfaceForwardDecl(self, p):
"%s and %s" % (identifier.name, p[0]),
[location, p[0].location])
return
except Exception, ex:
except Exception as ex:
if isinstance(ex, WebIDLError):
raise ex
pass
@@ -5739,7 +5728,7 @@ def handlePartialObject(self, location, identifier, nonPartialConstructor,
"non-%s object" %
(prettyname, prettyname),
[location, nonPartialObject.location])
except Exception, ex:
except Exception as ex:
if isinstance(ex, WebIDLError):
raise ex
pass
@@ -7040,8 +7029,8 @@ def __init__(self, outputdir='', lexer=None):
def _installBuiltins(self, scope):
assert isinstance(scope, IDLScope)

# xrange omits the last value.
for x in xrange(IDLBuiltinType.Types.ArrayBuffer, IDLBuiltinType.Types.Float64Array + 1):
# range omits the last value.
for x in range(IDLBuiltinType.Types.ArrayBuffer, IDLBuiltinType.Types.Float64Array + 1):
builtin = BuiltinTypes[x]
name = builtin.name
typedef = IDLTypedef(BuiltinLocation("<builtin type>"), scope, builtin, name)
@@ -7185,14 +7174,14 @@ def main():
f = open(fullPath, 'rb')
lines = f.readlines()
f.close()
print fullPath
print(fullPath)
parser.parse(''.join(lines), fullPath)
parser.finish()
except WebIDLError, e:
except WebIDLError as e:
if options.verbose_errors:
traceback.print_exc()
else:
print e
print(e)

if __name__ == '__main__':
main()
@@ -62,7 +62,7 @@ def run_tests(tests, verbose):
harness.start()
try:
_test.WebIDLTest.__call__(WebIDL.Parser(), harness)
except Exception, ex:
except Exception as ex:
print("TEST-UNEXPECTED-FAIL | Unhandled exception in test %s: %s" % (testpath, ex))
traceback.print_exc()
finally:
@@ -133,7 +133,7 @@ def checkAttr(attr, QName, name, type, readonly):
};
""")
results = parser.finish()
except Exception, x:
except:
threw = True
harness.ok(threw, "Should not allow [SetterThrows] on readonly attributes")

@@ -146,7 +146,7 @@ def checkAttr(attr, QName, name, type, readonly):
};
""")
results = parser.finish()
except Exception, x:
except:
threw = True
harness.ok(threw, "Should spell [Throws] correctly")

@@ -159,7 +159,7 @@ def checkAttr(attr, QName, name, type, readonly):
};
""")
results = parser.finish()
except Exception, x:
except:
threw = True
harness.ok(threw, "Should not allow [SameObject] on attributes not of interface type")

@@ -172,6 +172,6 @@ def checkAttr(attr, QName, name, type, readonly):
};
""")
results = parser.finish()
except Exception, x:
except:
threw = True
harness.ok(not threw, "Should allow [SameObject] on attributes of interface type")
@@ -38,7 +38,7 @@ def WebIDLTest(parser, harness):
""")

results = parser.finish()
except Exception, e:
except Exception as e:
harness.ok(False, "Shouldn't have thrown for [CEReactions] used on writable attribute. %s" % e)
threw = True

@@ -52,7 +52,7 @@ def WebIDLTest(parser, harness):
""")

results = parser.finish()
except Exception, e:
except Exception as e:
harness.ok(False, "Shouldn't have thrown for [CEReactions] used on regular operations. %s" % e)
threw = True

@@ -44,7 +44,7 @@ def WebIDLTest(parser, harness):
};
""")
results = parser.finish()
except Exception, exception:
except Exception as exception:
pass

harness.ok(exception, "Should have thrown.")
@@ -70,7 +70,7 @@ def WebIDLTest(parser, harness):
};
""")
results = parser.finish()
except Exception, exception:
except Exception as exception:
pass

harness.ok(exception, "Should have thrown (2).")
@@ -100,7 +100,7 @@ def WebIDLTest(parser, harness):
};
""")
results = parser.finish()
except Exception, exception:
except Exception as exception:
pass

harness.ok(exception, "Should have thrown (3).")
@@ -10,7 +10,7 @@ def WebIDLTest(parser, harness):
""")

results = parser.finish()
except Exception,x:
except:
threw = True

harness.ok(threw, "Constant cannot have [] as a default value")
@@ -8,7 +8,7 @@ def WebIDLTest(parser, harness):
try:
parser.parse(input)
results = parser.finish()
except WebIDL.WebIDLError, e:
except WebIDL.WebIDLError as e:
threw = True
lines = str(e).split('\n')

@@ -14,7 +14,7 @@ def WebIDLTest(parser, harness):
try:
parser.parse(input)
results = parser.finish()
except WebIDL.WebIDLError, e:
except WebIDL.WebIDLError as e:
threw = True
lines = str(e).split('\n')

@@ -124,7 +124,7 @@ def WebIDLTest(parser, harness):
""")

results = parser.finish()
except Exception,x:
except:
threw = True

harness.ok(threw, "Should have thrown on invalid Exposed value on interface.")
@@ -140,7 +140,7 @@ def WebIDLTest(parser, harness):
""")

results = parser.finish()
except Exception,x:
except:
threw = True

harness.ok(threw, "Should have thrown on invalid Exposed value on attribute.")
@@ -156,7 +156,7 @@ def WebIDLTest(parser, harness):
""")

results = parser.finish()
except Exception,x:
except:
threw = True

harness.ok(threw, "Should have thrown on invalid Exposed value on operation.")
@@ -172,7 +172,7 @@ def WebIDLTest(parser, harness):
""")

results = parser.finish()
except Exception,x:
except:
threw = True

harness.ok(threw, "Should have thrown on invalid Exposed value on constant.")
@@ -192,7 +192,7 @@ def WebIDLTest(parser, harness):
""")

results = parser.finish()
except Exception,x:
except:
threw = True

harness.ok(threw, "Should have thrown on member exposed where its interface is not.")
@@ -216,7 +216,7 @@ def WebIDLTest(parser, harness):
""")

results = parser.finish()
except Exception,x:
except:
threw = True

harness.ok(threw, "Should have thrown on LHS of implements being exposed where RHS is not.")
@@ -68,7 +68,7 @@ def WebIDLTest(parser, harness):
long m(float arg);
};
""")
except Exception, x:
except:
threw = True
harness.ok(threw, "[LenientFloat] only allowed on void methods")

@@ -81,7 +81,7 @@ def WebIDLTest(parser, harness):
void m(unrestricted float arg);
};
""")
except Exception, x:
except:
threw = True
harness.ok(threw, "[LenientFloat] only allowed on methods with unrestricted float args")

@@ -94,7 +94,7 @@ def WebIDLTest(parser, harness):
void m(sequence<unrestricted float> arg);
};
""")
except Exception, x:
except:
threw = True
harness.ok(threw, "[LenientFloat] only allowed on methods with unrestricted float args (2)")

@@ -107,7 +107,7 @@ def WebIDLTest(parser, harness):
void m((unrestricted float or FloatTypes) arg);
};
""")
except Exception, x:
except:
threw = True
harness.ok(threw, "[LenientFloat] only allowed on methods with unrestricted float args (3)")

@@ -120,6 +120,6 @@ def WebIDLTest(parser, harness):
readonly attribute float foo;
};
""")
except Exception, x:
except:
threw = True
harness.ok(threw, "[LenientFloat] only allowed on writable attributes")
@@ -9,7 +9,7 @@ def WebIDLTest(parser, harness):
""")
results = parser.finish()
harness.ok(False, "Should fail to parse")
except Exception, e:
except Exception as e:
harness.ok("Name collision" in e.message,
"Should have name collision for interface")

@@ -21,7 +21,7 @@ def WebIDLTest(parser, harness):
""")
results = parser.finish()
harness.ok(False, "Should fail to parse")
except Exception, e:
except Exception as e:
harness.ok("Name collision" in e.message,
"Should have name collision for dictionary")

@@ -33,7 +33,7 @@ def WebIDLTest(parser, harness):
""")
results = parser.finish()
harness.ok(False, "Should fail to parse")
except Exception, e:
except Exception as e:
harness.ok("Multiple unresolvable definitions" in e.message,
"Should have name collision for dictionary")

@@ -37,10 +37,10 @@ def shouldFail(prefix, iface):
p.finish()
harness.ok(False,
prefix + " - Interface passed when should've failed")
except WebIDL.WebIDLError, e:
except WebIDL.WebIDLError as e:
harness.ok(True,
prefix + " - Interface failed as expected")
except Exception, e:
except Exception as e:
harness.ok(False,
prefix + " - Interface failed but not as a WebIDLError exception: %s" % e)

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.