-
Notifications
You must be signed in to change notification settings - Fork 241
/
test_tags.py
1297 lines (1157 loc) · 50.9 KB
/
test_tags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
import collections.abc
import subprocess
try:
import ctypes
except ImportError:
ctypes = None
import os
import pathlib
import platform
import sys
import sysconfig
import types
import pretend
import pytest
from packaging import tags
from packaging._musllinux import _MuslVersion
@pytest.fixture
def example_tag():
return tags.Tag("py3", "none", "any")
@pytest.fixture
def manylinux_module(monkeypatch):
monkeypatch.setattr(tags._manylinux, "_get_glibc_version", lambda *args: (2, 20))
module_name = "_manylinux"
module = types.ModuleType(module_name)
monkeypatch.setitem(sys.modules, module_name, module)
return module
@pytest.fixture
def mock_interpreter_name(monkeypatch):
def mock(name):
name = name.lower()
if sys.implementation.name != name:
monkeypatch.setattr(sys.implementation, "name", name)
return True
return False
return mock
class TestTag:
def test_lowercasing(self):
tag = tags.Tag("PY3", "None", "ANY")
assert tag.interpreter == "py3"
assert tag.abi == "none"
assert tag.platform == "any"
def test_equality(self):
args = "py3", "none", "any"
assert tags.Tag(*args) == tags.Tag(*args)
def test_equality_fails_with_non_tag(self):
assert not tags.Tag("py3", "none", "any") == "non-tag"
def test_hashing(self, example_tag):
tags = {example_tag} # Should not raise TypeError.
assert example_tag in tags
def test_hash_equality(self, example_tag):
equal_tag = tags.Tag("py3", "none", "any")
assert example_tag == equal_tag # Sanity check.
assert example_tag.__hash__() == equal_tag.__hash__()
def test_str(self, example_tag):
assert str(example_tag) == "py3-none-any"
def test_repr(self, example_tag):
assert repr(example_tag) == "<py3-none-any @ {tag_id}>".format(
tag_id=id(example_tag)
)
def test_attribute_access(self, example_tag):
assert example_tag.interpreter == "py3"
assert example_tag.abi == "none"
assert example_tag.platform == "any"
class TestParseTag:
def test_simple(self, example_tag):
parsed_tags = tags.parse_tag(str(example_tag))
assert parsed_tags == {example_tag}
def test_multi_interpreter(self, example_tag):
expected = {example_tag, tags.Tag("py2", "none", "any")}
given = tags.parse_tag("py2.py3-none-any")
assert given == expected
def test_multi_platform(self):
expected = {
tags.Tag("cp37", "cp37m", platform)
for platform in (
"macosx_10_6_intel",
"macosx_10_9_intel",
"macosx_10_9_x86_64",
"macosx_10_10_intel",
"macosx_10_10_x86_64",
)
}
given = tags.parse_tag(
"cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64."
"macosx_10_10_intel.macosx_10_10_x86_64"
)
assert given == expected
class TestInterpreterName:
def test_sys_implementation_name(self, monkeypatch):
class MockImplementation:
pass
mock_implementation = MockImplementation()
mock_implementation.name = "sillywalk"
monkeypatch.setattr(sys, "implementation", mock_implementation, raising=False)
assert tags.interpreter_name() == "sillywalk"
def test_interpreter_short_names(self, mock_interpreter_name, monkeypatch):
mock_interpreter_name("cpython")
assert tags.interpreter_name() == "cp"
class TestInterpreterVersion:
def test_warn(self, monkeypatch):
class MockConfigVar:
def __init__(self, return_):
self.warn = None
self._return = return_
def __call__(self, name, warn):
self.warn = warn
return self._return
mock_config_var = MockConfigVar("38")
monkeypatch.setattr(tags, "_get_config_var", mock_config_var)
tags.interpreter_version(warn=True)
assert mock_config_var.warn
def test_python_version_nodot(self, monkeypatch):
monkeypatch.setattr(tags, "_get_config_var", lambda var, warn: "NN")
assert tags.interpreter_version() == "NN"
@pytest.mark.parametrize(
"version_info,version_str",
[
((1, 2, 3), "12"),
((1, 12, 3), "112"),
((11, 2, 3), "112"),
((11, 12, 3), "1112"),
((1, 2, 13), "12"),
],
)
def test_sys_version_info(self, version_info, version_str, monkeypatch):
monkeypatch.setattr(tags, "_get_config_var", lambda *args, **kwargs: None)
monkeypatch.setattr(sys, "version_info", version_info)
assert tags.interpreter_version() == version_str
class TestMacOSPlatforms:
@pytest.mark.parametrize(
"arch, is_32bit, expected",
[
("i386", True, "i386"),
("ppc", True, "ppc"),
("x86_64", False, "x86_64"),
("x86_64", True, "i386"),
("ppc64", False, "ppc64"),
("ppc64", True, "ppc"),
],
)
def test_architectures(self, arch, is_32bit, expected):
assert tags._mac_arch(arch, is_32bit=is_32bit) == expected
@pytest.mark.parametrize(
"version,arch,expected",
[
(
(10, 15),
"x86_64",
["x86_64", "intel", "fat64", "fat32", "universal2", "universal"],
),
(
(10, 4),
"x86_64",
["x86_64", "intel", "fat64", "fat32", "universal2", "universal"],
),
((10, 3), "x86_64", []),
((10, 15), "i386", ["i386", "intel", "fat32", "fat", "universal"]),
((10, 4), "i386", ["i386", "intel", "fat32", "fat", "universal"]),
((10, 3), "intel", ["intel", "universal"]),
((10, 5), "intel", ["intel", "universal"]),
((10, 15), "intel", ["intel", "universal"]),
((10, 3), "i386", []),
((10, 15), "ppc64", []),
((10, 6), "ppc64", []),
((10, 5), "ppc64", ["ppc64", "fat64", "universal"]),
((10, 3), "ppc64", []),
((10, 15), "ppc", []),
((10, 7), "ppc", []),
((10, 6), "ppc", ["ppc", "fat32", "fat", "universal"]),
((10, 0), "ppc", ["ppc", "fat32", "fat", "universal"]),
((11, 0), "riscv", ["riscv"]),
(
(11, 0),
"x86_64",
["x86_64", "intel", "fat64", "fat32", "universal2", "universal"],
),
((11, 0), "arm64", ["arm64", "universal2"]),
((11, 1), "arm64", ["arm64", "universal2"]),
((12, 0), "arm64", ["arm64", "universal2"]),
],
)
def test_binary_formats(self, version, arch, expected):
assert tags._mac_binary_formats(version, arch) == expected
def test_version_detection(self, monkeypatch):
if platform.system() != "Darwin":
monkeypatch.setattr(
platform, "mac_ver", lambda: ("10.14", ("", "", ""), "x86_64")
)
version = platform.mac_ver()[0].split(".")
major = version[0]
minor = version[1] if major == "10" else "0"
platforms = list(tags.mac_platforms(arch="x86_64"))
if (major, minor) == ("10", "16"):
print(platforms, "macosx_11+")
# For 10.16, the real version is at least 11.0.
prefix, major, minor, _ = platforms[0].split("_", maxsplit=3)
assert prefix == "macosx"
assert int(major) >= 11
assert minor == "0"
else:
expected = f"macosx_{major}_{minor}_"
print(platforms, expected)
assert platforms[0].startswith(expected)
def test_version_detection_10_15(self, monkeypatch):
monkeypatch.setattr(
platform, "mac_ver", lambda: ("10.15", ("", "", ""), "x86_64")
)
expected = "macosx_10_15_"
platforms = list(tags.mac_platforms(arch="x86_64"))
print(platforms, expected)
assert platforms[0].startswith(expected)
def test_version_detection_compatibility(self, monkeypatch):
if platform.system() != "Darwin":
monkeypatch.setattr(
subprocess,
"run",
lambda *args, **kwargs: subprocess.CompletedProcess(
[], 0, stdout="10.15"
),
)
monkeypatch.setattr(
platform, "mac_ver", lambda: ("10.16", ("", "", ""), "x86_64")
)
unexpected = "macosx_10_16_"
platforms = list(tags.mac_platforms(arch="x86_64"))
print(platforms, unexpected)
assert not platforms[0].startswith(unexpected)
@pytest.mark.parametrize("arch", ["x86_64", "i386"])
def test_arch_detection(self, arch, monkeypatch):
if platform.system() != "Darwin" or platform.mac_ver()[2] != arch:
monkeypatch.setattr(
platform, "mac_ver", lambda: ("10.14", ("", "", ""), arch)
)
monkeypatch.setattr(tags, "_mac_arch", lambda *args: arch)
assert next(tags.mac_platforms((10, 14))).endswith(arch)
def test_mac_platforms(self):
platforms = list(tags.mac_platforms((10, 5), "x86_64"))
assert platforms == [
"macosx_10_5_x86_64",
"macosx_10_5_intel",
"macosx_10_5_fat64",
"macosx_10_5_fat32",
"macosx_10_5_universal2",
"macosx_10_5_universal",
"macosx_10_4_x86_64",
"macosx_10_4_intel",
"macosx_10_4_fat64",
"macosx_10_4_fat32",
"macosx_10_4_universal2",
"macosx_10_4_universal",
]
assert len(list(tags.mac_platforms((10, 17), "x86_64"))) == 14 * 6
assert not list(tags.mac_platforms((10, 0), "x86_64"))
@pytest.mark.parametrize("major,minor", [(11, 0), (11, 3), (12, 0), (12, 3)])
def test_macos_11(self, major, minor):
platforms = list(tags.mac_platforms((major, minor), "x86_64"))
assert "macosx_11_0_arm64" not in platforms
assert "macosx_11_0_x86_64" in platforms
assert "macosx_11_3_x86_64" not in platforms
assert "macosx_11_0_universal" in platforms
assert "macosx_11_0_universal2" in platforms
# Mac OS "10.16" is the version number that binaries compiled against an old
# (pre 11.0) SDK will see. It can also be enabled explicitly for a process
# with the environment variable SYSTEM_VERSION_COMPAT=1.
assert "macosx_10_16_x86_64" in platforms
assert "macosx_10_15_x86_64" in platforms
assert "macosx_10_15_universal2" in platforms
assert "macosx_10_4_x86_64" in platforms
assert "macosx_10_3_x86_64" not in platforms
if major >= 12:
assert "macosx_12_0_x86_64" in platforms
assert "macosx_12_0_universal" in platforms
assert "macosx_12_0_universal2" in platforms
platforms = list(tags.mac_platforms((major, minor), "arm64"))
assert "macosx_11_0_arm64" in platforms
assert "macosx_11_3_arm64" not in platforms
assert "macosx_11_0_universal" not in platforms
assert "macosx_11_0_universal2" in platforms
assert "macosx_10_15_universal2" in platforms
assert "macosx_10_15_x86_64" not in platforms
assert "macosx_10_4_x86_64" not in platforms
assert "macosx_10_3_x86_64" not in platforms
if major >= 12:
assert "macosx_12_0_arm64" in platforms
assert "macosx_12_0_universal2" in platforms
class TestManylinuxPlatform:
def teardown_method(self):
# Clear the version cache
tags._manylinux._get_glibc_version.cache_clear()
def test_get_config_var_does_not_log(self, monkeypatch):
debug = pretend.call_recorder(lambda *a: None)
monkeypatch.setattr(tags.logger, "debug", debug)
tags._get_config_var("missing")
assert debug.calls == []
def test_get_config_var_does_log(self, monkeypatch):
debug = pretend.call_recorder(lambda *a: None)
monkeypatch.setattr(tags.logger, "debug", debug)
tags._get_config_var("missing", warn=True)
assert debug.calls == [
pretend.call(
"Config variable '%s' is unset, Python ABI tag may be incorrect",
"missing",
)
]
@pytest.mark.parametrize(
"arch,is_32bit,expected",
[
("linux-x86_64", False, "linux_x86_64"),
("linux-x86_64", True, "linux_i686"),
("linux-aarch64", False, "linux_aarch64"),
("linux-aarch64", True, "linux_armv7l"),
],
)
def test_linux_platforms_32_64bit_on_64bit_os(
self, arch, is_32bit, expected, monkeypatch
):
monkeypatch.setattr(sysconfig, "get_platform", lambda: arch)
monkeypatch.setattr(os, "confstr", lambda x: "glibc 2.20", raising=False)
monkeypatch.setattr(tags._manylinux, "_is_compatible", lambda *args: False)
linux_platform = list(tags._linux_platforms(is_32bit=is_32bit))[-1]
assert linux_platform == expected
def test_linux_platforms_manylinux_unsupported(self, monkeypatch):
monkeypatch.setattr(sysconfig, "get_platform", lambda: "linux_x86_64")
monkeypatch.setattr(os, "confstr", lambda x: "glibc 2.20", raising=False)
monkeypatch.setattr(tags._manylinux, "_is_compatible", lambda *args: False)
linux_platform = list(tags._linux_platforms(is_32bit=False))
assert linux_platform == ["linux_x86_64"]
def test_linux_platforms_manylinux1(self, monkeypatch):
monkeypatch.setattr(
tags._manylinux, "_is_compatible", lambda name, *args: name == "manylinux1"
)
monkeypatch.setattr(sysconfig, "get_platform", lambda: "linux_x86_64")
monkeypatch.setattr(platform, "machine", lambda: "x86_64")
monkeypatch.setattr(os, "confstr", lambda x: "glibc 2.20", raising=False)
platforms = list(tags._linux_platforms(is_32bit=False))
arch = platform.machine()
assert platforms == ["manylinux1_" + arch, "linux_" + arch]
def test_linux_platforms_manylinux2010(self, monkeypatch):
monkeypatch.setattr(sysconfig, "get_platform", lambda: "linux_x86_64")
monkeypatch.setattr(platform, "machine", lambda: "x86_64")
monkeypatch.setattr(os, "confstr", lambda x: "glibc 2.12", raising=False)
platforms = list(tags._linux_platforms(is_32bit=False))
arch = platform.machine()
expected = [
"manylinux_2_12_" + arch,
"manylinux2010_" + arch,
"manylinux_2_11_" + arch,
"manylinux_2_10_" + arch,
"manylinux_2_9_" + arch,
"manylinux_2_8_" + arch,
"manylinux_2_7_" + arch,
"manylinux_2_6_" + arch,
"manylinux_2_5_" + arch,
"manylinux1_" + arch,
"linux_" + arch,
]
assert platforms == expected
def test_linux_platforms_manylinux2014(self, monkeypatch):
monkeypatch.setattr(sysconfig, "get_platform", lambda: "linux_x86_64")
monkeypatch.setattr(platform, "machine", lambda: "x86_64")
monkeypatch.setattr(os, "confstr", lambda x: "glibc 2.17", raising=False)
platforms = list(tags._linux_platforms(is_32bit=False))
arch = platform.machine()
expected = [
"manylinux_2_17_" + arch,
"manylinux2014_" + arch,
"manylinux_2_16_" + arch,
"manylinux_2_15_" + arch,
"manylinux_2_14_" + arch,
"manylinux_2_13_" + arch,
"manylinux_2_12_" + arch,
"manylinux2010_" + arch,
"manylinux_2_11_" + arch,
"manylinux_2_10_" + arch,
"manylinux_2_9_" + arch,
"manylinux_2_8_" + arch,
"manylinux_2_7_" + arch,
"manylinux_2_6_" + arch,
"manylinux_2_5_" + arch,
"manylinux1_" + arch,
"linux_" + arch,
]
assert platforms == expected
def test_linux_platforms_manylinux2014_armhf_abi(self, monkeypatch):
monkeypatch.setattr(tags._manylinux, "_glibc_version_string", lambda: "2.30")
monkeypatch.setattr(
tags._manylinux,
"_is_compatible",
lambda name, *args: name == "manylinux2014",
)
monkeypatch.setattr(sysconfig, "get_platform", lambda: "linux_armv7l")
monkeypatch.setattr(
sys,
"executable",
os.path.join(
os.path.dirname(__file__),
"manylinux",
"hello-world-armv7l-armhf",
),
)
platforms = list(tags._linux_platforms(is_32bit=True))
expected = ["manylinux2014_armv7l", "linux_armv7l"]
assert platforms == expected
def test_linux_platforms_manylinux2014_i386_abi(self, monkeypatch):
monkeypatch.setattr(tags._manylinux, "_glibc_version_string", lambda: "2.17")
monkeypatch.setattr(sysconfig, "get_platform", lambda: "linux_x86_64")
monkeypatch.setattr(
sys,
"executable",
os.path.join(
os.path.dirname(__file__),
"manylinux",
"hello-world-x86_64-i386",
),
)
platforms = list(tags._linux_platforms(is_32bit=True))
expected = [
"manylinux_2_17_i686",
"manylinux2014_i686",
"manylinux_2_16_i686",
"manylinux_2_15_i686",
"manylinux_2_14_i686",
"manylinux_2_13_i686",
"manylinux_2_12_i686",
"manylinux2010_i686",
"manylinux_2_11_i686",
"manylinux_2_10_i686",
"manylinux_2_9_i686",
"manylinux_2_8_i686",
"manylinux_2_7_i686",
"manylinux_2_6_i686",
"manylinux_2_5_i686",
"manylinux1_i686",
"linux_i686",
]
assert platforms == expected
def test_linux_platforms_manylinux_glibc3(self, monkeypatch):
# test for a future glic 3.x version
monkeypatch.setattr(tags._manylinux, "_glibc_version_string", lambda: "3.2")
monkeypatch.setattr(tags._manylinux, "_is_compatible", lambda name, *args: True)
monkeypatch.setattr(sysconfig, "get_platform", lambda: "linux_aarch64")
monkeypatch.setattr(
sys,
"executable",
os.path.join(
os.path.dirname(__file__),
"manylinux",
"hello-world-aarch64",
),
)
platforms = list(tags._linux_platforms(is_32bit=False))
expected = (
["manylinux_3_2_aarch64", "manylinux_3_1_aarch64", "manylinux_3_0_aarch64"]
+ [f"manylinux_2_{i}_aarch64" for i in range(50, 16, -1)]
+ ["manylinux2014_aarch64", "linux_aarch64"]
)
assert platforms == expected
@pytest.mark.parametrize(
"native_arch, cross32_arch, musl_version",
[
("aarch64", "armv7l", _MuslVersion(1, 1)),
("i386", "i386", _MuslVersion(1, 2)),
("x86_64", "i686", _MuslVersion(1, 2)),
],
)
@pytest.mark.parametrize("cross32", [True, False], ids=["cross", "native"])
def test_linux_platforms_musllinux(
self, monkeypatch, native_arch, cross32_arch, musl_version, cross32
):
fake_executable = str(
pathlib.Path(__file__)
.parent.joinpath("musllinux", f"musl-{native_arch}")
.resolve()
)
monkeypatch.setattr(tags._musllinux.sys, "executable", fake_executable)
monkeypatch.setattr(sysconfig, "get_platform", lambda: f"linux_{native_arch}")
monkeypatch.setattr(tags._manylinux, "platform_tags", lambda *_: ())
recorder = pretend.call_recorder(lambda _: musl_version)
monkeypatch.setattr(tags._musllinux, "_get_musl_version", recorder)
platforms = list(tags._linux_platforms(is_32bit=cross32))
target_arch = cross32_arch if cross32 else native_arch
expected = [
f"musllinux_{musl_version[0]}_{minor}_{target_arch}"
for minor in range(musl_version[1], -1, -1)
] + [f"linux_{target_arch}"]
assert platforms == expected
assert recorder.calls == [pretend.call(fake_executable)]
def test_linux_platforms_manylinux2014_armv6l(self, monkeypatch):
monkeypatch.setattr(
tags._manylinux, "_is_compatible", lambda name, _: name == "manylinux2014"
)
monkeypatch.setattr(sysconfig, "get_platform", lambda: "linux_armv6l")
monkeypatch.setattr(os, "confstr", lambda x: "glibc 2.20", raising=False)
platforms = list(tags._linux_platforms(is_32bit=True))
expected = ["linux_armv6l"]
assert platforms == expected
@pytest.mark.parametrize(
"machine, abi, alt_machine",
[("x86_64", "x32", "i686"), ("armv7l", "armel", "armv7l")],
)
def test_linux_platforms_not_manylinux_abi(
self, monkeypatch, machine, abi, alt_machine
):
monkeypatch.setattr(tags._manylinux, "_is_compatible", lambda name, _: False)
monkeypatch.setattr(sysconfig, "get_platform", lambda: f"linux_{machine}")
monkeypatch.setattr(
sys,
"executable",
os.path.join(
os.path.dirname(__file__),
"manylinux",
f"hello-world-{machine}-{abi}",
),
)
platforms = list(tags._linux_platforms(is_32bit=True))
expected = [f"linux_{alt_machine}"]
assert platforms == expected
@pytest.mark.parametrize(
"platform_name,dispatch_func",
[
("Darwin", "mac_platforms"),
("Linux", "_linux_platforms"),
("Generic", "_generic_platforms"),
],
)
def test_platform_tags(platform_name, dispatch_func, monkeypatch):
expected = ["sillywalk"]
monkeypatch.setattr(platform, "system", lambda: platform_name)
monkeypatch.setattr(tags, dispatch_func, lambda: expected)
assert tags.platform_tags() == expected
class TestCPythonABI:
@pytest.mark.parametrize(
"py_debug,gettotalrefcount,result",
[(1, False, True), (0, False, False), (None, True, True)],
)
def test_debug(self, py_debug, gettotalrefcount, result, monkeypatch):
config = {"Py_DEBUG": py_debug, "WITH_PYMALLOC": 0, "Py_UNICODE_SIZE": 2}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
if gettotalrefcount:
monkeypatch.setattr(sys, "gettotalrefcount", 1, raising=False)
expected = ["cp37d" if result else "cp37"]
assert tags._cpython_abis((3, 7)) == expected
def test_debug_file_extension(self, monkeypatch):
config = {"Py_DEBUG": None}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
monkeypatch.delattr(sys, "gettotalrefcount", raising=False)
monkeypatch.setattr(tags, "EXTENSION_SUFFIXES", {"_d.pyd"})
assert tags._cpython_abis((3, 8)) == ["cp38d", "cp38"]
@pytest.mark.parametrize(
"debug,expected", [(True, ["cp38d", "cp38"]), (False, ["cp38"])]
)
def test__debug_cp38(self, debug, expected, monkeypatch):
config = {"Py_DEBUG": debug}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
assert tags._cpython_abis((3, 8)) == expected
@pytest.mark.parametrize(
"pymalloc,version,result",
[
(1, (3, 7), True),
(0, (3, 7), False),
(None, (3, 7), True),
(1, (3, 8), False),
],
)
def test_pymalloc(self, pymalloc, version, result, monkeypatch):
config = {"Py_DEBUG": 0, "WITH_PYMALLOC": pymalloc, "Py_UNICODE_SIZE": 2}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
base_abi = f"cp{version[0]}{version[1]}"
expected = [base_abi + "m" if result else base_abi]
assert tags._cpython_abis(version) == expected
@pytest.mark.parametrize(
"unicode_size,maxunicode,version,result",
[
(4, 0x10FFFF, (3, 2), True),
(2, 0xFFFF, (3, 2), False),
(None, 0x10FFFF, (3, 2), True),
(None, 0xFFFF, (3, 2), False),
(4, 0x10FFFF, (3, 3), False),
],
)
def test_wide_unicode(self, unicode_size, maxunicode, version, result, monkeypatch):
config = {"Py_DEBUG": 0, "WITH_PYMALLOC": 0, "Py_UNICODE_SIZE": unicode_size}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
monkeypatch.setattr(sys, "maxunicode", maxunicode)
base_abi = "cp" + tags._version_nodot(version)
expected = [base_abi + "u" if result else base_abi]
assert tags._cpython_abis(version) == expected
class TestCPythonTags:
def test_iterator_returned(self):
result_iterator = tags.cpython_tags(
(3, 8), ["cp38d", "cp38"], ["plat1", "plat2"]
)
assert isinstance(result_iterator, collections.abc.Iterator)
def test_all_args(self):
result_iterator = tags.cpython_tags(
(3, 11), ["cp311d", "cp311"], ["plat1", "plat2"]
)
result = list(result_iterator)
assert result == [
tags.Tag("cp311", "cp311d", "plat1"),
tags.Tag("cp311", "cp311d", "plat2"),
tags.Tag("cp311", "cp311", "plat1"),
tags.Tag("cp311", "cp311", "plat2"),
tags.Tag("cp311", "abi3", "plat1"),
tags.Tag("cp311", "abi3", "plat2"),
tags.Tag("cp311", "none", "plat1"),
tags.Tag("cp311", "none", "plat2"),
tags.Tag("cp310", "abi3", "plat1"),
tags.Tag("cp310", "abi3", "plat2"),
tags.Tag("cp39", "abi3", "plat1"),
tags.Tag("cp39", "abi3", "plat2"),
tags.Tag("cp38", "abi3", "plat1"),
tags.Tag("cp38", "abi3", "plat2"),
tags.Tag("cp37", "abi3", "plat1"),
tags.Tag("cp37", "abi3", "plat2"),
tags.Tag("cp36", "abi3", "plat1"),
tags.Tag("cp36", "abi3", "plat2"),
tags.Tag("cp35", "abi3", "plat1"),
tags.Tag("cp35", "abi3", "plat2"),
tags.Tag("cp34", "abi3", "plat1"),
tags.Tag("cp34", "abi3", "plat2"),
tags.Tag("cp33", "abi3", "plat1"),
tags.Tag("cp33", "abi3", "plat2"),
tags.Tag("cp32", "abi3", "plat1"),
tags.Tag("cp32", "abi3", "plat2"),
]
result_iterator = tags.cpython_tags(
(3, 8), ["cp38d", "cp38"], ["plat1", "plat2"]
)
result = list(result_iterator)
assert result == [
tags.Tag("cp38", "cp38d", "plat1"),
tags.Tag("cp38", "cp38d", "plat2"),
tags.Tag("cp38", "cp38", "plat1"),
tags.Tag("cp38", "cp38", "plat2"),
tags.Tag("cp38", "abi3", "plat1"),
tags.Tag("cp38", "abi3", "plat2"),
tags.Tag("cp38", "none", "plat1"),
tags.Tag("cp38", "none", "plat2"),
tags.Tag("cp37", "abi3", "plat1"),
tags.Tag("cp37", "abi3", "plat2"),
tags.Tag("cp36", "abi3", "plat1"),
tags.Tag("cp36", "abi3", "plat2"),
tags.Tag("cp35", "abi3", "plat1"),
tags.Tag("cp35", "abi3", "plat2"),
tags.Tag("cp34", "abi3", "plat1"),
tags.Tag("cp34", "abi3", "plat2"),
tags.Tag("cp33", "abi3", "plat1"),
tags.Tag("cp33", "abi3", "plat2"),
tags.Tag("cp32", "abi3", "plat1"),
tags.Tag("cp32", "abi3", "plat2"),
]
result = list(tags.cpython_tags((3, 3), ["cp33m"], ["plat1", "plat2"]))
assert result == [
tags.Tag("cp33", "cp33m", "plat1"),
tags.Tag("cp33", "cp33m", "plat2"),
tags.Tag("cp33", "abi3", "plat1"),
tags.Tag("cp33", "abi3", "plat2"),
tags.Tag("cp33", "none", "plat1"),
tags.Tag("cp33", "none", "plat2"),
tags.Tag("cp32", "abi3", "plat1"),
tags.Tag("cp32", "abi3", "plat2"),
]
def test_python_version_defaults(self):
tag = next(tags.cpython_tags(abis=["abi3"], platforms=["any"]))
interpreter = "cp" + tags._version_nodot(sys.version_info[:2])
assert interpreter == tag.interpreter
def test_abi_defaults(self, monkeypatch):
monkeypatch.setattr(tags, "_cpython_abis", lambda _1, _2: ["cp38"])
result = list(tags.cpython_tags((3, 8), platforms=["any"]))
assert tags.Tag("cp38", "cp38", "any") in result
assert tags.Tag("cp38", "abi3", "any") in result
assert tags.Tag("cp38", "none", "any") in result
def test_abi_defaults_needs_underscore(self, monkeypatch):
monkeypatch.setattr(tags, "_cpython_abis", lambda _1, _2: ["cp311"])
result = list(tags.cpython_tags((3, 11), platforms=["any"]))
assert tags.Tag("cp311", "cp311", "any") in result
assert tags.Tag("cp311", "abi3", "any") in result
assert tags.Tag("cp311", "none", "any") in result
def test_platforms_defaults(self, monkeypatch):
monkeypatch.setattr(tags, "platform_tags", lambda: ["plat1"])
result = list(tags.cpython_tags((3, 8), abis=["whatever"]))
assert tags.Tag("cp38", "whatever", "plat1") in result
def test_platforms_defaults_needs_underscore(self, monkeypatch):
monkeypatch.setattr(tags, "platform_tags", lambda: ["plat1"])
result = list(tags.cpython_tags((3, 11), abis=["whatever"]))
assert tags.Tag("cp311", "whatever", "plat1") in result
def test_major_only_python_version(self):
result = list(tags.cpython_tags((3,), ["abi"], ["plat"]))
assert result == [
tags.Tag("cp3", "abi", "plat"),
tags.Tag("cp3", "none", "plat"),
]
def test_major_only_python_version_with_default_abis(self):
result = list(tags.cpython_tags((3,), platforms=["plat"]))
assert result == [tags.Tag("cp3", "none", "plat")]
@pytest.mark.parametrize("abis", [[], ["abi3"], ["none"]])
def test_skip_redundant_abis(self, abis):
results = list(tags.cpython_tags((3, 0), abis=abis, platforms=["any"]))
assert results == [tags.Tag("cp30", "none", "any")]
def test_abi3_python33(self):
results = list(tags.cpython_tags((3, 3), abis=["cp33"], platforms=["plat"]))
assert results == [
tags.Tag("cp33", "cp33", "plat"),
tags.Tag("cp33", "abi3", "plat"),
tags.Tag("cp33", "none", "plat"),
tags.Tag("cp32", "abi3", "plat"),
]
def test_no_excess_abi3_python32(self):
results = list(tags.cpython_tags((3, 2), abis=["cp32"], platforms=["plat"]))
assert results == [
tags.Tag("cp32", "cp32", "plat"),
tags.Tag("cp32", "abi3", "plat"),
tags.Tag("cp32", "none", "plat"),
]
def test_no_abi3_python31(self):
results = list(tags.cpython_tags((3, 1), abis=["cp31"], platforms=["plat"]))
assert results == [
tags.Tag("cp31", "cp31", "plat"),
tags.Tag("cp31", "none", "plat"),
]
def test_no_abi3_python27(self):
results = list(tags.cpython_tags((2, 7), abis=["cp27"], platforms=["plat"]))
assert results == [
tags.Tag("cp27", "cp27", "plat"),
tags.Tag("cp27", "none", "plat"),
]
class TestGenericTags:
def test__generic_abi_macos(self, monkeypatch):
monkeypatch.setattr(
sysconfig, "get_config_var", lambda key: ".cpython-37m-darwin.so"
)
monkeypatch.setattr(tags, "interpreter_name", lambda: "cp")
assert tags._generic_abi() == ["cp37m"]
def test__generic_abi_linux_cpython(self, monkeypatch):
config = {
"Py_DEBUG": False,
"WITH_PYMALLOC": True,
"EXT_SUFFIX": ".cpython-37m-x86_64-linux-gnu.so",
}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
monkeypatch.setattr(tags, "interpreter_name", lambda: "cp")
# They are identical
assert tags._cpython_abis((3, 7)) == ["cp37m"]
assert tags._generic_abi() == ["cp37m"]
def test__generic_abi_jp(self, monkeypatch):
config = {"EXT_SUFFIX": ".return exactly this.so"}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
assert tags._generic_abi() == ["return exactly this"]
def test__generic_abi_graal(self, monkeypatch):
config = {"EXT_SUFFIX": ".graalpy-38-native-x86_64-darwin.so"}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
assert tags._generic_abi() == ["graalpy_38_native"]
def test__generic_abi_none(self, monkeypatch):
config = {"EXT_SUFFIX": "..so"}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
assert tags._generic_abi() == []
@pytest.mark.parametrize("ext_suffix", ["invalid", None])
def test__generic_abi_error(self, ext_suffix, monkeypatch):
config = {"EXT_SUFFIX": ext_suffix}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
with pytest.raises(SystemError) as e:
tags._generic_abi()
assert "EXT_SUFFIX" in str(e.value)
def test__generic_abi_linux_pypy(self, monkeypatch):
# issue gh-606
config = {
"Py_DEBUG": False,
"EXT_SUFFIX": ".pypy39-pp73-x86_64-linux-gnu.so",
}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
monkeypatch.setattr(tags, "interpreter_name", lambda: "pp")
assert tags._generic_abi() == ["pypy39_pp73"]
def test__generic_abi_old_windows(self, monkeypatch):
config = {
"EXT_SUFFIX": ".pyd",
"Py_DEBUG": 0,
"WITH_PYMALLOC": 0,
}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
assert tags._generic_abi() == tags._cpython_abis(sys.version_info[:2])
def test__generic_abi_windows(self, monkeypatch):
config = {
"EXT_SUFFIX": ".cp310-win_amd64.pyd",
}
monkeypatch.setattr(sysconfig, "get_config_var", config.__getitem__)
assert tags._generic_abi() == ["cp310"]
@pytest.mark.skipif(sys.implementation.name != "cpython", reason="CPython-only")
def test__generic_abi_agree(self):
"""Test that the two methods of finding the abi tag agree"""
assert tags._generic_abi() == tags._cpython_abis(sys.version_info[:2])
def test_generic_platforms(self):
platform = sysconfig.get_platform().replace("-", "_")
platform = platform.replace(".", "_")
assert list(tags._generic_platforms()) == [platform]
def test_iterator_returned(self):
result_iterator = tags.generic_tags("sillywalk33", ["abi"], ["plat1", "plat2"])
assert isinstance(result_iterator, collections.abc.Iterator)
def test_all_args(self):
result_iterator = tags.generic_tags("sillywalk33", ["abi"], ["plat1", "plat2"])
result = list(result_iterator)
assert result == [
tags.Tag("sillywalk33", "abi", "plat1"),
tags.Tag("sillywalk33", "abi", "plat2"),
tags.Tag("sillywalk33", "none", "plat1"),
tags.Tag("sillywalk33", "none", "plat2"),
]
@pytest.mark.parametrize("abi", [[], ["none"]])
def test_abi_unspecified(self, abi):
no_abi = list(tags.generic_tags("sillywalk34", abi, ["plat1", "plat2"]))
assert no_abi == [
tags.Tag("sillywalk34", "none", "plat1"),
tags.Tag("sillywalk34", "none", "plat2"),
]
def test_interpreter_default(self, monkeypatch):
monkeypatch.setattr(tags, "interpreter_name", lambda: "sillywalk")
monkeypatch.setattr(tags, "interpreter_version", lambda warn: "NN")
result = list(tags.generic_tags(abis=["none"], platforms=["any"]))
assert result == [tags.Tag("sillywalkNN", "none", "any")]
def test_abis_default(self, monkeypatch):
monkeypatch.setattr(tags, "_generic_abi", lambda: ["abi"])
result = list(tags.generic_tags(interpreter="sillywalk", platforms=["any"]))
assert result == [
tags.Tag("sillywalk", "abi", "any"),
tags.Tag("sillywalk", "none", "any"),
]
def test_platforms_default(self, monkeypatch):
monkeypatch.setattr(tags, "platform_tags", lambda: ["plat"])
result = list(tags.generic_tags(interpreter="sillywalk", abis=["none"]))
assert result == [tags.Tag("sillywalk", "none", "plat")]
class TestCompatibleTags:
def test_all_args(self):
result = list(tags.compatible_tags((3, 3), "cp33", ["plat1", "plat2"]))
assert result == [
tags.Tag("py33", "none", "plat1"),
tags.Tag("py33", "none", "plat2"),
tags.Tag("py3", "none", "plat1"),
tags.Tag("py3", "none", "plat2"),
tags.Tag("py32", "none", "plat1"),
tags.Tag("py32", "none", "plat2"),
tags.Tag("py31", "none", "plat1"),
tags.Tag("py31", "none", "plat2"),
tags.Tag("py30", "none", "plat1"),
tags.Tag("py30", "none", "plat2"),
tags.Tag("cp33", "none", "any"),
tags.Tag("py33", "none", "any"),
tags.Tag("py3", "none", "any"),
tags.Tag("py32", "none", "any"),
tags.Tag("py31", "none", "any"),
tags.Tag("py30", "none", "any"),
]
def test_all_args_needs_underscore(self):
result = list(tags.compatible_tags((3, 11), "cp311", ["plat1", "plat2"]))
assert result == [
tags.Tag("py311", "none", "plat1"),
tags.Tag("py311", "none", "plat2"),
tags.Tag("py3", "none", "plat1"),
tags.Tag("py3", "none", "plat2"),
tags.Tag("py310", "none", "plat1"),
tags.Tag("py310", "none", "plat2"),
tags.Tag("py39", "none", "plat1"),
tags.Tag("py39", "none", "plat2"),
tags.Tag("py38", "none", "plat1"),
tags.Tag("py38", "none", "plat2"),
tags.Tag("py37", "none", "plat1"),
tags.Tag("py37", "none", "plat2"),
tags.Tag("py36", "none", "plat1"),
tags.Tag("py36", "none", "plat2"),
tags.Tag("py35", "none", "plat1"),
tags.Tag("py35", "none", "plat2"),
tags.Tag("py34", "none", "plat1"),
tags.Tag("py34", "none", "plat2"),
tags.Tag("py33", "none", "plat1"),
tags.Tag("py33", "none", "plat2"),
tags.Tag("py32", "none", "plat1"),
tags.Tag("py32", "none", "plat2"),
tags.Tag("py31", "none", "plat1"),
tags.Tag("py31", "none", "plat2"),
tags.Tag("py30", "none", "plat1"),
tags.Tag("py30", "none", "plat2"),
tags.Tag("cp311", "none", "any"),
tags.Tag("py311", "none", "any"),
tags.Tag("py3", "none", "any"),
tags.Tag("py310", "none", "any"),