modular icon indicating copy to clipboard operation
modular copied to clipboard

[stdlib] NamedTemporaryFile

Open artemiogr97 opened this issue 1 year ago • 1 comments

Part of the implementation for #2018, #2742 must be merged first.

artemiogr97 avatar May 20 '24 15:05 artemiogr97

@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 avatar May 20 '24 15:05 artemiogr97

@artemiogr97 I think this is unblocked now. Are you still planning to work on this?

laszlokindrat avatar Jun 04 '24 14:06 laszlokindrat

@laszlokindrat yes, I was planning to check again this week

artemiogr97 avatar Jun 04 '24 14:06 artemiogr97

@laszlokindrat yes, I was planning to check again this week

Awesome, thanks, please ping me when you have something ready for review.

laszlokindrat avatar Jun 04 '24 15:06 laszlokindrat

@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.

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

artemiogr97 avatar Jun 04 '24 21:06 artemiogr97

!sync

JoeLoser avatar Jun 06 '24 22:06 JoeLoser

!sync

JoeLoser avatar Jun 07 '24 16:06 JoeLoser

✅🟣 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.

modularbot avatar Jun 10 '24 17:06 modularbot

Landed in fbc2595c97b4eb1d71b0e86aea6970e484ae3052! Thank you for your contribution 🎉

modularbot avatar Jun 11 '24 05:06 modularbot