How to force creation of a .pyc?
Nick Smallbone
nick.smallbone at gmail.com
Wed Feb 22 13:47:34 EST 2006
mrstephengross wrote:
> I would like to distribute a python program, but only in .pyc form (so
> that people cannot simply look at my code). Is there a way to do this?
> I've read up a little on the logic by which python creates .pyc's, and
> it sounds like python requires the main executed program to be in .py
> format. Any ideas?
>
If your main program is in main.py, you could perhaps launch it with
python -c "import main", or make another file which just does import
main. Then you could keep just the .pycs/.pyos.
But it's possible for a determined user to recover a lot of information
about the source code. For example, if I write
def foo(a):
for i in range(a):
print i
then
>>> dis.dis(foo)
2 0 SETUP_LOOP 25 (to 28)
3 LOAD_GLOBAL 0 (range)
6 LOAD_FAST 0 (a)
9 CALL_FUNCTION 1
12 GET_ITER
>> 13 FOR_ITER 11 (to 27)
16 STORE_FAST 1 (i)
3 19 LOAD_FAST 1 (i)
22 PRINT_ITEM
23 PRINT_NEWLINE
24 JUMP_ABSOLUTE 13
>> 27 POP_BLOCK
>> 28 LOAD_CONST 0 (None)
31 RETURN_VALUE
where 0..12 makes an iterator for range(a), 16..19 updates i and 22..23
prints it out. It would be possible to turn it into equivalent source
code. Decompyle (from
http://ftp.debian.org/debian/pool/main/d/decompyle/decompyle_2.3.2.orig.tar.gz)
can even do that.
More information about the Python-list
mailing list