Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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; 'skip:p 40': 0.04; 'void': 0.04; 'int': 0.05; 'char': 0.07; 'cpython': 0.07; 'fine,': 0.07; 'raises': 0.07; 'python': 0.07; 'be:': 0.09; 'counting': 0.09; 'null);': 0.09; 'null,': 0.09; 'null;': 0.09; 'oh,': 0.09; 'question:': 0.09; 'this:': 0.11; 'exception': 0.12; 'defined': 0.15; '"bad': 0.16; '"error",': 0.16; '"invalid': 0.16; '(eg:': 0.16; '*args)': 0.16; '*self,': 0.16; 'above?': 0.16; 'c-api': 0.16; 'cpython,': 0.16; 'exception?': 0.16; 'objects?': 0.16; 'outdata': 0.16; 'py_none;': 0.16; 'pyobject': 0.16; 'pyobject*': 0.16; 'subject:() ': 0.16; 'skip:" 40': 0.16; 'static': 0.16; 'thanks,': 0.17; 'code.': 0.18; 'allocated': 0.19; 'appropriate': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'file,': 0.22; '"not': 0.23; 'keeps': 0.24; 'memory': 0.24; 'wrote': 0.25; 'library.': 0.25; 'assume': 0.25; "i'll": 0.26; 'instead': 0.26; 'function': 0.27; 'thanks.': 0.28; 'thanks': 0.29; 'skip:p 30': 0.29; 'error': 0.29; 'problem': 0.29; 'skip:* 20': 0.29; 'skip:( 20': 0.31; 'break;': 0.31; 'format.': 0.31; 'to:addr:python-list': 0.32; 'another': 0.32; "i've": 0.33; 'passion': 0.33; 'sorry': 0.33; 'created': 0.33; 'several': 0.33; 'reference': 0.34; 'using': 0.34; 'skip:" 10': 0.34; 'there': 0.35; 'question': 0.35; 'header:User-Agent:1': 0.35; 'function.': 0.35; 'too': 0.36; 'hello,': 0.36; 'plain': 0.36; 'getting': 0.36; 'think': 0.36; 'case,': 0.36; 'enough': 0.37; 'else': 0.37; 'two': 0.37; 'skip:o 20': 0.37; 'case': 0.37; 'should': 0.37; 'but': 0.38; 'ok,': 0.39; 'to:addr:python.org': 0.39; 'where': 0.39; 'how': 0.39; 'works': 0.40; 'solution': 0.40; 'header:Received:5': 0.40; 'received:86': 0.60; 'skip:h 20': 0.60; 'and,': 0.63; 'skip:> 20': 0.68; 'details,': 0.68; 'here.': 0.68; 'reply,': 0.73; 'distracted': 0.84; 'leak': 0.84; 'received:hu': 0.84 X-Spam-Score: -1.344 X-Spam-Level: X-Spam-Status: No, score=-1.344 tagged_above=-20 required=4 tests=[AWL=0.025, BAYES_00=-2.599, DNS_FROM_OPENWHOIS=1.13, RDNS_NONE=0.1] Date: Tue, 26 Apr 2011 13:01:13 +0200 From: =?utf-8?Q?Heged=C3=BCs?= Ervin To: python-list@python.org Subject: Re: Py_INCREF() incomprehension References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) 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: 141 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303815622 news.xs4all.nl 41117 [::ffff:82.94.164.166]:50848 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4031 Hello, thanks for the reply, > >static PyObject* > >mycrypt_encrypt(PyObject *self, PyObject *args) > >{ > > int cRes = 0; > > int OutLen = 0; > > > > char * url; > > char * path; > > > > if (!PyArg_ParseTuple(args, "ss",&url,&path)) { > > Use the "s#" format instead to get the length as well. oh, thanks, I'll check it out, > > return NULL; > > } > > > > OutLen = strlen(url)*4; > > outdata=calloc(OutLen, sizeof(char)); > > > > if (!outdata) { > > handle_err(UER_NOMEM); > > I assume this raises MemoryError? yes, another question should be: do I need to use Py_INCREF()/Py_DECREF() at exception objects? There are several error types, which has defined in 3rd-party header file, based on these I've created my exceptions: ... static PyObject *cibcrypt_error_badparm; static PyObject *cibcrypt_error_nomem; static PyObject *cibcrypt_error_badsize; ... void handle_err(int errcode) { switch(errcode) { ... case -4: PyErr_SetString(cibcrypt_error_badparm, "Bad parameter"); break; case -5: PyErr_SetString(cibcrypt_error_nomem, "Not enough memory"); break; case -6: PyErr_SetString(cibcrypt_error_badsize, "Invalid buffer size"); break; ... } PyMODINIT_FUNC initcibcrypt(void) { PyObject *o; o = Py_InitModule3("mycrypt", mycrypt_methods, mycrypt_doc); ... cibcrypt_error_badparm = PyErr_NewException("cibcrypt.error_badparm", NULL, NULL); cibcrypt_error_nomem = PyErr_NewException("cibcrypt.error_nomem", NULL, NULL); cibcrypt_error_badsize = PyErr_NewException("cibcrypt.error_badsize", NULL, NULL); ... PyModule_AddObject(o, "error", cibcrypt_error_badparm); PyModule_AddObject(o, "error", cibcrypt_error_nomem); PyModule_AddObject(o, "error", cibcrypt_error_badsize); ... } is that correct? > > return NULL; > > } > > cRes = ekiEncodeUrl (url, strlen(url)+1, outdata,&OutLen, 1, path); > > > > if (cRes == 0) { > > return Py_BuildValue("s", outdata); > > You are leaking the memory allocated for outdata here. ok, and how can I handle that leak? When should I use the Py_DECREF, or any other function (eg: free()?))? > And, again, use the "s#" format. > > > > } else { > > handle_err(cRes); > > return NULL; > > } > > I assume this raises an appropriate exception? yes, see the handle_error() function. > > > return Py_None; > > This is unreachable code. yes, that's just an old typo, I think I purged these... thanks. > >where ekiEncodeUrl is in a 3rd-party library. > >I should call this function from Python like this: > > > >import mycrypt > > > >message = "PID=IEB0001&MSGT=10&TRID=000000012345678" > >crypted = mycrypt(mymessage, "/path/to/key"); > > > >Everything works fine, but sorry for the recurrent question: where > >should I use the Py_INCREF()/Py_DECREF() in code above? > > These two functions are not your problem here. which two functions did you mean? If you mean encrypt/decrypt (above), why did you wrote there is a memleak? > In any case, I recommend using Cython instead of plain C. It keeps > you from getting distracted too much by reference counting and other > CPython C-API details, so that you can think more about the > functionality you want to implement. ok, I'll try to see CPython, but - just for my passion :) - what is the solution to handle the leak above? Thanks again: a.