Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstration of old python code using pyupgrade #2100

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: pip install isort pycln
- run: pip install pyupgrade pycln isort
# Ignore adodbapi until this has been manually ran and merged in adodbapi code
# _dbgscript.py is non-UTF8 on purpose, which is not supported
# TODO: Progressively remove the keep-* flags and add --py38-plus
- run: pyupgrade --keep-percent-format --keep-mock --keep-runtime-typing $(git ls-files '**.py*' ':!:adodbapi/*' ':!:Pythonwin/pywin/test/_dbgscript.py')
- run: pycln . --config=pycln.toml --check
- run: isort . --diff --check-only
- uses: psf/black@stable
Expand Down
2 changes: 1 addition & 1 deletion AutoDuck/Dump2HHC.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def main():
cats = parseCategories()
for cat in cats:
file = os.path.join(gen_dir, cat.dump_file)
input = open(file, "r")
input = open(file)
parseTopics(cat, input)
input.close()

Expand Down
2 changes: 1 addition & 1 deletion AutoDuck/InsertExternalOverviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def main():
print("Invalid args")
sys.exit(1)
file = sys.argv[1]
input = open(file, "r")
input = open(file)
out = open(file + ".2", "w")
doc = document_object.GetDocument()
linksHTML = genLinksHTML(doc.links)
Expand Down
2 changes: 1 addition & 1 deletion AutoDuck/TOCToHHK.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def main():
file = sys.argv[1]
output = sys.argv[2]
input = open(file, "r")
input = open(file)
out = open(output, "w")
line = input.readline()
out.write(
Expand Down
6 changes: 3 additions & 3 deletions Pythonwin/Scintilla/src/LexGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def UpdateFile(filename, updated):
it as modified."""
try:
infile = open(filename, "rb")
except IOError: # File is not there yet
except OSError: # File is not there yet
out = open(filename, "wb")
out.write(updated)
out.close()
Expand Down Expand Up @@ -135,8 +135,8 @@ def Generate(inpath, outpath, commentPrefix, eolType, *lists):
# print "generate '%s' -> '%s' (comment prefix: %r, eols: %r)"\
# % (inpath, outpath, commentPrefix, eolType)
try:
infile = open(inpath, "r")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know Scintilla is vendored, but these changes are automated. Just like pycln, isort and black, is that fine? I assume the idea is that we trust this won't lead to behaviour changes and that updating Scintilla won't cause these changes to be lost because they'll be automatically re-applied.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem then will be that re-vendoring in the future would break, meaning we'd need to work out what to re-run etc. It seems better long term to just ignore scintilla entirely, which works both today and in the future.

except IOError:
infile = open(inpath)
except OSError:
print("Can not open", inpath)
return
original = infile.read()
Expand Down
2 changes: 1 addition & 1 deletion Pythonwin/pywin/framework/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def OnInitDialog(self):
open(os.path.join(site_packages, "pywin32.version.txt")).read().strip()
)
ver = "pywin32 build %s" % build_no
except EnvironmentError:
except OSError:
ver = None
if ver is None:
# See if we are Part of Active Python
Expand Down
2 changes: 1 addition & 1 deletion Pythonwin/pywin/framework/bitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def OnOpenDocument(self, filename):
try:
try:
self.bitmap.LoadBitmapFile(f)
except IOError:
except OSError:
win32ui.MessageBox("Could not load the bitmap from %s" % filename)
return 0
finally:
Expand Down
6 changes: 3 additions & 3 deletions Pythonwin/pywin/framework/editor/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ def OnSaveDocument(self, fileName):
# and the file may be a hard-link, causing the link
# to follow the backup.
shutil.copy2(fileName, bakFileName)
except (os.error, NameError, IOError):
except (os.error, NameError, OSError):
pass
try:
self.SaveFile(fileName)
except IOError as details:
except OSError as details:
win32ui.MessageBox("Error - could not save file\r\n\r\n%s" % details)
return 0
except (UnicodeEncodeError, LookupError) as details:
Expand All @@ -99,7 +99,7 @@ def OnSaveDocument(self, fileName):
if rc == win32con.IDYES:
try:
self.SaveFile(fileName, encoding="latin-1")
except IOError as details:
except OSError as details:
win32ui.MessageBox(
"Error - could not save file\r\n\r\n%s" % details
)
Expand Down
2 changes: 1 addition & 1 deletion Pythonwin/pywin/framework/editor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def OnOpenDocument(self, filename):
win32ui.SetStatusText("Loading file...", 1)
try:
f = open(filename, "rb")
except IOError:
except OSError:
win32ui.MessageBox(
filename
+ "\nCan not find this file\nPlease verify that the correct path and file name are given"
Expand Down
4 changes: 2 additions & 2 deletions Pythonwin/pywin/framework/mdi_pychecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def OnOpenDocument(self, fnm):
# and starting a new grep can communicate the default parameters to the
# new grep.
try:
params = open(fnm, "r").read()
params = open(fnm).read()
except:
params = None
self.setInitParams(params)
Expand Down Expand Up @@ -386,7 +386,7 @@ def _inactive_idleHandler(self, handler, count):
if self.verbose:
self.GetFirstView().Append("# .." + f + "\n")
win32ui.SetStatusText("Searching " + f, 0)
lines = open(f, "r").readlines()
lines = open(f).readlines()
for i in range(len(lines)):
line = lines[i]
if self.pat.search(line) is not None:
Expand Down
8 changes: 4 additions & 4 deletions Pythonwin/pywin/framework/scriptutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def IsOnPythonPath(path):

def GetPackageModuleName(fileName):
"""Given a filename, return (module name, new path).
eg - given "c:\a\b\c\my.py", return ("b.c.my",None) if "c:\a" is on sys.path.
If no package found, will return ("my", "c:\a\b\c")
eg - given "c:\a\b\\c\\my.py", return ("b.c.my",None) if "c:\a" is on sys.path.
If no package found, will return ("my", "c:\a\b\\c")
"""
path, fname = os.path.split(fileName)
path = origPath = win32ui.FullPath(path)
Expand Down Expand Up @@ -308,7 +308,7 @@ def RunScript(defName=None, defArgs=None, bShowDialog=1, debuggingType=None):
# So: do the binary thing and manually normalize \r\n.
try:
f = open(script, "rb")
except IOError as exc:
except OSError as exc:
win32ui.MessageBox(
"The file could not be opened - %s (%d)" % (exc.strerror, exc.errno)
)
Expand Down Expand Up @@ -505,7 +505,7 @@ def CheckFile():
win32ui.DoWaitCursor(1)
try:
f = open(pathName)
except IOError as details:
except OSError as details:
print("Cant open file '%s' - %s" % (pathName, details))
return
try:
Expand Down
4 changes: 2 additions & 2 deletions Pythonwin/pywin/framework/sgrepmdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def OnOpenDocument(self, fnm):
# and starting a new grep can communicate the default parameters to the
# new grep.
try:
params = open(fnm, "r").read()
params = open(fnm).read()
except:
params = None
self.setInitParams(params)
Expand Down Expand Up @@ -299,7 +299,7 @@ def SearchFile(self, handler, count):
# while grep is running
if os.path.isfile(f):
win32ui.SetStatusText("Searching " + f, 0)
lines = open(f, "r").readlines()
lines = open(f).readlines()
for i in range(len(lines)):
line = lines[i]
if self.pat.search(line) is not None:
Expand Down
2 changes: 1 addition & 1 deletion Pythonwin/pywin/framework/winout.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def OnSaveDocument(self, fileName):
win32ui.SetStatusText("Saving file...", 1)
try:
self.SaveFile(fileName)
except IOError as details:
except OSError as details:
win32ui.MessageBox("Error - could not save file\r\n\r\n%s" % details)
return 0
win32ui.SetStatusText("Ready")
Expand Down
4 changes: 2 additions & 2 deletions Pythonwin/pywin/scintilla/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __init__(self, f):
return # We are ready to roll!
finally:
cf.close()
except (os.error, IOError, EOFError):
except (os.error, OSError, EOFError):
pass
fp = open(f)
b_close = True
Expand Down Expand Up @@ -167,7 +167,7 @@ def __init__(self, f):
marshal.dump(src_stat[stat.ST_MTIME], cf)
marshal.dump(self.cache, cf)
cf.close()
except (IOError, EOFError):
except (OSError, EOFError):
pass # Ignore errors - may be read only.

def configure(self, editor, subsections=None):
Expand Down
4 changes: 2 additions & 2 deletions Pythonwin/pywin/scintilla/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def OnOpenDocument(self, filename):
self._LoadTextFromFile(f)
finally:
f.close()
except IOError:
except OSError:
rc = win32ui.MessageBox(
"Could not load the file from %s\n\nDo you want to create a new file?"
% filename,
Expand All @@ -63,7 +63,7 @@ def OnOpenDocument(self, filename):
self._LoadTextFromFile(f)
finally:
f.close()
except IOError as e:
except OSError as e:
rc = win32ui.MessageBox("Cannot create the file %s" % filename)
return 1

Expand Down
1 change: 0 additions & 1 deletion Pythonwin/pywin/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

4 changes: 2 additions & 2 deletions com/win32com/client/gencache.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__():
# Initialize the module. Called once explicitly at module import below.
try:
_LoadDicts()
except IOError:
except OSError:
Rebuild()


Expand Down Expand Up @@ -100,7 +100,7 @@ def _LoadDicts():
except AttributeError:
# The __loader__ has no get_data method. See below.
return
except IOError:
except OSError:
# Our gencache is in a .zip file (and almost certainly readonly)
# but no dicts file. That actually needn't be fatal for a frozen
# application. Assuming they call "EnsureModule" with the same
Expand Down
2 changes: 1 addition & 1 deletion com/win32com/client/genpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ def open_writer(self, filename, encoding="mbcs"):
# don't step on each others' toes.
# Could be a classmethod one day...
temp_filename = self.get_temp_filename(filename)
return open(temp_filename, "wt", encoding=encoding)
return open(temp_filename, "w", encoding=encoding)

def finish_writer(self, filename, f, worked):
f.close()
Expand Down
2 changes: 1 addition & 1 deletion com/win32com/client/makepy.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ def main():
path = os.path.dirname(outputName)
if path != "" and not os.path.exists(path):
os.makedirs(path)
f = open(outputName, "wt", encoding="mbcs")
f = open(outputName, "w", encoding="mbcs")

else:
f = None
Expand Down
2 changes: 1 addition & 1 deletion com/win32com/demos/excelAddin.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def UnregisterAddin(klass):
winreg.HKEY_CURRENT_USER,
"Software\\Microsoft\\Office\\Excel\\Addins\\" + klass._reg_progid_,
)
except WindowsError:
except OSError:
pass


Expand Down
8 changes: 4 additions & 4 deletions com/win32com/demos/excelRTDServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ExcelRTDServer:

def __init__(self):
"""Constructor"""
super(ExcelRTDServer, self).__init__()
super().__init__()
self.IsAlive = self.ALIVE
self.__callback = None
self.topics = {}
Expand Down Expand Up @@ -266,7 +266,7 @@ class RTDTopic:
The others are more for convenience."""

def __init__(self, TopicStrings):
super(RTDTopic, self).__init__()
super().__init__()
self.TopicStrings = TopicStrings
self.__currentValue = None
self.__dirty = False
Expand Down Expand Up @@ -332,7 +332,7 @@ class TimeServer(ExcelRTDServer):
INTERVAL = 0.5 # secs. Threaded timer will wake us up at this interval.

def __init__(self):
super(TimeServer, self).__init__()
super().__init__()

# Simply timer thread to ensure we get to update our topics, and
# tell excel about any changes. This is a pretty basic and dirty way to
Expand Down Expand Up @@ -384,7 +384,7 @@ class TimeTopic(RTDTopic):
"""

def __init__(self, TopicStrings):
super(TimeTopic, self).__init__(TopicStrings)
super().__init__(TopicStrings)
try:
self.cmd, self.delay = self.TopicStrings
except Exception as E:
Expand Down
6 changes: 2 additions & 4 deletions com/win32com/demos/iebutton.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: latin-1 -*-

# PyWin32 Internet Explorer Button
#
# written by Leonard Ritter (paniq@gmx.net)
Expand Down Expand Up @@ -157,7 +155,7 @@ def register(classobj):
winreg.SetValueEx(hKey, "ToolTip", 0, winreg.REG_SZ, classobj._tool_tip_)
winreg.SetValueEx(hKey, "Icon", 0, winreg.REG_SZ, classobj._icon_)
winreg.SetValueEx(hKey, "HotIcon", 0, winreg.REG_SZ, classobj._hot_icon_)
except WindowsError:
except OSError:
print("Couldn't set standard toolbar reg keys.")
else:
print("Set standard toolbar reg keys.")
Expand All @@ -180,7 +178,7 @@ def unregister(classobj):
winreg.DeleteValue(hKey, "Icon")
winreg.DeleteValue(hKey, "HotIcon")
winreg.DeleteKey(winreg.HKEY_LOCAL_MACHINE, subKeyCLSID)
except WindowsError:
except OSError:
print("Couldn't delete Standard toolbar regkey.")
else:
print("Deleted Standard toolbar regkey.")
Expand Down
6 changes: 2 additions & 4 deletions com/win32com/demos/ietoolbar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: latin-1 -*-

# PyWin32 Internet Explorer Toolbar
#
# written by Leonard Ritter (paniq@gmx.net)
Expand Down Expand Up @@ -328,7 +326,7 @@ def DllRegisterServer():
subKey = winreg.SetValueEx(
hkey, comclass._reg_clsid_, 0, winreg.REG_BINARY, "\0"
)
except WindowsError:
except OSError:
print(
"Couldn't set registry value.\nhkey: %d\tCLSID: %s\n"
% (hkey, comclass._reg_clsid_)
Expand All @@ -351,7 +349,7 @@ def DllUnregisterServer():
winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Internet Explorer\\Toolbar"
)
winreg.DeleteValue(hkey, comclass._reg_clsid_)
except WindowsError:
except OSError:
print(
"Couldn't delete registry value.\nhkey: %d\tCLSID: %s\n"
% (hkey, comclass._reg_clsid_)
Expand Down
2 changes: 1 addition & 1 deletion com/win32com/demos/outlookAddin.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def UnregisterAddin(klass):
winreg.HKEY_CURRENT_USER,
"Software\\Microsoft\\Office\\Outlook\\Addins\\" + klass._reg_progid_,
)
except WindowsError:
except OSError:
pass


Expand Down
4 changes: 2 additions & 2 deletions com/win32com/makegw/makegwparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

class error_not_found(Exception):
def __init__(self, msg="The requested item could not be found"):
super(error_not_found, self).__init__(msg)
super().__init__(msg)


class error_not_supported(Exception):
def __init__(self, msg="The required functionality is not supported"):
super(error_not_supported, self).__init__(msg)
super().__init__(msg)


VERBOSE = 0
Expand Down
2 changes: 1 addition & 1 deletion com/win32com/olectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def MAKE_SCODE(sev, fac, code):
return int((int(-sev) << 31) | ((fac) << 16) | ((code)))
return int((int(-sev) << 31) | ((fac) << 16) | (code))


def STD_CTL_SCODE(n):
Expand Down
2 changes: 1 addition & 1 deletion com/win32com/test/testIterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def suite():
and issubclass(item, unittest.TestCase)
and item != _BaseTestCase
):
suite.addTest(unittest.makeSuite(item))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(item))
return suite


Expand Down
Loading