From 22efab538eb31e6232f63b814077cae59c81c03d Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 22 Sep 2023 10:24:33 -0400 Subject: [PATCH] Update Python2 string module usage (#2127) --- Pythonwin/pywin/framework/bitmap.py | 6 +++--- Pythonwin/pywin/idle/PyParse.py | 2 -- com/help/active_directory.html | 4 ++-- com/help/adsi.html | 6 +++--- com/win32com/makegw/makegwenum.py | 3 +-- com/win32com/test/testDCOM.py | 5 ++--- com/win32comext/adsi/demos/test.py | 9 ++++----- com/win32comext/axdebug/debugger.py | 7 +++---- com/win32comext/axdebug/expressions.py | 5 ++--- .../axscript/demos/client/ie/calc.htm | 4 ++-- com/win32comext/axscript/server/axsite.py | 2 +- win32/help/event.d | 18 ++++++++---------- win32/help/security_directories.html | 11 +++++------ win32/scripts/VersionStamp/BrandProject.py | 3 +-- win32/scripts/VersionStamp/vssutil.py | 9 ++++----- win32/scripts/ce/pysynch.py | 10 ++++------ 16 files changed, 45 insertions(+), 59 deletions(-) diff --git a/Pythonwin/pywin/framework/bitmap.py b/Pythonwin/pywin/framework/bitmap.py index 1ec4d50f4..0099fbe59 100644 --- a/Pythonwin/pywin/framework/bitmap.py +++ b/Pythonwin/pywin/framework/bitmap.py @@ -144,9 +144,9 @@ def MatchDocType(self, fileName, fileType): # magic=file.readline() # if magic <> "P6\n": # raise TypeError, "The file is not a PPM format file" -# rowcollist=string.split(file.readline()) -# cols=string.atoi(rowcollist[0]) -# rows=string.atoi(rowcollist[1]) +# rowcollist=file.readline().split() +# cols=int(rowcollist[0]) +# rows=int(rowcollist[1]) # file.readline() # whats this one? # self.bitmap.LoadPPMFile(file,(cols,rows)) diff --git a/Pythonwin/pywin/idle/PyParse.py b/Pythonwin/pywin/idle/PyParse.py index b11b4744e..fa34ff589 100644 --- a/Pythonwin/pywin/idle/PyParse.py +++ b/Pythonwin/pywin/idle/PyParse.py @@ -1,5 +1,4 @@ import re -import string import sys # Reason last stmt is continued (or C_NONE if it's not). @@ -362,7 +361,6 @@ def get_continuation_type(self): # if continuation is C_BRACKET, index of last open bracket def _study2(self): - _ws = string.whitespace if self.study_level >= 2: return self._study1() diff --git a/com/help/active_directory.html b/com/help/active_directory.html index a7309642a..d7764002b 100644 --- a/com/help/active_directory.html +++ b/com/help/active_directory.html @@ -114,7 +114,7 @@

Discovery

for i in servers: ex_servers.append(i.cn) - print '\texchange servers',string.join(ex_servers) + print '\texchange servers'," ".join(ex_servers) ex_first_store='CN=First Storage Group,CN=InformationStore,CN=%s,CN=Servers,CN=%s,%s'%(ex_servers[-1],admin_grp,ex_admin_grps) @@ -122,7 +122,7 @@

Discovery

for i in win32com.client.GetObject('LDAP://'+ex_first_store): ex_stores.append('cn='+i.cn+','+ex_first_store) - print '\tExchange stores:',string.join(ex_stores,"',") + print '\tExchange stores:',"',".join(ex_stores)

Making the object

diff --git a/com/help/adsi.html b/com/help/adsi.html index 77415bc1d..6fd2ab119 100644 --- a/com/help/adsi.html +++ b/com/help/adsi.html @@ -228,11 +228,11 @@

Recursively listing all unique members of a distribution dsobj = ldap.OpenDSObject(path,logon_ex,password,0) dsobj.Getinfo() if dsobj.Class=='organizationalPerson': - user_dict[string.capitalize(dsobj.cn)]=dsobj.uid + user_dict[dsobj.cn.capitalize()]=dsobj.uid elif dsobj.Class=='groupOfNames': for i in dsobj.Members(): if i.Class=='organizationalPerson': - user_dict[string.capitalize(i.cn)]=i.uid + user_dict[i.cn.capitalize()]=i.uid elif type(i.member)==types.TupleType: for j in i.member: newpath='LDAP://'+server+'/'+j @@ -241,7 +241,7 @@

Recursively listing all unique members of a distribution newpath='LDAP://'+server+'/'+i.member getmembers(newpath) elif dsobj.Class=='Remote-Address': - User_dict[string.capitalize(dsobj.cn)]=dsobj.uid + User_dict[dsobj.cn.capitalize()]=dsobj.uid elif dsobj.Class=='Public-Folder': pass else: diff --git a/com/win32com/makegw/makegwenum.py b/com/win32com/makegw/makegwenum.py index 39c9b2d19..90acc613a 100644 --- a/com/win32com/makegw/makegwenum.py +++ b/com/win32com/makegw/makegwenum.py @@ -11,11 +11,10 @@ # INTERNAL FUNCTIONS # # -import string def is_interface_enum(enumtype): - return not (enumtype[0] in string.uppercase and enumtype[2] in string.uppercase) + return not (enumtype[0].isupper() and enumtype[2].isupper()) def _write_enumifc_cpp(f, interface): diff --git a/com/win32com/test/testDCOM.py b/com/win32com/test/testDCOM.py index 9f5a334e1..9dedf5d10 100644 --- a/com/win32com/test/testDCOM.py +++ b/com/win32com/test/testDCOM.py @@ -12,7 +12,6 @@ The Python.Interpreter object must be installed on the local machine, but no special DCOM configuration should be necessary. """ -import string import sys # NOTE: If you configured the object locally using dcomcnfg, you could @@ -23,7 +22,7 @@ def test(serverName): - if string.lower(serverName) == string.lower(win32api.GetComputerName()): + if serverName.lower() == win32api.GetComputerName().lower(): print("You must specify a remote server name, not the local machine!") return @@ -34,7 +33,7 @@ def test(serverName): ob = win32com.client.DispatchEx("Python.Interpreter", serverName, clsctx=clsctx) ob.Exec("import win32api") actualName = ob.Eval("win32api.GetComputerName()") - if string.lower(serverName) != string.lower(actualName): + if serverName.lower() != actualName.lower(): print( "Error: The object created on server '%s' reported its name as '%s'" % (serverName, actualName) diff --git a/com/win32comext/adsi/demos/test.py b/com/win32comext/adsi/demos/test.py index d5b8fde98..b0e15f5c6 100644 --- a/com/win32comext/adsi/demos/test.py +++ b/com/win32comext/adsi/demos/test.py @@ -1,4 +1,3 @@ -import string import sys import pythoncom @@ -31,12 +30,12 @@ def DumpRoot(): # Reading attributeSchema and classSchema Objects def _DumpClass(child): attrs = "Abstract lDAPDisplayName schemaIDGUID schemaNamingContext attributeSyntax oMSyntax" - _DumpTheseAttributes(child, string.split(attrs)) + _DumpTheseAttributes(child, attrs.split()) def _DumpAttribute(child): attrs = "lDAPDisplayName schemaIDGUID adminDescription adminDisplayName rDNAttID defaultHidingValue defaultObjectCategory systemOnly defaultSecurityDescriptor" - _DumpTheseAttributes(child, string.split(attrs)) + _DumpTheseAttributes(child, attrs.split()) def _DumpTheseAttributes(child, attrs): @@ -144,7 +143,7 @@ def DumpSchema2(): schema = ADsGetObject(path, IID_IADsContainer) nclass = nprop = nsyntax = 0 for item in schema: - item_class = string.lower(item.Class) + item_class = item.Class.lower() if item_class == "class": items = [] if item.Abstract: @@ -152,7 +151,7 @@ def DumpSchema2(): if item.Auxiliary: items.append("Auxiliary") # if item.Structural: items.append("Structural") - desc = string.join(items, ", ") + desc = ", ".join(items) import win32com.util iid_name = win32com.util.IIDToInterfaceName(item.PrimaryInterface) diff --git a/com/win32comext/axdebug/debugger.py b/com/win32comext/axdebug/debugger.py index 3157586bc..2630e9188 100644 --- a/com/win32comext/axdebug/debugger.py +++ b/com/win32comext/axdebug/debugger.py @@ -1,5 +1,4 @@ import os -import string import sys import pythoncom @@ -46,7 +45,7 @@ def BuildModule(module, built_nodes, rootNode, create_node_fn, create_node_args) keep = module.__name__ keep = keep and (built_nodes.get(module) is None) if keep and hasattr(module, "__file__"): - keep = string.lower(os.path.splitext(module.__file__)[1]) not in [ + keep = os.path.splitext(module.__file__)[1].lower() not in [ ".pyd", ".dll", ] @@ -59,10 +58,10 @@ def BuildModule(module, built_nodes, rootNode, create_node_fn, create_node_args) node.realNode = realNode # Split into parent nodes. - parts = string.split(module.__name__, ".") + parts = module.__name__.split(".") if parts[-1][:8] == "__init__": parts = parts[:-1] - parent = string.join(parts[:-1], ".") + parent = ".".join(parts[:-1]) parentNode = rootNode if parent: parentModule = sys.modules[parent] diff --git a/com/win32comext/axdebug/expressions.py b/com/win32comext/axdebug/expressions.py index e573eb514..67bf148e7 100644 --- a/com/win32comext/axdebug/expressions.py +++ b/com/win32comext/axdebug/expressions.py @@ -1,5 +1,4 @@ import io -import string import sys import traceback from pprint import pprint @@ -15,7 +14,7 @@ def MakeNiceString(ob): stream = io.StringIO() pprint(ob, stream) - return string.strip(stream.getvalue()) + return stream.getvalue().strip() class ProvideExpressionContexts(gateways.ProvideExpressionContexts): @@ -65,7 +64,7 @@ def Start(self, callback): sys.exc_info()[0], sys.exc_info()[1] ) # l is a list of strings with trailing "\n" - self.result = string.join((s[:-1] for s in l), "\n") + self.result = "\n".join(s[:-1] for s in l) self.hresult = winerror.E_FAIL finally: self.isComplete = 1 diff --git a/com/win32comext/axscript/demos/client/ie/calc.htm b/com/win32comext/axscript/demos/client/ie/calc.htm index 7c7fb48d4..46ef9a41f 100644 --- a/com/win32comext/axscript/demos/client/ie/calc.htm +++ b/com/win32comext/axscript/demos/client/ie/calc.htm @@ -48,7 +48,7 @@ PendingOp = NullOp else: FlagNewNum = 1 - Accum = PendingOp( Accum, string.atof(ReadOut) ) + Accum = PendingOp( Accum, float(ReadOut) ) ax.document.Keypad.ReadOut.Value = str(Accum) PendingOp = fn @@ -65,7 +65,7 @@ ClearEntry_OnClick() def Neg_OnClick(): - ax.document.Keypad.ReadOut.Value = str(-string.atof(ax.document.Keypad.ReadOut.Value)) + ax.document.Keypad.ReadOut.Value = str(-float(ax.document.Keypad.ReadOut.Value)) diff --git a/com/win32comext/axscript/server/axsite.py b/com/win32comext/axscript/server/axsite.py index 8f0dd33b1..29fa0ebe9 100644 --- a/com/win32comext/axscript/server/axsite.py +++ b/com/win32comext/axscript/server/axsite.py @@ -76,7 +76,7 @@ def __init__(self, objModel={}, engine=None, lcid=0): self.lcid = lcid self.objModel = {} for name, object in objModel.items(): - # Gregs code did string.lower this - I think that is callers job if he wants! + # Gregs code did str.lower this - I think that is callers job if he wants! self.objModel[name] = object self.engine = None diff --git a/win32/help/event.d b/win32/help/event.d index 1110bb527..baf4c349a 100644 --- a/win32/help/event.d +++ b/win32/help/event.d @@ -64,8 +64,8 @@ def date2sec(self,evt_date): reg_result=regexp.search(evt_date) date=reg_result.group(1) the_time=reg_result.group(2) - (mon,day,yr)=map(lambda x: string.atoi(x),string.split(date,'/')) - (hr,min,sec)=map(lambda x: string.atoi(x),string.split(the_time,':')) + (mon,day,yr)=map(lambda x: int(x),date.split('/')) + (hr,min,sec)=map(lambda x: int(x),the_time.split(':')) tup=[yr,mon,day,hr,min,sec,0,0,0] sec=time.mktime(tup) return sec @@ -86,7 +86,6 @@ import win32con import winerror import time import re -import string import sys import traceback @@ -99,8 +98,8 @@ def date2sec(evt_date): reg_result=regexp.search(evt_date) date=reg_result.group(1) the_time=reg_result.group(2) - (mon,day,yr)=map(lambda x: string.atoi(x),string.split(date,'/')) - (hr,min,sec)=map(lambda x: string.atoi(x),string.split(the_time,':')) + (mon,day,yr)=map(lambda x: int(x),date.split('/')) + (hr,min,sec)=map(lambda x: int(x),the_time.split(':')) tup=[yr,mon,day,hr,min,sec,0,0,0] sec=time.mktime(tup) @@ -146,7 +145,7 @@ try: evt_id=str(winerror.HRESULT_CODE(ev_obj.EventID)) evt_type=str(evt_dict[ev_obj.EventType]) msg = str(win32evtlogutil.SafeFormatMessage(ev_obj, logtype)) - print string.join((the_time,computer,src,cat,record,evt_id,evt_type,msg[0:15]),':') + print(':'.join((the_time,computer,src,cat,record,evt_id,evt_type,msg[0:15]))) if seconds < begin_sec-28800: break #get out of while loop as well win32evtlog.CloseEventLog(hand) @@ -275,7 +274,6 @@ import win32con import winerror import time import re -import string import sys import threading import traceback @@ -323,7 +321,7 @@ class thread_it ( threading.Thread ) : evt_id=str(winerror.HRESULT_CODE(ev_obj.EventID)) evt_type=str(evt_dict[ev_obj.EventType]) msg = str(win32evtlogutil.SafeFormatMessage(ev_obj, logtype)) - results=string.join((now_time,the_time,computer,src,cat,record,evt_id,evt_type,msg[0:15]),':') + results=':'.join((now_time,the_time,computer,src,cat,record,evt_id,evt_type,msg[0:15])) self.data.append(results) if seconds < begin_sec-28800: break win32evtlog.CloseEventLog(hand) @@ -340,8 +338,8 @@ class thread_it ( threading.Thread ) : date=reg_result.group(1) the_time=reg_result.group(2) - (mon,day,yr)=map(lambda x: string.atoi(x),string.split(date,'/')) - (hr,min,sec)=map(lambda x: string.atoi(x),string.split(the_time,':')) + (mon,day,yr)=map(lambda x: int(x),date.split('/')) + (hr,min,sec)=map(lambda x: int(x),the_time.split(':')) tup=[yr,mon,day,hr,min,sec,0,0,0] sec=time.mktime(tup) diff --git a/win32/help/security_directories.html b/win32/help/security_directories.html index 194698e7e..dcdfa9516 100644 --- a/win32/help/security_directories.html +++ b/win32/help/security_directories.html @@ -323,7 +323,6 @@

Python code

import os import sys import win32net -import string import time import copy import getopt @@ -383,7 +382,7 @@

Python code

#get the server for the domain -- it has to be a primary dc group=0 resume=0 - sys_id=string.strip(sys_id) + sys_id=sys_id.strip() if D_group.has_key(sys_id): group=1 elif D_except.has_key(sys_id): @@ -405,7 +404,7 @@

Python code

def get_perm_base(file): all_perms=fileperm.get_perms(file) for (domain_id,mask) in all_perms.items(): - (domain,sys_id)=string.split(domain_id,'\\',1) + (domain,sys_id)=domain_id.split('\\',1) mask_name=get_mask(mask) Results.append(file+','+sys_id+','+mask_name) @@ -414,7 +413,7 @@

Python code

perm_list.append(file) all_perms=fileperm.get_perms(file) for (domain_id,mask) in all_perms.items(): - (domain,sys_id)=string.split(domain_id,'\\',1) + (domain,sys_id)=domain_id.split('\\',1) print domain,sys_id sys_id=str(sys_id) mask_name=get_mask(mask) @@ -435,8 +434,8 @@

Python code

continue all_perms=fileperm.get_perms(file) for (domain_id,mask) in all_perms.items(): - if string.find(domain_id,'\\')!=-1: - (domain,sys_id)=string.split(domain_id,'\\',1) + if domain_id.find('\\')!=-1: + (domain,sys_id)=domain_id.split('\\',1) else: sys_id=domain_id mask_name=get_mask(mask) diff --git a/win32/scripts/VersionStamp/BrandProject.py b/win32/scripts/VersionStamp/BrandProject.py index 6a0cf1716..cfa9ca0c9 100644 --- a/win32/scripts/VersionStamp/BrandProject.py +++ b/win32/scripts/VersionStamp/BrandProject.py @@ -4,7 +4,6 @@ # stamp DLL/EXE files with version information. import os -import string import sys import bulkstamp @@ -77,7 +76,7 @@ def usage(msg): if opt == "-a": bAuto = 1 if opt == "-f": - infile, outfile = string.split(val, "=", 2) + infile, outfile = val.split("=", 2) stampFiles.append((infile, outfile)) if opt == "-d": desc = val diff --git a/win32/scripts/VersionStamp/vssutil.py b/win32/scripts/VersionStamp/vssutil.py index 6679ebc93..23be6278d 100644 --- a/win32/scripts/VersionStamp/vssutil.py +++ b/win32/scripts/VersionStamp/vssutil.py @@ -1,4 +1,3 @@ -import string import time import traceback @@ -37,7 +36,7 @@ def test(projectName): def SubstituteInString(inString, evalEnv): substChar = "$" - fields = string.split(inString, substChar) + fields = inString.split(substChar) newFields = [] for i in range(len(fields)): didSubst = 0 @@ -52,7 +51,7 @@ def SubstituteInString(inString, evalEnv): print("Could not substitute", strVal) if not didSubst: newFields.append(strVal) - return string.join(map(str, newFields), "") + return "".join(map(str, newFields)) def SubstituteInFile(inName, outName, evalEnv): @@ -102,7 +101,7 @@ def VssLog(project, linePrefix="", noLabels=5, maxItems=150): ) if labelNum > noLabels: break - return string.join(lines, "\n") + return "\n".join(lines) def SubstituteVSSInFile(projectName, inName, outName): @@ -171,7 +170,7 @@ def MakeNewBuildNo(project, buildDesc=None, auto=0, bRebrand=0): oldBuild = "" else: try: - buildNo = string.atoi(buildNo) + buildNo = int(buildNo) if not bRebrand: buildNo = buildNo + 1 buildNo = str(buildNo) diff --git a/win32/scripts/ce/pysynch.py b/win32/scripts/ce/pysynch.py index c3054f0e2..3244ae45d 100644 --- a/win32/scripts/ce/pysynch.py +++ b/win32/scripts/ce/pysynch.py @@ -3,7 +3,6 @@ import fnmatch import getopt import os -import string import sys import win32api @@ -62,7 +61,6 @@ def CopyFileToCe(src_name, dest_name, progress=None): if progress is not None: progress(bytes) finally: - pass dh.Close() finally: sh.Close() @@ -144,9 +142,9 @@ def copy(args): src = args[:-1] dest = args[-1] # See if WCE: leading anywhere indicates a direction. - if string.find(src[0], "WCE:") == 0: + if src[0].find("WCE:") == 0: bToDevice = 0 - elif string.find(dest, "WCE:") == 0: + elif dest.find("WCE:") == 0: bToDevice = 1 else: # Assume copy to device. @@ -224,7 +222,7 @@ def run(args): prog_args.append('"' + arg + '"') else: prog_args.append(arg) - prog_args = string.join(prog_args, " ") + prog_args = " ".join(prog_args) wincerapi.CeCreateProcess(prog_args, "", None, None, 0, 0, None, "", None) @@ -247,7 +245,7 @@ def DumpCommands(): if isinstance(item, Callable): doc = getattr(item, "__doc__", "") if doc: - lines = string.split(doc, "\n") + lines = doc.split("\n") print("%-10s - %s" % (name, lines[0])) for line in lines[1:]: if line: