8000 gh-126606: don't write incomplete pyc files by cfbolz · Pull Request #126627 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126606: don't write incomplete pyc files #126627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve test:
- use swap_attr instead of mock
- nicer comments
- remove magic constant
  • Loading branch information
cfbolz committed Nov 13, 2024
commit bffdc028f4ee2e9fbdc24c7e48d798e53b98ad63
26 changes: 15 additions & 11 deletions Lib/test/test_importlib/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
importlib_util = util.import_importlib('importlib.util')

import importlib.util
from importlib import _bootstrap_external
import os
import pathlib
import re
import string
import sys
from test import support
from test.support import os_helper
import textwrap
import types
import unittest
Expand Down Expand Up @@ -777,30 +779,32 @@ def test_complete_multi_phase_init_module(self):

class MiscTests(unittest.TestCase):
def test_atomic_write_should_notice_incomplete_writes(self):
from importlib import _bootstrap_external
from test.support import os_helper
import _pyio
import os

oldwrite = os.write
seen_write = False

# emulate an os.write that only writes partial data
truncate_at_length = 100

# Emulate an os.write that only writes partial data.
def write(fd, data):
nonlocal seen_write
seen_write = True
return oldwrite(fd, data[:100])
return oldwrite(fd, data[:truncate_at_length])

# need to patch _io to be _pyio, so that io.FileIO is affected by the
# os.write patch
with (unittest.mock.patch('importlib._bootstrap_external._io', _pyio),
unittest.mock.patch('os.write', write)):
# Need to patch _io to be _pyio, so that io.FileIO is affected by the
# os.write patch.
with (support.swap_attr(_bootstrap_external, '_io', _pyio),
support.swap_attr(os, 'write', write)):
with self.assertRaises(OSError):
_bootstrap_external._write_atomic(os_helper.TESTFN, b'x' * 10000)
# Make sure we write something longer than the point where we
# truncate.
content = b'x' * (truncate_at_length * 2)
_bootstrap_external._write_atomic(os_helper.TESTFN, content)
assert seen_write

with self.assertRaises(OSError):
os.stat(os_helper.TESTFN) # did not get written
os.stat(support.os_helper.TESTFN) # Check that the file did not get written.


if __name__ == '__main__':
Expand Down
Loading
0