Obscuring Python source from end users

Chris Angelico rosuav at gmail.com
Mon Sep 29 10:55:41 CEST 2014

On Mon, Sep 29, 2014 at 6:41 PM,  <norman.ives at gmail.com> wrote:
> Crunching the source is an interesting idea that could achieve that end, but it seems fraught with problems like maintaining consistency between renaming objects in a module and renaming where imports happen.

Here's a technique that you could use. Pick two prefixes, one for long
names and one for short names, and maintain a translation table. So,
for instance, you could have two source files like this:

# file1.py
# Do something with the length and the count
def xx_some_func(length, cnt):
    # TODO: Implement me.

# file2.py
import file1

def xx_foo(lst, len, count):
    # Do something for every element in the list
    # where the something is blah blah blah
    for element in lst:
        file1.xx_some_func(len, count)

And then your crunched files might be:

# file1.py:
def x_a(a,b):

# file2.py
import file1
def x_b(a,b,c):
 for d in a:

The translation is 100% mechanical: any place you find "xx_some_func",
you replace it with "x_a". If you find a string that begins "xx_" and
isn't in your translation table, you construct a new name (don't
forget that you can use x_A as well as x_a) and add it to the table.

It ought to be possible to do an AST reconstitution for at least part
of this. I can hunt down some of my PEP 463 test code to help out with
that. It should be possible to figure out what names are local, and
then just use those.

If this is of interest, I'll see what I can knock together.


More information about the Python-list mailing list