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


Groups > comp.lang.python > #4030

Re: Py_INCREF() incomprehension

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!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'int': 0.05; 'char': 0.07; 'cpython': 0.07; 'fine,': 0.07; 'raises': 0.07; 'python': 0.07; 'counting': 0.09; 'null;': 0.09; 'question:': 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; 'this:': 0.11; 'users,': 0.14; '*args)': 0.16; '*self,': 0.16; 'above?': 0.16; 'c-api': 0.16; 'exception?': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 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; 'code.': 0.18; 'allocated': 0.19; 'appropriate': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'module,': 0.23; 'keeps': 0.24; 'memory': 0.24; 'library.': 0.25; 'assume': 0.25; "i'm": 0.26; 'instead': 0.26; 'function': 0.27; 'problem': 0.29; 'stefan': 0.29; 'skip:( 20': 0.31; 'format.': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; 'sorry': 0.33; 'module': 0.33; 'reference': 0.34; 'uses': 0.34; 'using': 0.34; 'header:X -Complaints-To:1': 0.34; 'skip:" 10': 0.34; 'header:User-Agent:1': 0.35; 'too': 0.36; 'plain': 0.36; 'getting': 0.36; 'think': 0.36; 'case,': 0.36; 'else': 0.37; 'two': 0.37; 'skip:o 20': 0.37; 'should': 0.37; 'but': 0.38; 'received:org': 0.38; 'provider': 0.39; 'to:addr:python.org': 0.39; 'where': 0.39; 'received:de': 0.39; 'header:Mime-Version:1': 0.39; 'works': 0.40; 'header:Received:5': 0.40; 'messages': 0.40; 'skip:h 20': 0.60; 'and,': 0.63; 'here': 0.65; 'details,': 0.68; 'here.': 0.68; 'received:93': 0.80; 'distracted': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: Py_INCREF() incomprehension
Date Tue, 26 Apr 2011 12:28:17 +0200
References <BANLkTin-87OpQyRpbhbhc1i+wBRMr_+rPg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host ppp-93-104-30-18.dynamic.mnet-online.de
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Lightning/1.0b2 Thunderbird/3.1.8
In-Reply-To <BANLkTin-87OpQyRpbhbhc1i+wBRMr_+rPg@mail.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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.831.1303813716.9059.python-list@python.org> (permalink)
Lines 80
NNTP-Posting-Host 82.94.164.166
X-Trace 1303813716 news.xs4all.nl 81485 [::ffff:82.94.164.166]:33721
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:4030

Show key headers only | View raw


Ervin Hegedüs, 26.04.2011 11:48:
> Hello Python users,
>
> I'm working on a Python module in C - that's a cryptographic module,
> which uses a 3rd-party lib from a provider (a bank).
> This module will encrypt and decrypt the messages for the provider web service.
>
> Here is a part of source:
>
> 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.


>          return NULL;
>      }
>
>      OutLen = strlen(url)*4;
>      outdata=calloc(OutLen, sizeof(char));
>
>      if (!outdata) {
>          handle_err(UER_NOMEM);

I assume this raises MemoryError?


>          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.

And, again, use the "s#" format.


>      } else {
>          handle_err(cRes);
>          return NULL;
>      }

I assume this raises an appropriate exception?


>      return Py_None;

This is unreachable code.


> 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.

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.

Stefan

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


Thread

Re: Py_INCREF() incomprehension Stefan Behnel <stefan_ml@behnel.de> - 2011-04-26 12:28 +0200

csiph-web