[pywin32-checkins] pywin32/com/win32com/client genpy.py, 1.56, 1.57 makepy.py, 1.26, 1.27
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Mark H. <mha...@us...> - 2008-11-26 02:17:02
|
Update of /cvsroot/pywin32/pywin32/com/win32com/client In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv16123 Modified Files: genpy.py makepy.py Log Message: rationalize the 2 places where we opened files for writing to automatically do the .temp and encoding thing, plus other py3k friendly changes. Index: genpy.py =================================================================== RCS file: /cvsroot/pywin32/pywin32/com/win32com/client/genpy.py,v retrieving revision 1.56 retrieving revision 1.57 diff -C2 -d -r1.56 -r1.57 *** genpy.py 14 Nov 2008 00:22:25 -0000 1.56 --- genpy.py 26 Nov 2008 01:46:30 -0000 1.57 *************** *** 98,101 **** --- 98,102 ---- # classes. class WritableItem: + # __cmp__ used for sorting in py2x... def __cmp__(self, other): "Compare for sorting" *************** *** 103,106 **** --- 104,112 ---- if ret==0 and self.doc: ret = cmp(self.doc[0], other.doc[0]) return ret + # ... but not used in py3k - __lt__ minimum needed there + def __lt__(self, other): # py3k variant + if self.order == other.order: + return self.doc < other.doc + return self.order < other.order def __repr__(self): return "OleItem: doc=%s, order=%d" % (repr(self.doc), self.order) *************** *** 205,209 **** if vdesc[4] == pythoncom.VAR_CONST: val = vdesc[1] ! if type(val) in (type(0), type(0L)): if val==0x80000000L: # special case use = "0x80000000L" # 'L' for future warning --- 211,215 ---- if vdesc[4] == pythoncom.VAR_CONST: val = vdesc[1] ! if sys.version_info <= (2,4) and (isinstance(val, int) or isinstance(val, long)): if val==0x80000000L: # special case use = "0x80000000L" # 'L' for future warning *************** *** 360,364 **** stream = generator.file # Write in alpha order. ! names = self.mapFuncs.keys() names.sort() specialItems = {"count":None, "item":None,"value":None,"_newenum":None} # If found, will end up with (entry, invoke_tupe) --- 366,370 ---- stream = generator.file # Write in alpha order. ! names = list(self.mapFuncs.keys()) names.sort() specialItems = {"count":None, "item":None,"value":None,"_newenum":None} # If found, will end up with (entry, invoke_tupe) *************** *** 761,764 **** --- 767,797 ---- return oleItems, enumItems, recordItems, vtableItems + def open_writer(self, filename, encoding="mbcs"): + # A place to put code to open a file with the appropriate encoding. + # Does *not* set self.file - just opens and returns a file. + # Actually *deletes* the filename asked for and returns a handle to a + # temp file - finish_writer then puts everything back in place. This + # is so errors don't leave a 1/2 generated file around causing bizarre + # errors later. + # Could be a classmethod one day... + try: + os.unlink(filename) + except os.error: + pass + filename = filename + ".temp" + if sys.version_info > (3,0): + ret = open(filename, "wt", encoding=encoding) + else: + import codecs # not available in py3k. + ret = codecs.open(filename, "wt", encoding) + return ret + + def finish_writer(self, filename, f, worked): + f.close() + if worked: + os.rename(filename + ".temp", filename) + else: + os.unlink(filename + ".temp") + def generate(self, file, is_for_demand = 0): if is_for_demand: *************** *** 782,786 **** self.bHaveWrittenCoClassBaseClass = 0 self.bHaveWrittenEventBaseClass = 0 ! encoding = self.file.encoding or "mbcs" print >> self.file, '# -*- coding: %s -*-' % (encoding,) --- 815,823 ---- self.bHaveWrittenCoClassBaseClass = 0 self.bHaveWrittenEventBaseClass = 0 ! # You must provide a file correctly configured for writing unicode. ! # We assert this is it may indicate somewhere in pywin32 that needs ! # upgrading. ! assert self.file.encoding, self.file ! encoding = self.file.encoding # or "mbcs" print >> self.file, '# -*- coding: %s -*-' % (encoding,) *************** *** 795,802 **** print >> self.file, 'makepy_version =', `makepy_version` ! try: ! print >> self.file, 'python_version = 0x%x' % (sys.hexversion,) ! except AttributeError: ! print >> self.file, 'python_version = 0x0 # Presumably Python 1.5.2 - 0x0 is not a problem' print >> self.file print >> self.file, 'import win32com.client.CLSIDToClass, pythoncom' --- 832,836 ---- print >> self.file, 'makepy_version =', `makepy_version` ! print >> self.file, 'python_version = 0x%x' % (sys.hexversion,) print >> self.file print >> self.file, 'import win32com.client.CLSIDToClass, pythoncom' *************** *** 836,842 **** if enumItems: print >> stream, "class constants:" ! list = enumItems.values() ! list.sort() ! for oleitem in list: oleitem.WriteEnumerationItems(stream) self.progress.Tick() --- 870,876 ---- if enumItems: print >> stream, "class constants:" ! items = enumItems.values() ! items.sort() ! for oleitem in items: oleitem.WriteEnumerationItems(stream) self.progress.Tick() *************** *** 844,857 **** if self.generate_type == GEN_FULL: ! list = oleItems.values() ! list = filter(lambda l: l is not None, list) ! list.sort() ! for oleitem in list: self.progress.Tick() oleitem.WriteClass(self) ! list = vtableItems.values() ! list.sort() ! for oleitem in list: self.progress.Tick() oleitem.WriteClass(self) --- 878,891 ---- if self.generate_type == GEN_FULL: ! items = oleItems.values() ! items = [l for l in items if l is not None] ! items.sort() ! for oleitem in items: self.progress.Tick() oleitem.WriteClass(self) ! items = vtableItems.values() ! items.sort() ! for oleitem in items: self.progress.Tick() oleitem.WriteClass(self) *************** *** 860,865 **** print >> stream, 'RecordMap = {' ! list = recordItems.values() ! for record in list: if str(record.clsid) == pythoncom.IID_NULL: print >> stream, "\t###%s: %s, # Typedef disabled because it doesn't have a non-null GUID" % (`record.doc[0]`, `str(record.clsid)`) --- 894,898 ---- print >> stream, 'RecordMap = {' ! for record in recordItems.itervalues(): if str(record.clsid) == pythoncom.IID_NULL: print >> stream, "\t###%s: %s, # Typedef disabled because it doesn't have a non-null GUID" % (`record.doc[0]`, `str(record.clsid)`) *************** *** 981,985 **** an_item = oleitem or vtableitem assert not self.file, "already have a file?" ! self.file = open(os.path.join(dir, an_item.python_name) + ".py", "w") try: if oleitem is not None: --- 1014,1022 ---- an_item = oleitem or vtableitem assert not self.file, "already have a file?" ! # like makepy.py, we gen to a .temp file so failure doesn't ! # leave a 1/2 generated mess. ! out_name = os.path.join(dir, an_item.python_name) + ".py" ! worked = False ! self.file = self.open_writer(out_name) try: if oleitem is not None: *************** *** 988,993 **** self.do_gen_child_item(vtableitem) self.progress.Tick() finally: ! self.file.close() self.file = None finally: --- 1025,1031 ---- self.do_gen_child_item(vtableitem) self.progress.Tick() + worked = True finally: ! self.finish_writer(out_name, self.file, worked) self.file = None finally: Index: makepy.py =================================================================== RCS file: /cvsroot/pywin32/pywin32/com/win32com/client/makepy.py,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** makepy.py 14 Nov 2008 00:22:25 -0000 1.26 --- makepy.py 26 Nov 2008 01:46:30 -0000 1.27 *************** *** 67,74 **** """ ! import genpy, string, sys, os, types, pythoncom ! import codecs ! import selecttlb ! import gencache from win32com.client import NeedUnicodeConversions, Dispatch --- 67,72 ---- """ ! import sys, os, pythoncom ! from win32com.client import genpy, selecttlb, gencache from win32com.client import NeedUnicodeConversions, Dispatch *************** *** 254,257 **** --- 252,257 ---- for typelib, info in typelibs: + gen = genpy.Generator(typelib, info.dll, progress, bUnicodeToString=bUnicodeToString, bBuildHidden=bBuildHidden) + if file is None: this_name = gencache.GetGeneratedFileName(info.clsid, info.lcid, info.major, info.minor) *************** *** 269,292 **** else: outputName = full_name + ".py" ! # generate to a temp file (so errors don't leave a 1/2 ! # generated file) and one which can handle unicode! ! try: ! os.unlink(outputName) ! except os.error: ! pass ! encoding = 'mbcs' # could make this a param. ! fileUse = codecs.open(outputName + ".temp", "wt", ! encoding) progress.LogBeginGenerate(outputName) else: fileUse = file ! gen = genpy.Generator(typelib, info.dll, progress, bUnicodeToString=bUnicodeToString, bBuildHidden=bBuildHidden) ! ! gen.generate(fileUse, bForDemand) ! ! if file is None: ! fileUse.close() ! os.rename(outputName + ".temp", outputName) if bToGenDir: progress.SetDescription("Importing module") --- 269,284 ---- else: outputName = full_name + ".py" ! fileUse = gen.open_writer(outputName) progress.LogBeginGenerate(outputName) else: fileUse = file ! worked = False ! try: ! gen.generate(fileUse, bForDemand) ! worked = True ! finally: ! if file is None: ! gen.finish_writer(outputName, fileUse, worked) if bToGenDir: progress.SetDescription("Importing module") |