Skip to content

Commit 4240a90

Browse files
committed
gyb: make SyntaxSupport python 3 friendly
This adjusts the code to run identically under python 2 and python 3. We would previously fail to digest the content in Python 3 as the `map` is not equivalent. This now results in the same encoding as Python 2.
1 parent 10d15b8 commit 4240a90

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

utils/gyb_syntax_support/__init__.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -144,34 +144,36 @@ def dedented_lines(description):
144144
return textwrap.dedent(description).split('\n')
145145

146146

147-
def digest_syntax_node(digest, node):
148-
# Hash into the syntax name and serialization code
149-
digest.update(node.name)
150-
digest.update(str(get_serialization_code(node.syntax_kind)))
151-
for child in node.children:
152-
# Hash into the expected child syntax
153-
digest.update(child.syntax_kind)
154-
# Hash into the child name
155-
digest.update(child.name)
156-
# Hash into whether the child is optional
157-
digest.update(str(child.is_optional))
158-
159-
160-
def digest_syntax_token(digest, token):
161-
# Hash into the token name and serialization code
162-
digest.update(token.name)
163-
digest.update(str(token.serialization_code))
164-
165-
166-
def digest_trivia(digest, trivia):
167-
digest.update(trivia.name)
168-
digest.update(str(trivia.serialization_code))
169-
digest.update(str(trivia.characters))
170-
171-
172147
def calculate_node_hash():
173148
digest = hashlib.sha1()
174-
map(lambda node: digest_syntax_node(digest, node), SYNTAX_NODES)
175-
map(lambda token: digest_syntax_token(digest, token), SYNTAX_TOKENS)
176-
map(lambda trivia: digest_trivia(digest, trivia), TRIVIAS)
149+
150+
def _digest_syntax_node(node):
151+
# Hash into the syntax name and serialization code
152+
digest.update(node.name.encode("utf-8"))
153+
digest.update(str(get_serialization_code(node.syntax_kind)).encode("utf-8"))
154+
for child in node.children:
155+
# Hash into the expected child syntax
156+
digest.update(child.syntax_kind.encode("utf-8"))
157+
# Hash into the child name
158+
digest.update(child.name.encode("utf-8"))
159+
# Hash into whether the child is optional
160+
digest.update(str(child.is_optional).encode("utf-8"))
161+
162+
def _digest_syntax_token(token):
163+
# Hash into the token name and serialization code
164+
digest.update(token.name.encode("utf-8"))
165+
digest.update(str(token.serialization_code).encode("utf-8"))
166+
167+
def _digest_trivia(trivia):
168+
digest.update(trivia.name.encode("utf-8"))
169+
digest.update(str(trivia.serialization_code).encode("utf-8"))
170+
digest.update(str(trivia.characters).encode("utf-8"))
171+
172+
for node in SYNTAX_NODES:
173+
_digest_syntax_node(node)
174+
for token in SYNTAX_TOKENS:
175+
_digest_syntax_token(token)
176+
for trivia in TRIVIAS:
177+
_digest_trivia(trivia)
178+
177179
return digest.hexdigest()

0 commit comments

Comments
 (0)