Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'arguments': 0.05; 'parser': 0.05; 'string,': 0.05; 'args': 0.07; 'raises': 0.07; 'type,': 0.07; 'python': 0.07; ':-)': 0.07; '(there': 0.09; 'arg,': 0.09; 'args,': 0.09; 'arguments,': 0.09; 'exception.': 0.09; 'null);': 0.09; 'null,': 0.09; 'of)': 0.09; 'question:': 0.09; 'this).': 0.09; 'exception': 0.12; 'binary': 0.14; 'defined': 0.15; '"can\'t': 0.16; '"error",': 0.16; '(needed': 0.16; '-1:': 0.16; '>because': 0.16; '>how': 0.16; 'constructs': 0.16; 'parsed': 0.16; 'pyobject': 0.16; 'simple:': 0.16; 'subject:() ': 0.16; 'tuple,': 0.16; 'wanted.': 0.16; 'argument': 0.16; 'static': 0.16; 'input': 0.18; 'have,': 0.20; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'variable': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'e.g.': 0.22; 'right,': 0.22; 'parameters.': 0.23; 'objects': 0.24; 'calling': 0.25; 'used,': 0.25; 'value.': 0.25; 'says': 0.25; '(see': 0.26; 'correct': 0.26; '(in': 0.27; 'object': 0.27; 'function': 0.27; 'thanks': 0.29; 'skip:p 30': 0.29; 'string': 0.29; 'error': 0.29; 'skip:* 20': 0.29; 'probably': 0.30; 'cc:addr:python.org': 0.31; 'points': 0.31; 'break;': 0.31; 'clear,': 0.31; 'fails,': 0.31; 'does': 0.31; 'match': 0.31; 'url:docs': 0.33; "isn't": 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'right?': 0.35; 'hello,': 0.36; 'think': 0.36; 'data': 0.37; 'two': 0.37; 'case': 0.37; 'url:python': 0.37; 'useful': 0.37; 'but': 0.38; 'url:org': 0.38; 'so,': 0.38; 'set': 0.39; 'where': 0.39; 'how': 0.39; 'takes': 0.40; 'empty': 0.40; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'might': 0.40; 'give': 0.61; 'order': 0.61; 'here': 0.65; 'want,': 0.72; '_it_': 0.84; 'bang': 0.84; 'knows,': 0.84; 'received:hu': 0.84; 'notices': 0.93 X-Spam-Score: -1.31 X-Spam-Level: X-Spam-Status: No, score=-1.31 tagged_above=-20 required=4 tests=[AWL=0.059, BAYES_00=-2.599, DNS_FROM_OPENWHOIS=1.13, RDNS_DYNAMIC=0.1] Date: Tue, 26 Apr 2011 20:44:26 +0200 From: =?utf-8?Q?Heged=C3=BCs?= Ervin To: Thomas Rachel Subject: Re: Py_INCREF() incomprehension References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) Cc: python-list@python.org 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: 89 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303843478 news.xs4all.nl 34849 [::ffff:82.94.164.166]:49868 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4066 Hello, > >But, when I don't read input arguments (there isn't > >PyArg_ParseTuple), there isn't exception. > > > >How Python handle the number of arguments? > > From what you tell it: with PyArg_ParseTuple(). (see > http://docs.python.org/c-api/arg.html for this). > > You give a format string (in your case: "ss", again: better use > "s#s#" if possible) which is parsed in order to get the (needed > number of) parameters. > > If you call with () or only one arg, args points to an empty tuple, > but the parser wants two arguments -> bang. > > If you call with more than two args, the function notices it too: > the arguments would just be dropped, which is probably not what is > wanted. > > If you call with two args, but of wrong type, they don't match to > "s" (=string) -> bang again. > > Only with calling with the correct number AND type of args, the > function says "ok". > > Why is "s#" better than "s"? Simple: the former gives the string > length as well. "s" means a 0-terminated string, which might not be > what you want, especially with binary data (what you have, I > suppose). > > If you give e.g. "ab\0cd" where "s" is used, you get an exception as > well, as this string cannot be parsed cmpletely. So better use "s#" > and get the length as well. so, if em I right, if PyArg_ParseTuple() fails, _it_ raises TypeError exception... (?) I think it's clear, thanks :) > >I just ask this, > >because I don't set errstring with PyErr_SetString, but I get > >TypeError - how does Python knows, this error raised? > > There is magic inside... :-) waov :) and (maybe) final question: :) I defined many exceptions: static PyObject *cibcrypt_error_nokey; static PyObject *cibcrypt_error_nofile; static PyObject *cibcrypt_error_badpad; ... void handle_err(int errcode) { switch(errcode) { case -1: PyErr_SetString(cibcrypt_error_nokey, "Can't find key."); break; ... } ... cibcrypt_error_nokey = PyErr_NewException("cibcrypt.error_nokey", NULL, NULL); ... PyModule_AddObject(o, "error", cibcrypt_error_nokey); I am right, here also no need any Py_INCREF()/Py_DECREF() action, based on this doc: http://docs.python.org/c-api/arg.html "Another useful function is PyErr_SetFromErrno(), which only takes an exception argument and constructs the associated value by inspection of the global variable errno. The most general function is PyErr_SetObject(), which takes two object arguments, the exception and its associated value. You don’t need to Py_INCREF() the objects passed to any of these function" so, this part of code is right? thanks again: a.