Skip to content

Commit 7c80b08

Browse files
authored
gyp: use keys in calls to sorted (#106)
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
1 parent da1aa91 commit 7c80b08

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

pylib/gyp/xcodeproj_file.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@
138138
"""
139139

140140
import gyp.common
141+
from functools import cmp_to_key
141142
import hashlib
143+
from operator import attrgetter
142144
import posixpath
143145
import re
144146
import struct
@@ -423,6 +425,8 @@ def _HashUpdate(hash, data):
423425
"""
424426

425427
hash.update(struct.pack(">i", len(data)))
428+
if isinstance(data, str):
429+
data = data.encode("utf-8")
426430
hash.update(data)
427431

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

14841488
def SortGroup(self):
14851489
self._properties["children"] = sorted(
1486-
self._properties["children"], cmp=lambda x, y: x.Compare(y)
1490+
self._properties["children"], key=cmp_to_key(lambda x, y: x.Compare(y))
14871491
)
14881492

14891493
# Recurse.
@@ -2891,7 +2895,7 @@ def SortGroups(self):
28912895
# according to their defined order.
28922896
self._properties["mainGroup"]._properties["children"] = sorted(
28932897
self._properties["mainGroup"]._properties["children"],
2894-
cmp=lambda x, y: x.CompareRootGroup(y),
2898+
key=cmp_to_key(lambda x, y: x.CompareRootGroup(y)),
28952899
)
28962900

28972901
# Sort everything else by putting group before files, and going
@@ -2986,9 +2990,7 @@ def AddOrGetProjectReference(self, other_pbxproject):
29862990
# Xcode seems to sort this list case-insensitively
29872991
self._properties["projectReferences"] = sorted(
29882992
self._properties["projectReferences"],
2989-
cmp=lambda x, y: cmp(
2990-
x["ProjectRef"].Name().lower(), y["ProjectRef"].Name().lower()
2991-
),
2993+
key=lambda x: x["ProjectRef"].Name().lower
29922994
)
29932995
else:
29942996
# The link already exists. Pull out the relevnt data.
@@ -3120,7 +3122,7 @@ def CompareProducts(x, y, remote_products):
31203122
product_group = ref_dict["ProductGroup"]
31213123
product_group._properties["children"] = sorted(
31223124
product_group._properties["children"],
3123-
cmp=lambda x, y, rp=remote_products: CompareProducts(x, y, rp),
3125+
key=cmp_to_key(lambda x, y, rp=remote_products: CompareProducts(x, y, rp)),
31243126
)
31253127

31263128

@@ -3155,7 +3157,7 @@ def Print(self, file=sys.stdout):
31553157
else:
31563158
self._XCPrint(file, 0, "{\n")
31573159
for property, value in sorted(
3158-
self._properties.items(), cmp=lambda x, y: cmp(x, y)
3160+
self._properties.items()
31593161
):
31603162
if property == "objects":
31613163
self._PrintObjects(file)
@@ -3183,7 +3185,7 @@ def _PrintObjects(self, file):
31833185
self._XCPrint(file, 0, "\n")
31843186
self._XCPrint(file, 0, "/* Begin " + class_name + " section */\n")
31853187
for object in sorted(
3186-
objects_by_class[class_name], cmp=lambda x, y: cmp(x.id, y.id)
3188+
objects_by_class[class_name], key=attrgetter("id")
31873189
):
31883190
object.Print(file)
31893191
self._XCPrint(file, 0, "/* End " + class_name + " section */\n")

0 commit comments

Comments
 (0)