[stdlib] NamedTemporaryFile
Part of the implementation for #2018, #2742 must be merged first.
@JoeLoser, @laszlokindrat, I'm having some issues with this: The implementation that I had in #2352 used to work in an older release, in the current release I had the following compiler error:
for _ in range(TMP_MAX): #error here: field 'self._file_handle' destroyed out of the middle of a value, preventing the overall value from being destroyed
var potential_name = final_dir / (
prefix + _get_random_name() + suffix
)
if os.path.exists(potential_name):
continue
try:
self.name = potential_name
self._file_handle = FileHandle(potential_name, mode=mode)
return
except:
continue
I had to put the if condition inside the for to be able to compile:
for _ in range(TMP_MAX):
var potential_name = final_dir / (
prefix + _get_random_name() + suffix
)
try:
if os.path.exists(potential_name) == False:
self.name = potential_name
self._file_handle = FileHandle(potential_name, mode=mode)
return
except:
continue
But now there is a runtime error which I don't really understand, I also had the same tests on my old PR and everything was ok.
@artemiogr97 I think this is unblocked now. Are you still planning to work on this?
@laszlokindrat yes, I was planning to check again this week
@laszlokindrat yes, I was planning to check again this week
Awesome, thanks, please ping me when you have something ready for review.
@JoeLoser, @laszlokindrat, I'm having some issues with this: The implementation that I had in #2352 used to work in an older release, in the current release I had the following compiler error:
for _ in range(TMP_MAX): #error here: field 'self._file_handle' destroyed out of the middle of a value, preventing the overall value from being destroyed var potential_name = final_dir / ( prefix + _get_random_name() + suffix ) if os.path.exists(potential_name): continue try: self.name = potential_name self._file_handle = FileHandle(potential_name, mode=mode) return except: continueI had to put the if condition inside the for to be able to compile:
for _ in range(TMP_MAX): var potential_name = final_dir / ( prefix + _get_random_name() + suffix ) try: if os.path.exists(potential_name) == False: self.name = potential_name self._file_handle = FileHandle(potential_name, mode=mode) return except: continueBut now there is a runtime error which I don't really understand, I also had the same tests on my old PR and everything was ok.
Turns out that adding an else after the for loop makes the compiler happier, style-wise I prefer the first implementation that I had so I changed it again
!sync
!sync
✅🟣 This contribution has been merged 🟣✅
Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours.
We use Copybara to merge external contributions, click here to learn more.
Landed in fbc2595c97b4eb1d71b0e86aea6970e484ae3052! Thank you for your contribution 🎉