66import random
77import re
88import sys
9- from encodings .aliases import aliases
9+ from encodings .aliases import aliases as ALIASES
1010from functools import reduce , wraps
1111from importlib import import_module
1212from inspect import currentframe
4040 "DARWIN" , "LANG" , "LINUX" , "MASKS" , "PY3" , "UNIX" , "WINDOWS" ]
4141CODECS_REGISTRY = None
4242CODECS_CATEGORIES = ["native" , "custom" ]
43- try :
44- LANG = getlocale ()[0 ][:2 ].lower ()
45- except TypeError :
46- LANG = None
43+ LANG = getlocale ()[0 ][:2 ].lower () if getlocale () else None
4744MASKS = {
4845 'a' : printable ,
4946 'b' : "" .join (chr (i ) for i in range (256 )),
@@ -601,7 +598,7 @@ def examples(encoding, number=10):
601598 except LookupError :
602599 pass
603600 i += 1
604- for alias , codec in aliases .items ():
601+ for alias , codec in ALIASES .items ():
605602 if name == codec :
606603 if codec not in e :
607604 e .append (codec )
@@ -634,19 +631,25 @@ def list_encodings(*categories):
634631 # first, determine the list of valid categories
635632 valid_categories = list_categories ()
636633 # then, if "non-native" is in the input list, extend the list with the whole categories but "native"
637- categories = list (categories )
634+ categories , exclude = list (categories ), []
638635 for c in categories [:]:
639636 if c == "non-native" :
640637 for c in valid_categories :
641638 if c == "native" or c in categories :
642639 continue
643640 categories .append (c )
644641 categories .remove ("non-native" )
645- break
642+ if c .startswith ("~" ):
643+ exclude .append (c [1 :])
644+ categories .remove (c )
645+ try :
646+ categories .remove (c [1 :])
647+ except ValueError :
648+ pass
646649 # now, filter codecs according to the input list of categories
647650 enc = []
648- if len (categories ) == 0 or "native" in categories :
649- for a in set (aliases .values ()):
651+ if ( len (categories ) == 0 or "native" in categories ) and "native" not in exclude :
652+ for a in set (ALIASES .values ()):
650653 try :
651654 __orig_lookup (a )
652655 except LookupError :
@@ -660,7 +663,7 @@ def list_encodings(*categories):
660663 else :
661664 ci = search_function (generate_string_from_regex (p ))
662665 c = "other" if ci is None else ci .parameters ['category' ]
663- if len (categories ) == 0 or c in categories :
666+ if ( len (categories ) == 0 or c in categories ) and c not in exclude :
664667 enc .append (name )
665668 for category in categories :
666669 if category not in valid_categories :
@@ -873,7 +876,7 @@ def lookup(encoding, macro=True):
873876 try :
874877 # finally, get a CodecInfo with the original lookup function and refine it with a dictionary of parameters
875878 ci = __orig_lookup (encoding )
876- ci .parameters = {'category' : "native" , 'module' : "codecs" , 'name' : aliases .get (ci .name , ci .name )}
879+ ci .parameters = {'category' : "native" , 'module' : "codecs" , 'name' : ALIASES .get (ci .name , ci .name )}
877880 return ci
878881 except LookupError :
879882 if not macro :
@@ -932,7 +935,7 @@ def search(encoding_regex):
932935 if c >= 3 :
933936 matches .append (n )
934937 break
935- for s , n in aliases .items ():
938+ for s , n in ALIASES .items ():
936939 if re .search (encoding_regex , s ) or re .search (encoding_regex , n ):
937940 matches .append (n )
938941 break
0 commit comments