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

Skip to content

Commit fdef271

Browse files
committed
* Import/pythonrun.h, Python/{import,pythonrun}.c,
mac/macsetfiletype.c: changes by Jack to execute .pyc file passed as command line argument. On the Mac .pyc files are given a special type so they can be double-clicked
1 parent 9566408 commit fdef271

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

Python/import.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ static object *modules;
176176
/* Forward */
177177
static int init_builtin PROTO((char *));
178178

179+
/* Helper for reading .pyc files */
180+
181+
long
182+
get_pyc_magic()
183+
{
184+
return MAGIC;
185+
}
186+
179187
/* Initialization */
180188

181189
void
@@ -482,6 +490,9 @@ get_module(m, name, m_ret)
482490
/* Now write the code object to the ".pyc" file */
483491
strcpy(namebuf + len, "c");
484492
fpc = fopen(namebuf, "wb");
493+
#ifdef macintosh
494+
setfiletype(namebuf, 'PYTH', 'PYC ');
495+
#endif
485496
if (fpc == NULL) {
486497
if (verbose)
487498
fprintf(stderr,

Python/pythonrun.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3737
#include "ceval.h"
3838
#include "pythonrun.h"
3939
#include "import.h"
40+
#include "marshal.h"
4041

4142
#ifdef HAVE_SIGNAL_H
4243
#include <signal.h>
@@ -192,11 +193,24 @@ run_script(fp, filename)
192193
char *filename;
193194
{
194195
object *m, *d, *v;
196+
char *ext;
197+
195198
m = add_module("__main__");
196199
if (m == NULL)
197200
return -1;
198201
d = getmoduledict(m);
199-
v = run_file(fp, filename, file_input, d, d);
202+
ext = filename + strlen(filename) - 4;
203+
if ( strcmp(ext, ".pyc") == 0 ) {
204+
/* Try to run a pyc file. First, re-open in binary */
205+
fclose(fp);
206+
if( (fp = fopen(filename, "rb")) == NULL ) {
207+
fprintf(stderr, "python: Can't reopen .pyc file\n");
208+
return -1;
209+
}
210+
v = run_pyc_file(fp, filename, d, d);
211+
} else {
212+
v = run_file(fp, filename, file_input, d, d);
213+
}
200214
flushline();
201215
if (v == NULL) {
202216
print_error();
@@ -356,6 +370,38 @@ run_node(n, filename, globals, locals)
356370
return v;
357371
}
358372

373+
object *
374+
run_pyc_file(fp, filename, globals, locals)
375+
FILE *fp;
376+
char *filename;
377+
object *globals, *locals;
378+
{
379+
codeobject *co;
380+
object *v;
381+
long magic;
382+
long get_pyc_magic();
383+
384+
magic = rd_long(fp);
385+
if (magic != get_pyc_magic()) {
386+
err_setstr(RuntimeError,
387+
"Bad magic number in .pyc file");
388+
return NULL;
389+
}
390+
(void) rd_long(fp);
391+
v = rd_object(fp);
392+
fclose(fp);
393+
if (v == NULL || !is_codeobject(v)) {
394+
XDECREF(v);
395+
err_setstr(RuntimeError,
396+
"Bad code object in .pyc file");
397+
return NULL;
398+
}
399+
co = (codeobject *)v;
400+
v = eval_code(co, globals, locals, (object *)NULL, (object *)NULL);
401+
DECREF(co);
402+
return v;
403+
}
404+
359405
object *
360406
compile_string(str, filename, start)
361407
char *str;

0 commit comments

Comments
 (0)