Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61136

Re: Embedding multiple interpreters

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'scripts': 0.03; 'third-party': 0.04; 'interpreter': 0.05; '(using': 0.07; 'modified': 0.07; 'nicely': 0.07; 'sys': 0.07; 'tries': 0.07; 'bug.': 0.09; 'closest': 0.09; 'etc).': 0.09; 'objects,': 0.09; "people's": 0.09; 'php,': 0.09; 'sake': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'thread': 0.14; '"w")': 0.16; 'accesses': 0.16; 'exhaustive.': 0.16; 'finds': 0.16; 'fly': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ie.': 0.16; 'instance:': 0.16; 'normal,': 0.16; 'rewriting': 0.16; 'rule.': 0.16; 'suggested,': 0.16; 'sys.stdout': 0.16; 'trivially': 0.16; 'true:': 0.16; 'prevent': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'split': 0.19; 'memory': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'features,': 0.24; 'module,': 0.24; '(or': 0.24; 'environment': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'compiled': 0.26; 'extension': 0.26; 'logging': 0.26; 'switch': 0.26; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'ideal': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; 'code': 0.31; 'ctypes': 0.31; 'embedding': 0.31; 'pipe': 0.31; 'file': 0.32; 'probably': 0.32; 'figure': 0.32; 'another': 0.32; 'quite': 0.32; 'running': 0.33; 'fri,': 0.33; 'maybe': 0.34; "i'd": 0.34; "can't": 0.35; 'created': 0.35; 'something': 0.35; 'plans': 0.35; "who's": 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'c++': 0.36; 'curious': 0.36; "he's": 0.36; 'interaction': 0.36; 'module.': 0.36; 'done': 0.36; 'doing': 0.36; 'possible': 0.36; 'effort': 0.37; 'example,': 0.37; 'application': 0.37; 'two': 0.37; 'list': 0.37; 'being': 0.38; 'minimum': 0.38; 'skip:o 20': 0.38; 'system,': 0.38; 'pm,': 0.38; 'little': 0.38; 'embedded': 0.39; 'though,': 0.39; 'sure': 0.39; 'unable': 0.39; 'users': 0.40; 'ensure': 0.60; 'skip:u 10': 0.60; 'easy': 0.60; 'most': 0.60; 'affect': 0.61; 'browser': 0.61; 'simply': 0.61; 'simple': 0.61; 'protection': 0.63; 'kind': 0.63; 'such': 0.63; 'more': 0.64; 'different': 0.65; 'love': 0.65; 'wall': 0.65; 'between': 0.67; 'bare': 0.84; 'cripple': 0.84; 'etc),': 0.84; 'fired': 0.84; 'interaction.': 0.84; 'interference': 0.84; 'me;': 0.84; 'plugins': 0.84; 'plugins.': 0.84; 'safe.': 0.84; 'similar)': 0.84; 'together,': 0.84; 'capture': 0.91; 'difficult,': 0.91; 'thing,': 0.91; 'to:none': 0.92; 'connection,': 0.95; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=tUKyIsag+EwI4mZsLWl6k/YVcyED86jVE99bcahMzHU=; b=g5gv2vVcd+C9gaNvICMJANrK4RCgHzFnse5huGXgZvIW6xh8SlQh5Y/hxp761Itztc zGYtBFvvt9mni/WUF7zxFg4hNl12uFJbpz8Nt1ZpplZKwaBu9pzcv2Huln7ifY6UDjea R8dVBq5O90dVUvW9kjnC/TVu1v9PP3QYhcxJjrKCio7DhoDEWGVifQ/B/42g6/zFibBi VA4Okr5Hxvu/eAlWr8cHfGvPhLP5w+7gho6Zpc4Wi/V6yVQI/2LM5CRzL9cZsBU1tU3b Xxk+baV+L2/aU51F5qV9m8ZMjDC8eUVCsfqxyfPPljiMxZRTf97lM4oMhBolMgYpPOlh ug8Q==
MIME-Version 1.0
X-Received by 10.66.66.42 with SMTP id c10mr3665542pat.98.1386331033024; Fri, 06 Dec 2013 03:57:13 -0800 (PST)
In-Reply-To <52A19A55.1000901@entropicsoftware.com>
References <mailman.3620.1386298086.18130.python-list@python.org> <bgdat9FouohU1@mid.individual.net> <52A183F8.7000807@entropicsoftware.com> <CAPTjJmq2udX39gxCLjfNWGyeTzMAsHJDHOwXHojNM7Yq18OH7g@mail.gmail.com> <52A19A55.1000901@entropicsoftware.com>
Date Fri, 6 Dec 2013 22:57:12 +1100
Subject Re: Embedding multiple interpreters
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3641.1386331036.18130.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386331036 news.xs4all.nl 2855 [2001:888:2000:d::a6]:55530
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61136

Show key headers only | View raw


On Fri, Dec 6, 2013 at 8:35 PM, Garthy
<garthy_nhtyp@entropicsoftware.com> wrote:
> I think the ideal is completely sandboxed, but it's something that I
> understand I may need to make compromises on. The bare minimum would be
> protection against inadvertent interaction. Better yet would be a setup that
> made such interaction annoyingly difficult, and the ideal would be where it
> was impossible to interfere.

In Python, "impossible to interfere" is a pipe dream. There's no way
to stop Python from fiddling around with the file system, and if
ctypes is available, with memory in the running program. The only way
to engineer that kind of protection is to prevent _the whole process_
from doing those things (using OS features, not Python features),
hence the need to split the code out into another process (which might
be chrooted, might be running as a user with no privileges, etc).

A setup that makes such interaction "annoyingly difficult" is possible
as long as your users don't think Ruby. For instance:

# script1.py
import sys
sys.stdout = open("logfile", "w")
while True: print("Blah blah")

# script2.py
import sys
sys.stdout = open("otherlogfile", "w")
while True: print("Bleh bleh")


These two scripts won't play nicely together, because each has
modified global state in a different module. So you'd have to set that
as a rule. (For this specific example, you probably want to capture
stdout/stderr to some sort of global log file anyway, and/or use the
logging module, but it makes a simple example.) Most Python scripts
aren't going to do this sort of thing, or if they do, will do very
little of it. Monkey-patching other people's code is a VERY rare thing
in Python.

> The closest analogy for understanding would be browser plugins: Scripts from
> multiple authors who for the most part aren't looking to create deliberate
> incompatibilities or interference between plugins. The isolation is basic,
> and some effort is made to make sure that one plugin can't cripple another
> trivially, but the protection is not exhaustive.

Browser plugins probably need a lot more protection - maybe it's not
exhaustive, but any time someone finds a way for one plugin to affect
another, the plugin / browser authors are going to treat it as a bug.
If I understand you, though, this is more akin to having two forms on
one page and having JS validation code for each. It's trivially easy
for one to check the other's form objects, but quite simple to avoid
too, so for the sake of encapsulation you simply stay safe.

> With the single interpreter and multiple thread approach suggested, do you
> know if this will work with threads created externally to Python, ie. if I
> can create a thread in my application as normal, and then call something
> like PyGILState_Ensure() to make sure that Python has the internals it needs
> to work with it, and then use the GIL (or similar) to ensure that accesses
> to it remain thread-safe?

Now that's something I can't help with. The only time I embedded
Python seriously was a one-Python-per-process system (arbitrary number
of processes fork()ed from one master, but each process had exactly
one Python environment and exactly one database connection, etc), and
I ended up being unable to make it secure, so I had to switch to
embedding ECMAScript (V8, specifically, as it happens... I'm morbidly
curious what my boss plans to do, now that he's fired me; he hinted at
rewriting the C++ engine in PHP, and I'd love to be a fly on the wall
as he tries to test a PHP extension for V8 and figure out whether or
not he can trust arbitrary third-party compiled code). But there'll be
someone on this list who's done threads and embedded Python.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Embedding multiple interpreters Garthy <garthy_nhtyp@entropicsoftware.com> - 2013-12-06 13:04 +1030
  Re: Embedding multiple interpreters Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-06 19:58 +1300
    Re: Embedding multiple interpreters Garthy <garthy_nhtyp@entropicsoftware.com> - 2013-12-06 18:29 +1030
      Re: Embedding multiple interpreters Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-07 11:09 +1300
        Re: Embedding multiple interpreters Garthy <garthy_nhtyp@entropicsoftware.com> - 2013-12-07 11:27 +1030
    Re: Embedding multiple interpreters Garthy <garthy_nhtyp@entropicsoftware.com> - 2013-12-06 18:51 +1030
    Re: Embedding multiple interpreters Chris Angelico <rosuav@gmail.com> - 2013-12-06 19:33 +1100
    Re: Embedding multiple interpreters Chris Angelico <rosuav@gmail.com> - 2013-12-06 20:27 +1100
    Re: Embedding multiple interpreters Garthy <garthy_nhtyp@entropicsoftware.com> - 2013-12-06 20:05 +1030
      Re: Embedding multiple interpreters Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-07 11:23 +1300
        Re: Embedding multiple interpreters Garthy <garthy_nhtyp@entropicsoftware.com> - 2013-12-07 11:01 +1030
    Re: Embedding multiple interpreters Garthy <garthy_nhtyp@entropicsoftware.com> - 2013-12-06 20:14 +1030
    Re: Embedding multiple interpreters Tim Golden <mail@timgolden.me.uk> - 2013-12-06 10:17 +0000
    Re: Embedding multiple interpreters Chris Angelico <rosuav@gmail.com> - 2013-12-06 22:57 +1100
    Re: Embedding multiple interpreters Garthy <garthy_nhtyp@entropicsoftware.com> - 2013-12-07 00:26 +1030
    Re: Embedding multiple interpreters Garthy <garthy_nhtyp@entropicsoftware.com> - 2013-12-07 00:34 +1030

csiph-web