Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'preferably': 0.03; 'subject:Python': 0.04; 'startup': 0.04; 'c++,': 0.07; 'terry': 0.07; 'python': 0.07; 'ctypes': 0.09; 'garbage': 0.09; 'instruction.': 0.09; "object's": 0.09; 'presume': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'string)': 0.09; 'pm,': 0.11; 'wrote:': 0.14; 'library': 0.15; '(1).': 0.16; 'allocate': 0.16; 'dictionary,': 0.16; 'did,': 0.16; 'hooks': 0.16; 'ideally,': 0.16; 'module?': 0.16; 'non-empty': 0.16; 'pointers': 0.16; 'reedy': 0.16; 'set,': 0.16; 'simulate': 0.16; 'subject:memory': 0.16; 'switching': 0.16; 'terminates': 0.16; 'tuple,': 0.16; 'libraries': 0.16; 'compiled': 0.18; 'allocated': 0.19; 'purposes,': 0.19; '(or': 0.22; 'jan': 0.22; 'header:In- Reply-To:1': 0.22; 'trying': 0.23; 'allocation': 0.23; 'module,': 0.23; 'objects': 0.24; 'thus': 0.24; 'memory': 0.24; 'possible,': 0.25; 'script': 0.26; "i'm": 0.26; 'object': 0.27; 'function': 0.27; 'shared': 0.29; 'error': 0.29; 'daniel': 0.29; 'explicitly': 0.29; 'expertise': 0.30; 'list': 0.30; 'implement': 0.30; 'invoke': 0.31; 'objects.': 0.31; 'however,': 0.31; 'determine': 0.31; 'language.': 0.31; 'to:addr:python-list': 0.32; 'usual': 0.32; 'sufficient': 0.33; 'someone': 0.33; 'module': 0.33; 'using': 0.34; 'header:X-Complaints-To:1': 0.34; 'skip:" 10': 0.34; 'decide': 0.34; 'requires': 0.35; 'there': 0.35; 'file': 0.35; 'purposes': 0.35; 'that,': 0.35; 'print': 0.35; 'question': 0.35; 'header:User-Agent:1': 0.35; 'point': 0.35; 'executable': 0.35; 'explicit': 0.35; 'function.': 0.35; 'hoping': 0.36; 'doing': 0.36; 'hello,': 0.36; 'appreciated.': 0.36; 'list,': 0.36; 'think': 0.36; 'case,': 0.36; 'either': 0.37; 'received:org': 0.38; 'science': 0.38; 'though': 0.38; 'realize': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'where': 0.39; 'header:Mime-Version:1': 0.39; 'how': 0.39; 'add': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'allows': 0.40; 'might': 0.40; 'our': 0.63; 'year': 0.63; 'here': 0.65; 'believe': 0.66; 'want,': 0.72; 'analysis': 0.74; 'guarantee': 0.75; 'canadian': 0.80; 'subject:management': 0.84; 'can:': 0.91; 'hooked': 0.91; 'crash': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Hooking into Python's memory management Date: Wed, 04 May 2011 14:58:23 -0400 References: <4DC1841B.2030601@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: <4DC1841B.2030601@gmail.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 66 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304535520 news.xs4all.nl 81473 [::ffff:82.94.164.166]:60015 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4644 On 5/4/2011 12:51 PM, Daniel Neilson wrote: > > Hello, > I'm hoping that there will be someone here with sufficient expertise to > answer a question on Python 3 for me. > > I work in the Computer Science department at a large Canadian > University. We are currently doing a feasibility analysis for switching > to using Python in our first year major-stream courses. If you did, I believe you would be following in the footsteps of MIT. > Part of our first year curriculum requires that students be exposed to > explicit dynamic memory allocation in the form of C++'s new/delete, C's > malloc/free, etc. I realize that Python is garbage collected, and that > there is never a need to explicitly allocate & deallocate objects. > However, I am trying to determine whether or not it is possible to > simulate that behaviour within Python via a module for the purposes of > instruction. The Python ctypes module allows one to invoke compiled C (or C++, I presume) functions in shared libraries (.dll on Windows, .so on *Nix). > For these purposes, I would like to know whether it is possible within > Python 3 to write a Python-only module that, essentially, hooks into the > "constructor" and "destructor" of many of the Python built-in types Python is compiled as a small startup executable (<30 KB). The builtins are all in shared libraries that you can access with ctypes. The functions in those libraries are documented in the Python/C API manual. > (specifically list, dictionary, set, tuple, and string) so that the > module can: > 1) Maintain a list of object id()'s for objects that have been created. > Ideally, this list would also contain the file & line number where the > object was created. > 2) Provide a "deallocate" function that will remove a given object's > id() from the list from (1). > 3) Print out an error message if the python script terminates with a > non-empty list from (1). Preferably with a list of objects that are > still "allocated." I presume you can do all of this easily. For point 3, a script can register an 'atexit' function. As a sidenote, using ctypes 'allows' one to crash (segfault, bluescreen) a program just like when using C ;-). It thus voids the usual guarantee that one cannot do that. > Baring a Python-only module, would this behaviour be possible to add via > a C-language module? I do not think you will *need* to do this, though you might eventually decide to make a custom library with just the functions you want, with the names you want. > A module that hooked in to all memory allocation, and inspected the type > of the object being allocated to conditionally add the object's id() to > the list would also suffice. > > In either case, if such a module is possible, any pointers you could > provide regarding how to implement such a module would be appreciated. I hope the above helps. I think Python is a great learning language. -- Terry Jan Reedy