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


Groups > comp.lang.python > #4055

Re: Py_INCREF() incomprehension

Date 2011-04-26 19:28 +0200
From Hegedüs Ervin <airween@gmail.com>
Subject Re: Py_INCREF() incomprehension
References <mailman.830.1303811297.9059.python-list@python.org> <ip6dc5$m7d$1@r03.glglgl.eu> <mailman.839.1303826380.9059.python-list@python.org> <ip6qmr$50j$1@r03.glglgl.eu>
Newsgroups comp.lang.python
Message-ID <mailman.851.1303838890.9059.python-list@python.org> (permalink)

Show all headers | View raw


Dear Thomas,

thank you again,

> The ownership rules say that the input parameter belongs to the
> caller who holds it at least until we return. (We just "borrow" it.)
> So no action needed.

ok, its' clear, I understand,
 
> >>* Py_BuildValue()
> 
> This function "transfers ownership", as it is none of
> (PyTuple_GetItem(), PyList_GetItem(), PyDict_GetItem(),
> PyDict_GetItemString()).
> 
> So the value it returns belongs to us, for now.
> 
> We do transfer ownership to our caller (implicitly), so no action is
> required as well here.

also,
 
> >so, it means when I implicit allocate a new object (whit
> >Py_BuildValue()), Python's GC will free that pointer when it
> >doesn't require anymore?
> 
> In a way, yes. But you have to obey ownership: whom belongs the
> current reference? If it is not ours, and we need it, we do
> Py_(X)INCREF(). If we got it, but don't need it, we do
> Py_(X)DECREF().

right, it's clear again,
 
> >>BTW: Is there any reason for using calloc()? malloc() would probably
> >>be faster...
> >
> >may be, I didn't measure it ever... but calloc() gives clear
> >space... :)
> 
> Ok. (But as sizeof(char) is, by C standard definition, always 1, you
> can write it shorter.)

oh' well, thanks, I just wrote "from a wrist" :), I just realize
it now... :)

Another question: here is an another part ot my code:

static PyObject*
mycrypt_decrypt(PyObject *self, PyObject *args)
{
    if (!PyArg_ParseTuple(args, "ss", &data, &path)) {
        return NULL;
    }

...

}

When I call this function from Python without argument or more
than it expects, I get an exception, eg.:
TypeError: function takes exactly 2 arguments (0 given)

But, when I don't read input arguments (there isn't
PyArg_ParseTuple), there isn't exception.

How Python handle the number of arguments? I just ask this,
because I don't set errstring with PyErr_SetString, but I get
TypeError - how does Python knows, this error raised?

Hope you understand my question... :)

thanks for all:


a.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Py_INCREF() incomprehension Ervin Hegedüs <airween@gmail.com> - 2011-04-26 11:48 +0200
  Re: Py_INCREF() incomprehension Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-26 14:23 +0200
  Re: Py_INCREF() incomprehension Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-26 14:21 +0200
    Re: Py_INCREF() incomprehension Hegedüs Ervin <airween@gmail.com> - 2011-04-26 16:03 +0200
      Re: Py_INCREF() incomprehension Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-26 18:08 +0200
        Re: Py_INCREF() incomprehension Hegedüs Ervin <airween@gmail.com> - 2011-04-26 19:28 +0200
          Re: Py_INCREF() incomprehension Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-26 19:45 +0200
            Re: Py_INCREF() incomprehension Hegedüs Ervin <airween@gmail.com> - 2011-04-26 20:44 +0200
              Re: Py_INCREF() incomprehension Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-04-27 11:58 +0200
                Re: Py_INCREF() incomprehension Hegedüs Ervin <airween@gmail.com> - 2011-05-01 22:00 +0200
                Re: Py_INCREF() incomprehension Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-05-02 10:48 +1200
                Re: Py_INCREF() incomprehension Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-05-02 02:59 +0200
                Re: Py_INCREF() incomprehension Hegedüs, Ervin <airween@gmail.com> - 2011-05-02 08:41 +0200
                Re: Py_INCREF() incomprehension Stefan Behnel <stefan_ml@behnel.de> - 2011-05-02 09:07 +0200

csiph-web