@@ -271,10 +271,49 @@ impl InternerGuard<'_> {
271
271
key,
272
272
operator,
273
273
value,
274
- } => (
275
- Variable :: String ( key. into ( ) ) ,
276
- Edges :: from_string ( operator, value) ,
277
- ) ,
274
+ } => {
275
+ // Normalize `platform_system` markers to `sys_platform` nodes.
276
+ //
277
+ // The `platform` module is "primarily intended for diagnostic information to be
278
+ // read by humans."
279
+ //
280
+ // We only normalize when we can confidently guarantee that the values are
281
+ // exactly equivalent. For example, we normalize `platform_system == 'Windows'`
282
+ // to `sys_platform == 'win32'`, but we do not normalize `platform_system == 'FreeBSD'`
283
+ // to `sys_platform == 'freebsd'`, since FreeBSD typically includes a major version
284
+ // in its `sys.platform` output.
285
+ //
286
+ // For cases that aren't normalized, we do our best to encode known-incompatible
287
+ // values in `exclusions`.
288
+ //
289
+ // See: https://discuss.python.org/t/clarify-usage-of-platform-system/70900
290
+ let ( key, value) = match ( key, value. as_str ( ) ) {
291
+ ( MarkerValueString :: PlatformSystem , "Windows" ) => {
292
+ ( CanonicalMarkerValueString :: SysPlatform , "win32" . to_string ( ) )
293
+ }
294
+ ( MarkerValueString :: PlatformSystem , "Darwin" ) => (
295
+ CanonicalMarkerValueString :: SysPlatform ,
296
+ "darwin" . to_string ( ) ,
297
+ ) ,
298
+ ( MarkerValueString :: PlatformSystem , "Linux" ) => {
299
+ ( CanonicalMarkerValueString :: SysPlatform , "linux" . to_string ( ) )
300
+ }
301
+ ( MarkerValueString :: PlatformSystem , "AIX" ) => {
302
+ ( CanonicalMarkerValueString :: SysPlatform , "aix" . to_string ( ) )
303
+ }
304
+ ( MarkerValueString :: PlatformSystem , "Emscripten" ) => (
305
+ CanonicalMarkerValueString :: SysPlatform ,
306
+ "emscripten" . to_string ( ) ,
307
+ ) ,
308
+ // See: https://peps.python.org/pep-0738/#sys
309
+ ( MarkerValueString :: PlatformSystem , "Android" ) => (
310
+ CanonicalMarkerValueString :: SysPlatform ,
311
+ "android" . to_string ( ) ,
312
+ ) ,
313
+ _ => ( key. into ( ) , value) ,
314
+ } ;
315
+ ( Variable :: String ( key) , Edges :: from_string ( operator, value) )
316
+ }
278
317
// A variable representing the existence or absence of a particular extra.
279
318
MarkerExpression :: Extra {
280
319
name : MarkerValueExtra :: Extra ( extra) ,
@@ -821,86 +860,23 @@ impl InternerGuard<'_> {
821
860
return exclusions;
822
861
}
823
862
let mut tree = NodeId :: FALSE ;
824
- for ( a, b) in [
825
- // sys_platform == 'darwin' and platform_system == 'Windows'
826
- (
827
- MarkerExpression :: String {
828
- key : MarkerValueString :: SysPlatform ,
829
- operator : MarkerOperator :: Equal ,
830
- value : "darwin" . to_string ( ) ,
831
- } ,
832
- MarkerExpression :: String {
833
- key : MarkerValueString :: PlatformSystem ,
834
- operator : MarkerOperator :: Equal ,
835
- value : "Windows" . to_string ( ) ,
836
- } ,
837
- ) ,
838
- // sys_platform == 'darwin' and platform_system == 'Linux'
839
- (
840
- MarkerExpression :: String {
841
- key : MarkerValueString :: SysPlatform ,
842
- operator : MarkerOperator :: Equal ,
843
- value : "darwin" . to_string ( ) ,
844
- } ,
845
- MarkerExpression :: String {
846
- key : MarkerValueString :: PlatformSystem ,
847
- operator : MarkerOperator :: Equal ,
848
- value : "Linux" . to_string ( ) ,
849
- } ,
850
- ) ,
851
- // sys_platform == 'win32' and platform_system == 'Darwin'
852
- (
853
- MarkerExpression :: String {
854
- key : MarkerValueString :: SysPlatform ,
855
- operator : MarkerOperator :: Equal ,
856
- value : "win32" . to_string ( ) ,
857
- } ,
858
- MarkerExpression :: String {
859
- key : MarkerValueString :: PlatformSystem ,
860
- operator : MarkerOperator :: Equal ,
861
- value : "Darwin" . to_string ( ) ,
862
- } ,
863
- ) ,
864
- // sys_platform == 'win32' and platform_system == 'Linux'
865
- (
866
- MarkerExpression :: String {
867
- key : MarkerValueString :: SysPlatform ,
868
- operator : MarkerOperator :: Equal ,
869
- value : "win32" . to_string ( ) ,
870
- } ,
871
- MarkerExpression :: String {
872
- key : MarkerValueString :: PlatformSystem ,
873
- operator : MarkerOperator :: Equal ,
874
- value : "Linux" . to_string ( ) ,
875
- } ,
876
- ) ,
877
- // sys_platform == 'linux' and platform_system == 'Darwin'
863
+
864
+ // Pairs of `os_name` and `sys_platform` that are known to be incompatible.
865
+ //
866
+ // For example: `os_name == 'nt' and sys_platform == 'darwin'`
867
+ let mut pairs = vec ! [
878
868
(
879
869
MarkerExpression :: String {
880
- key : MarkerValueString :: SysPlatform ,
881
- operator : MarkerOperator :: Equal ,
882
- value : "linux" . to_string ( ) ,
883
- } ,
884
- MarkerExpression :: String {
885
- key : MarkerValueString :: PlatformSystem ,
870
+ key: MarkerValueString :: OsName ,
886
871
operator: MarkerOperator :: Equal ,
887
- value : "Darwin " . to_string ( ) ,
872
+ value: "nt " . to_string( ) ,
888
873
} ,
889
- ) ,
890
- // sys_platform == 'linux' and platform_system == 'Windows'
891
- (
892
874
MarkerExpression :: String {
893
875
key: MarkerValueString :: SysPlatform ,
894
876
operator: MarkerOperator :: Equal ,
895
877
value: "linux" . to_string( ) ,
896
878
} ,
897
- MarkerExpression :: String {
898
- key : MarkerValueString :: PlatformSystem ,
899
- operator : MarkerOperator :: Equal ,
900
- value : "Windows" . to_string ( ) ,
901
- } ,
902
879
) ,
903
- // os_name == 'nt' and sys_platform == 'darwin'
904
880
(
905
881
MarkerExpression :: String {
906
882
key: MarkerValueString :: OsName ,
@@ -913,7 +889,6 @@ impl InternerGuard<'_> {
913
889
value: "darwin" . to_string( ) ,
914
890
} ,
915
891
) ,
916
- // os_name == 'nt' and sys_platform == 'linux'
917
892
(
918
893
MarkerExpression :: String {
919
894
key: MarkerValueString :: OsName ,
@@ -923,10 +898,9 @@ impl InternerGuard<'_> {
923
898
MarkerExpression :: String {
924
899
key: MarkerValueString :: SysPlatform ,
925
900
operator: MarkerOperator :: Equal ,
926
- value : "linux " . to_string ( ) ,
901
+ value: "ios " . to_string( ) ,
927
902
} ,
928
903
) ,
929
- // os_name == 'posix' and sys_platform == 'win32'
930
904
(
931
905
MarkerExpression :: String {
932
906
key: MarkerValueString :: OsName ,
@@ -939,51 +913,61 @@ impl InternerGuard<'_> {
939
913
value: "win32" . to_string( ) ,
940
914
} ,
941
915
) ,
942
- // os_name == 'nt' and platform_system == 'Darwin'
943
- (
944
- MarkerExpression :: String {
945
- key : MarkerValueString :: OsName ,
946
- operator : MarkerOperator :: Equal ,
947
- value : "nt" . to_string ( ) ,
948
- } ,
949
- MarkerExpression :: String {
950
- key : MarkerValueString :: PlatformSystem ,
951
- operator : MarkerOperator :: Equal ,
952
- value : "Darwin" . to_string ( ) ,
953
- } ,
954
- ) ,
955
- // os_name == 'nt' and platform_system == 'Linux'
956
- (
957
- MarkerExpression :: String {
958
- key : MarkerValueString :: OsName ,
959
- operator : MarkerOperator :: Equal ,
960
- value : "nt" . to_string ( ) ,
961
- } ,
962
- MarkerExpression :: String {
963
- key : MarkerValueString :: PlatformSystem ,
964
- operator : MarkerOperator :: Equal ,
965
- value : "Linux" . to_string ( ) ,
966
- } ,
967
- ) ,
968
- // os_name == 'posix' and platform_system == 'Windows'
969
- (
970
- MarkerExpression :: String {
971
- key : MarkerValueString :: OsName ,
972
- operator : MarkerOperator :: Equal ,
973
- value : "posix" . to_string ( ) ,
974
- } ,
975
- MarkerExpression :: String {
976
- key : MarkerValueString :: PlatformSystem ,
977
- operator : MarkerOperator :: Equal ,
978
- value : "Windows" . to_string ( ) ,
979
- } ,
980
- ) ,
981
- ] {
916
+ ] ;
917
+
918
+ // Pairs of `platform_system` and `sys_platform` that are known to be incompatible.
919
+ //
920
+ // For example: `platform_system == 'FreeBSD' and sys_platform == 'darwin'`
921
+ //
922
+ // Any `platform_system` values that we normalize away (like `Windows` to `win32`) are
923
+ // omitted, since we never expect them to be present in the tree.
924
+ //
925
+ // Unfortunately, we can't include Cygwin here, since Cygwin appears to use a
926
+ // `platform_system` value with versions encoded (e.g., `CYGWIN_NT-10.0-22631).
927
+ //
928
+ for platform_system in [ "FreeBSD" , "NetBSD" , "OpenBSD" , "SunOS" , "iOS" , "iPadOS" ] {
929
+ // An enumeration of known values, excluding FreeBSD, SunOS, and other Unix systems,
930
+ // which use the lowercased `uname -s`, which typically includes a version (e.g.,
931
+ // `freebsd8`).
932
+ //
933
+ // See: https://docs.python.org/3/library/sys.html#sys.platform
934
+ for sys_platform in [
935
+ "aix" ,
936
+ "android" ,
937
+ "emscripten" ,
938
+ "ios" ,
939
+ "linux" ,
940
+ "darwin" ,
941
+ "win32" ,
942
+ "cygwin" ,
943
+ "wasi" ,
944
+ ] {
945
+ // Some of the above pairs are actually compatible.
946
+ if matches ! ( ( platform_system, sys_platform) , ( "iOS" | "iPadOS" , "ios" ) ) {
947
+ continue ;
948
+ }
949
+ pairs. push ( (
950
+ MarkerExpression :: String {
951
+ key : MarkerValueString :: PlatformSystem ,
952
+ operator : MarkerOperator :: Equal ,
953
+ value : platform_system. to_string ( ) ,
954
+ } ,
955
+ MarkerExpression :: String {
956
+ key : MarkerValueString :: SysPlatform ,
957
+ operator : MarkerOperator :: Equal ,
958
+ value : sys_platform. to_string ( ) ,
959
+ } ,
960
+ ) ) ;
961
+ }
962
+ }
963
+
964
+ for ( a, b) in pairs {
982
965
let a = self . expression ( a) ;
983
966
let b = self . expression ( b) ;
984
967
let a_and_b = conjunction ( self , a, b) ;
985
968
tree = disjunction ( self , tree, a_and_b) ;
986
969
}
970
+
987
971
self . state . exclusions = Some ( tree) ;
988
972
tree
989
973
}
0 commit comments