4848ATOMS = frozenset ([tokenize .NAME , tokenize .NUMBER , tokenize .STRING ])
4949
5050EXCEPT_REGEX = re .compile (r'^\s*except [\s,()\w]+ as \w+:$' )
51- PYTHON_SHEBANG_REGEX = re .compile (r'^#!.*\bpython[23 ]?\b\s*$' )
51+ PYTHON_SHEBANG_REGEX = re .compile (r'^#!.*\bpython[3 ]?\b\s*$' )
5252
5353MAX_PYTHON_FILE_DETECTION_BYTES = 1024
5454
55- try :
56- unicode
57- except NameError :
58- unicode = str
59-
60-
61- try :
62- RecursionError
63- except NameError :
64- # Python before 3.5.
65- RecursionError = RuntimeError
66-
6755
6856def standard_paths ():
6957 """Yield paths to standard modules."""
@@ -72,14 +60,12 @@ def standard_paths():
7260 # Yield lib paths.
7361 path = distutils .sysconfig .get_python_lib (standard_lib = True ,
7462 plat_specific = is_plat_spec )
75- for name in os .listdir (path ):
76- yield name
63+ yield from os .listdir (path )
7764
7865 # Yield lib-dynload paths.
7966 dynload_path = os .path .join (path , 'lib-dynload' )
8067 if os .path .isdir (dynload_path ):
81- for name in os .listdir (dynload_path ):
82- yield name
68+ yield from os .listdir (dynload_path )
8369
8470
8571def standard_package_names ():
@@ -118,8 +104,8 @@ def unused_import_module_name(messages):
118104 for message in messages :
119105 if isinstance (message , pyflakes .messages .UnusedImport ):
120106 module_name = re .search (pattern , str (message ))
121- module_name = module_name .group ()[1 :- 1 ]
122107 if module_name :
108+ module_name = module_name .group ()[1 :- 1 ]
123109 yield (message .lineno , module_name )
124110
125111
@@ -184,15 +170,6 @@ def create_key_to_messages_dict(messages):
184170
185171def check (source ):
186172 """Return messages from pyflakes."""
187- if sys .version_info [0 ] == 2 and isinstance (source , unicode ):
188- # Convert back to original byte string encoding, otherwise pyflakes
189- # call to compile() will complain. See PEP 263. This only affects
190- # Python 2.
191- try :
192- source = source .encode ('utf-8' )
193- except UnicodeError : # pragma: no cover
194- return []
195-
196173 reporter = ListReporter ()
197174 try :
198175 pyflakes .api .check (source , filename = '<string>' , reporter = reporter )
@@ -201,7 +178,7 @@ def check(source):
201178 return reporter .messages
202179
203180
204- class StubFile ( object ) :
181+ class StubFile :
205182 """Stub out file for pyflakes."""
206183
207184 def write (self , * _ ):
@@ -267,7 +244,7 @@ def multiline_statement(line, previous_line=''):
267244 return True
268245
269246
270- class PendingFix ( object ) :
247+ class PendingFix :
271248 """Allows a rewrite operation to span multiple lines.
272249
273250 In the main rewrite loop, every time a helper function returns a
@@ -835,8 +812,8 @@ def _fix_file(input_file, filename, args, write_to_stdout, standard_out,
835812 if original_source != filtered_source :
836813 if args .check :
837814 standard_out .write (
838- '{filename}: Unused imports/variables detected\n ' . format (
839- filename = filename ) )
815+ f '{ filename } : Unused imports/variables detected\n ',
816+ )
840817 sys .exit (1 )
841818 if write_to_stdout :
842819 standard_out .write (filtered_source )
@@ -866,8 +843,8 @@ def open_with_encoding(filename, encoding, mode='r',
866843 if not encoding :
867844 encoding = detect_encoding (filename , limit_byte_check = limit_byte_check )
868845
869- return io . open (filename , mode = mode , encoding = encoding ,
870- newline = '' ) # Preserve line endings
846+ return open (filename , mode = mode , encoding = encoding ,
847+ newline = '' ) # Preserve line endings
871848
872849
873850def detect_encoding (filename , limit_byte_check = - 1 ):
@@ -934,7 +911,7 @@ def is_python_file(filename):
934911 if not text :
935912 return False
936913 first_line = text .splitlines ()[0 ]
937- except (IOError , IndexError ):
914+ except (OSError , IndexError ):
938915 return False
939916
940917 if not PYTHON_SHEBANG_REGEX .match (first_line ):
@@ -1075,8 +1052,8 @@ def _main(argv, standard_out, standard_error, standard_input=None):
10751052 else :
10761053 try :
10771054 fix_file (name , args = args , standard_out = standard_out )
1078- except IOError as exception :
1079- _LOGGER .error (unicode (exception ))
1055+ except OSError as exception :
1056+ _LOGGER .error (str (exception ))
10801057 failure = True
10811058
10821059 return 1 if failure else 0
0 commit comments