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


Groups > comp.lang.python > #87135

Question about importlib

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!81.171.88.16.MISMATCH!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'subsequent': 0.05; 'subject:Question': 0.07; 'string': 0.09; 'arguments': 0.09; 'attributes': 0.09; 'caller': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tests,': 0.09; 'question.': 0.14; 'before.': 0.16; 'element,': 0.16; 'imports': 0.16; 'insights': 0.16; 'modules,': 0.16; 'posting,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sec': 0.16; 'module': 0.19; 'passing': 0.19; 'import': 0.22; 'fairly': 0.24; 'question': 0.24; 'second': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'xml': 0.29; 'posting': 0.31; 'extract': 0.31; 'once,': 0.31; 'overhead': 0.31; 'anyone': 0.31; 'minimal': 0.33; 'could': 0.34; 'but': 0.35; 'there': 0.35; 'machine.': 0.36; 'thanks': 0.36; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'called': 0.40; 'hope': 0.61; 'happen': 0.63; 'more': 0.64; 'situation': 0.65; 'within': 0.65; 'worth': 0.66; 'frank': 0.68; 'containing': 0.69; 'actually,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From "Frank Millman" <frank@chagford.com>
Subject Question about importlib
Date Sun, 8 Mar 2015 09:30:25 +0200
X-Gmane-NNTP-Posting-Host 197.86.205.221
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.3790.4657
X-RFC2646 Format=Flowed; Original
X-MimeOLE Produced By Microsoft MimeOLE V6.00.3790.4913
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
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.162.1425799936.21433.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1425799936 news.xs4all.nl 2860 [2001:888:2000:d::a6]:50367
X-Complaints-To abuse@xs4all.nl
X-Received-Body-CRC 2821642462
X-Received-Bytes 4241
Xref csiph.com comp.lang.python:87135

Show key headers only | View raw


Hi all

It is well known that if you import a module more than once, the overhead 
for the subsequent imports is negligible.

Does anyone happen to know if the same is true of the following?

        module_name, func_name = func_name.rsplit('.', 1)
        module = importlib.import_module(module_name)
        getattr(module, func_name)(caller, xml_elem)

I have a situation where I want to call a function dynamically, by passing a 
string containing a path to the function. The same function could be called 
multiple times. I would hope that the overhead for the second and subsequent 
calls is also fairly minimal.

Actually, as I write this, I realise that there is a more important question 
that had not occurred to me before. Is this a potential security risk? My 
intention is that the caller would only call functions within my own 
modules, but this could be used to call any arbitrary function.

You cannot pass arbitrary arguments to the function. The arguments are the 
'caller', which can only be an object within my application, and an xml 
element, from which the function can extract attributes using 
xml_elem.get(attr_name).

Before posting this I ran some timing tests, and the overhead does indeed 
seem to be minimal - 0.0001 sec on a slow desktop machine.

I think it is still worth posting, for any insights into either question.

Thanks

Frank Millman


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


Thread

Question about importlib "Frank Millman" <frank@chagford.com> - 2015-03-08 09:30 +0200

csiph-web