◐ Shell
clean mode source ↗

Issue #13645: pyc files now contain the size of the corresponding source · python/cpython@5136ac0

@@ -70,11 +70,6 @@ def test_module_reuse(self):

7070

module_dict_id = id(module.__dict__)

7171

with open(mapping['_temp'], 'w') as file:

7272

file.write("testing_var = 42\n")

73-

# For filesystems where the mtime is only to a second granularity,

74-

# everything that has happened above can be too fast;

75-

# force an mtime on the source that is guaranteed to be different

76-

# than the original mtime.

77-

loader.path_mtime = self.fake_mtime(loader.path_mtime)

7873

module = loader.load_module('_temp')

7974

self.assertTrue('testing_var' in module.__dict__,

8075

"'testing_var' not in "

@@ -190,10 +185,17 @@ def _test_partial_timestamp(self, test, *, del_source=False):

190185

del_source=del_source)

191186

test('_temp', mapping, bc_path)

192187188+

def _test_partial_size(self, test, *, del_source=False):

189+

with source_util.create_modules('_temp') as mapping:

190+

bc_path = self.manipulate_bytecode('_temp', mapping,

191+

lambda bc: bc[:11],

192+

del_source=del_source)

193+

test('_temp', mapping, bc_path)

194+193195

def _test_no_marshal(self, *, del_source=False):

194196

with source_util.create_modules('_temp') as mapping:

195197

bc_path = self.manipulate_bytecode('_temp', mapping,

196-

lambda bc: bc[:8],

198+

lambda bc: bc[:12],

197199

del_source=del_source)

198200

file_path = mapping['_temp'] if not del_source else bc_path

199201

with self.assertRaises(EOFError):

@@ -202,7 +204,7 @@ def _test_no_marshal(self, *, del_source=False):

202204

def _test_non_code_marshal(self, *, del_source=False):

203205

with source_util.create_modules('_temp') as mapping:

204206

bytecode_path = self.manipulate_bytecode('_temp', mapping,

205-

lambda bc: bc[:8] + marshal.dumps(b'abcd'),

207+

lambda bc: bc[:12] + marshal.dumps(b'abcd'),

206208

del_source=del_source)

207209

file_path = mapping['_temp'] if not del_source else bytecode_path

208210

with self.assertRaises(ImportError):

@@ -211,7 +213,7 @@ def _test_non_code_marshal(self, *, del_source=False):

211213

def _test_bad_marshal(self, *, del_source=False):

212214

with source_util.create_modules('_temp') as mapping:

213215

bytecode_path = self.manipulate_bytecode('_temp', mapping,

214-

lambda bc: bc[:8] + b'<test>',

216+

lambda bc: bc[:12] + b'<test>',

215217

del_source=del_source)

216218

file_path = mapping['_temp'] if not del_source else bytecode_path

217219

with self.assertRaises(EOFError):

@@ -235,15 +237,15 @@ def test_empty_file(self):

235237

def test(name, mapping, bytecode_path):

236238

self.import_(mapping[name], name)

237239

with open(bytecode_path, 'rb') as file:

238-

self.assertGreater(len(file.read()), 8)

240+

self.assertGreater(len(file.read()), 12)

239241240242

self._test_empty_file(test)

241243242244

def test_partial_magic(self):

243245

def test(name, mapping, bytecode_path):

244246

self.import_(mapping[name], name)

245247

with open(bytecode_path, 'rb') as file:

246-

self.assertGreater(len(file.read()), 8)

248+

self.assertGreater(len(file.read()), 12)

247249248250

self._test_partial_magic(test)

249251

@@ -254,7 +256,7 @@ def test_magic_only(self):

254256

def test(name, mapping, bytecode_path):

255257

self.import_(mapping[name], name)

256258

with open(bytecode_path, 'rb') as file:

257-

self.assertGreater(len(file.read()), 8)

259+

self.assertGreater(len(file.read()), 12)

258260259261

self._test_magic_only(test)

260262

@@ -276,10 +278,21 @@ def test_partial_timestamp(self):

276278

def test(name, mapping, bc_path):

277279

self.import_(mapping[name], name)

278280

with open(bc_path, 'rb') as file:

279-

self.assertGreater(len(file.read()), 8)

281+

self.assertGreater(len(file.read()), 12)

280282281283

self._test_partial_timestamp(test)

282284285+

@source_util.writes_bytecode_files

286+

def test_partial_size(self):

287+

# When the size is partial, regenerate the .pyc, else

288+

# raise EOFError.

289+

def test(name, mapping, bc_path):

290+

self.import_(mapping[name], name)

291+

with open(bc_path, 'rb') as file:

292+

self.assertGreater(len(file.read()), 12)

293+294+

self._test_partial_size(test)

295+283296

@source_util.writes_bytecode_files

284297

def test_no_marshal(self):

285298

# When there is only the magic number and timestamp, raise EOFError.

@@ -375,6 +388,13 @@ def test(name, mapping, bytecode_path):

375388376389

self._test_partial_timestamp(test, del_source=True)

377390391+

def test_partial_size(self):

392+

def test(name, mapping, bytecode_path):

393+

with self.assertRaises(EOFError):

394+

self.import_(bytecode_path, name)

395+396+

self._test_partial_size(test, del_source=True)

397+378398

def test_no_marshal(self):

379399

self._test_no_marshal(del_source=True)

380400