Skip to content

Commit

Permalink
gyp: use keys in calls to sorted (#106)
Browse files Browse the repository at this point in the history
This replaces uses of the cmp parameter of sorted (which was removed in
Python 3) with either the key parameter, or no parameter where key
seems redundant. This also encodes Unicode strings before hashing them
(which is needed in Python 3).

Fixes: nodejs/node-gyp#2101
  • Loading branch information
nwhetsell authored May 13, 2021
1 parent da1aa91 commit 7c80b08
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pylib/gyp/xcodeproj_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@
"""

import gyp.common
from functools import cmp_to_key
import hashlib
from operator import attrgetter
import posixpath
import re
import struct
Expand Down Expand Up @@ -423,6 +425,8 @@ def _HashUpdate(hash, data):
"""

hash.update(struct.pack(">i", len(data)))
if isinstance(data, str):
data = data.encode("utf-8")
hash.update(data)

if seed_hash is None:
Expand Down Expand Up @@ -1483,7 +1487,7 @@ def TakeOverOnlyChild(self, recurse=False):

def SortGroup(self):
self._properties["children"] = sorted(
self._properties["children"], cmp=lambda x, y: x.Compare(y)
self._properties["children"], key=cmp_to_key(lambda x, y: x.Compare(y))
)

# Recurse.
Expand Down Expand Up @@ -2891,7 +2895,7 @@ def SortGroups(self):
# according to their defined order.
self._properties["mainGroup"]._properties["children"] = sorted(
self._properties["mainGroup"]._properties["children"],
cmp=lambda x, y: x.CompareRootGroup(y),
key=cmp_to_key(lambda x, y: x.CompareRootGroup(y)),
)

# Sort everything else by putting group before files, and going
Expand Down Expand Up @@ -2986,9 +2990,7 @@ def AddOrGetProjectReference(self, other_pbxproject):
# Xcode seems to sort this list case-insensitively
self._properties["projectReferences"] = sorted(
self._properties["projectReferences"],
cmp=lambda x, y: cmp(
x["ProjectRef"].Name().lower(), y["ProjectRef"].Name().lower()
),
key=lambda x: x["ProjectRef"].Name().lower
)
else:
# The link already exists. Pull out the relevnt data.
Expand Down Expand Up @@ -3120,7 +3122,7 @@ def CompareProducts(x, y, remote_products):
product_group = ref_dict["ProductGroup"]
product_group._properties["children"] = sorted(
product_group._properties["children"],
cmp=lambda x, y, rp=remote_products: CompareProducts(x, y, rp),
key=cmp_to_key(lambda x, y, rp=remote_products: CompareProducts(x, y, rp)),
)


Expand Down Expand Up @@ -3155,7 +3157,7 @@ def Print(self, file=sys.stdout):
else:
self._XCPrint(file, 0, "{\n")
for property, value in sorted(
self._properties.items(), cmp=lambda x, y: cmp(x, y)
self._properties.items()
):
if property == "objects":
self._PrintObjects(file)
Expand Down Expand Up @@ -3183,7 +3185,7 @@ def _PrintObjects(self, file):
self._XCPrint(file, 0, "\n")
self._XCPrint(file, 0, "/* Begin " + class_name + " section */\n")
for object in sorted(
objects_by_class[class_name], cmp=lambda x, y: cmp(x.id, y.id)
objects_by_class[class_name], key=attrgetter("id")
):
object.Print(file)
self._XCPrint(file, 0, "/* End " + class_name + " section */\n")
Expand Down

0 comments on commit 7c80b08

Please sign in to comment.