Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b93f521

Browse files
committed
Support for freezing packages (Just).
1 parent 201f46d commit b93f521

4 files changed

Lines changed: 58 additions & 12 deletions

File tree

Mac/Lib/py_resource.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ def open(dst):
2828
Res.UseResFile(output)
2929
return output
3030

31-
def writemodule(name, id, data, type='PYC ', preload=0):
31+
def writemodule(name, id, data, type='PYC ', preload=0, ispackage=0):
3232
"""Write pyc code to a PYC resource with given name and id."""
3333
# XXXX Check that it doesn't exist
34+
35+
# Normally, byte 4-7 are the time stamp, but that is not used
36+
# for 'PYC ' resources. We abuse byte 4 as a flag to indicate
37+
# that it is a package rather than an ordinary module.
38+
# See also macimport.c. (jvr)
39+
if ispackage:
40+
data = data[:4] + '\377\0\0\0' + data[8:] # flag resource as package
41+
else:
42+
data = data[:4] + '\0\0\0\0' + data[8:] # clear mod date field, used as package flag
3443
res = Res.Resource(data)
3544
res.AddResource(type, id, name)
3645
if preload:
@@ -40,22 +49,23 @@ def writemodule(name, id, data, type='PYC ', preload=0):
4049
res.WriteResource()
4150
res.ReleaseResource()
4251

43-
def frompycfile(file, name=None, preload=0):
52+
def frompycfile(file, name=None, preload=0, ispackage=0):
4453
"""Copy one pyc file to the open resource file"""
4554
if name == None:
4655
d, name = os.path.split(file)
4756
name = name[:-4]
4857
id = findfreeid()
49-
writemodule(name, id, __builtin__.open(file, 'rb').read(), preload=preload)
58+
data = __builtin__.open(file, 'rb').read()
59+
writemodule(name, id, data, preload=preload, ispackage=ispackage)
5060
return id, name
5161

52-
def frompyfile(file, name=None, preload=0):
62+
def frompyfile(file, name=None, preload=0, ispackage=0):
5363
"""Compile python source file to pyc file and add to resource file"""
5464
import py_compile
5565

5666
py_compile.compile(file)
5767
file = file +'c'
58-
return frompycfile(file, name, preload=preload)
68+
return frompycfile(file, name, preload=preload, ispackage=ispackage)
5969

6070
# XXXX Note this is incorrect, it only handles one type and one file....
6171

Mac/Python/macimport.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,45 @@ char *filename;
332332
co = NULL;
333333
} else {
334334
co = PyMarshal_ReadObjectFromString((*h)+8, size-8);
335+
/*
336+
** Normally, byte 4-7 are the time stamp, but that is not used
337+
** for 'PYC ' resources. We abuse byte 4 as a flag to indicate
338+
** that it is a package rather than an ordinary module.
339+
** See also py_resource.py. (jvr)
340+
*/
341+
if ((*h)[4] & 0xff) {
342+
/* it's a package */
343+
/* Set __path__ to the package name */
344+
PyObject *d, *s;
345+
int err;
346+
347+
m = PyImport_AddModule(module);
348+
if (m == NULL) {
349+
co = NULL;
350+
goto packageerror;
351+
}
352+
d = PyModule_GetDict(m);
353+
s = PyString_InternFromString(module);
354+
if (s == NULL) {
355+
co = NULL;
356+
goto packageerror;
357+
}
358+
err = PyDict_SetItemString(d, "__path__", s);
359+
Py_DECREF(s);
360+
if (err != 0) {
361+
co = NULL;
362+
goto packageerror;
363+
}
364+
}
335365
}
336366
}
367+
packageerror:
337368
HUnlock(h);
338369
if ( filerh != -1 )
339370
CloseResFile(filerh);
340371
UseResFile(oldrh);
341372
if ( co ) {
342-
m = PyImport_ExecCodeModule(module, co);
373+
m = PyImport_ExecCodeModuleEx(module, co, "<pyc resource>");
343374
Py_DECREF(co);
344375
} else {
345376
m = NULL;

Mac/Tools/macfreeze/macgen_bin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ def getfragname(path, dynamicfiles):
128128

129129

130130
def addpythonmodules(module_dict):
131+
# XXX should really use macgen_rsrc.generate(), this does the same, but skips __main__
131132
items = module_dict.items()
132133
items.sort()
133134
for name, module in items:
134-
if module.gettype() != 'module' or name == "__main__":
135+
mtype = module.gettype()
136+
if mtype not in ['module', 'package'] or name == "__main__":
135137
continue
136138
location = module.__file__
137139

@@ -143,7 +145,8 @@ def addpythonmodules(module_dict):
143145
continue
144146

145147
print 'Adding module ³%s²' % name
146-
id, name = py_resource.frompyfile(location, name, preload=0)
148+
id, name = py_resource.frompyfile(location, name, preload=0,
149+
ispackage=mtype=='package')
147150

148151
def Pstring(str):
149152
if len(str) > 255:

Mac/Tools/macfreeze/macgen_rsrc.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
def generate(output, module_dict, debug=0, preload=1):
88
fsid = py_resource.create(output)
9-
9+
1010
for name, module in module_dict.items():
11-
if module.gettype() != 'module':
11+
mtype = module.gettype()
12+
if mtype not in ['module', 'package']:
1213
continue
1314
location = module.__file__
1415

@@ -19,10 +20,11 @@ def generate(output, module_dict, debug=0, preload=1):
1920
print '*** skipping', location
2021
continue
2122

22-
id, name = py_resource.frompyfile(location, name, preload=preload)
23+
id, name = py_resource.frompyfile(location, name, preload=preload,
24+
ispackage=mtype=='package')
2325
if debug > 0:
2426
print 'PYC resource %5d\t%s\t%s'%(id, name, location)
25-
27+
2628
Res.CloseResFile(fsid)
2729

2830
def warnings(module_dict):

0 commit comments

Comments
 (0)