|
19 | 19 | from itertools import chain |
20 | 20 |
|
21 | 21 | import pythoncom |
22 | | -import win32com |
23 | 22 |
|
24 | | -from . import build |
| 23 | +from . import build, gencache |
25 | 24 |
|
26 | 25 | makepy_version = "0.5.01" # Written to generated file. |
27 | 26 |
|
@@ -1017,35 +1016,15 @@ def open_writer(self, filename, encoding="utf-8"): |
1017 | 1016 |
|
1018 | 1017 | def finish_writer(self, filename, f, worked): |
1019 | 1018 | f.close() |
1020 | | - try: |
1021 | | - os.unlink(filename) |
1022 | | - except OSError: |
1023 | | - pass |
1024 | 1019 | temp_filename = self.get_temp_filename(filename) |
1025 | 1020 | if worked: |
| 1021 | + os.replace(temp_filename, filename) |
| 1022 | + else: |
1026 | 1023 | try: |
1027 | | - os.rename(temp_filename, filename) |
| 1024 | + os.unlink(filename) |
| 1025 | + os.unlink(temp_filename) |
1028 | 1026 | except OSError: |
1029 | | - # If we are really unlucky, another process may have written the |
1030 | | - # file in between our calls to os.unlink and os.rename. So try |
1031 | | - # again, but only once. |
1032 | | - # There are still some race conditions, but they seem difficult to |
1033 | | - # fix, and they probably occur much less frequently: |
1034 | | - # * The os.rename failure could occur more than once if more than |
1035 | | - # two processes are involved. |
1036 | | - # * In between os.unlink and os.rename, another process could try |
1037 | | - # to import the module, having seen that it already exists. |
1038 | | - # * If another process starts a COM server while we are still |
1039 | | - # generating __init__.py, that process sees that the folder |
1040 | | - # already exists and assumes that __init__.py is already there |
1041 | | - # as well. |
1042 | | - try: |
1043 | | - os.unlink(filename) |
1044 | | - except OSError: |
1045 | | - pass |
1046 | | - os.rename(temp_filename, filename) |
1047 | | - else: |
1048 | | - os.unlink(temp_filename) |
| 1027 | + pass |
1049 | 1028 |
|
1050 | 1029 | def get_temp_filename(self, filename): |
1051 | 1030 | return "%s.%d.temp" % (filename, os.getpid()) |
@@ -1327,7 +1306,8 @@ def generate_child(self, child, dir): |
1327 | 1306 | self.progress.Tick() |
1328 | 1307 | worked = True |
1329 | 1308 | finally: |
1330 | | - self.finish_writer(out_name, self.file, worked) |
| 1309 | + with gencache.ModuleMutex(self.base_mod_name.split(".")[-1]): |
| 1310 | + self.finish_writer(out_name, self.file, worked) |
1331 | 1311 | self.file = None |
1332 | 1312 | finally: |
1333 | 1313 | self.progress.Finished() |
|
0 commit comments