memory overhead using from-import?

Bengt Richter bokr at oz.net
Sun Dec 21 17:44:18 EST 2003


On Sun, 21 Dec 2003 11:31:49 +0000, William Trenker <wtrenker at shaw.ca> wrote:

>William Trenker wrote:
>
>> Could someone please confirm that if I code the following line in my own module:
>> from cgi import escape
>> that the only additional memory overhead from the cgi module will be that required to store the escape function's code.
>
>After some experimenting with the /proc filesystem and a closer reading of the Python docs, I think I've answered my own question.  It looks like from-import does import the entire module.  So,
>from cgi import escape
>brings in the entire cgi module but only exposes the escape function to the importing module's namespace.
>
>The code, below, set me straight.
>
>Bill
>
>Python 2.3.2 (#1, Oct 27 2003, 10:19:56) 
>[GCC 2.95.3 20010315 (release)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
>>>> import cgi
>>>> import os,re
>>>> pid = os.getpid()
>>>> s = file('/proc/%s/status'%pid,'rb').read()
>>>> print re.search('(VmSize:.*?kB).*(VmExe:.*?kB)',s,re.DOTALL).groups()
>('VmSize:\t    5300 kB', 'VmExe:\t     712 kB')
>
>Python 2.3.2 (#1, Oct 27 2003, 10:19:56) 
>[GCC 2.95.3 20010315 (release)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
>>>> from cgi import escape
>>>> import os, re
>>>> pid = os.getpid()
>>>> s = file('/proc/%s/status'%pid,'rb').read()
>>>> print re.search('(VmSize:.*?kB).*(VmExe:.*?kB)',s,re.DOTALL).groups()
>('VmSize:\t    5300 kB', 'VmExe:\t     712 kB')
>

I wonder if anyone has tried something like (not that it would avoid temporary
memory usage (which might be returned to python free pools rather than the system),
and it depends on escape not messing with globals, and who knows
what entangling chains of reference there might be, but anyway):

 >>> execfile(r'D:\Python23\Lib\cgi.py', d)
 >>> escape = d.get('escape')
 >>> d.clear()
 >>> del d
 >>> help(escape)
 Help on function escape in module __main__:

 escape(s, quote=None)
     Replace special characters '&', '<' and '>' by SGML entities.

Looks, accessible still, so

 >>> escape
 <function escape at 0x00AAE8B0>
 >>> escape('<a href="http://www.python.org">good site</a>')
 '<a href="http://www.python.org">good site</a>'

BTW,

 >>> import sys
 >>> 'cgi' in sys.modules.keys()
 False

Alternatively, I guess some ambitious wizard could code up something so you could
write

   thing, another =  from_extract(modulename, thingname)

in place of

   from modulename import thing, another

to get the effect you want (or maybe an exception ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list