PySequence_SetItem [英] PySequence_SetItem

查看:93
本文介绍了PySequence_SetItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下代码非常直接来自Python / C参考手册的

部分1.2.1.1:


#include< Python.h>


int

main(无效)

{

PyObject * l,* x;


Py_Initialize();


l = PyList_New(3);

x = PyInt_FromLong(1L);


if(l == NULL || x == NULL){

PyErr_Print();

退出(EXIT_FAILURE);

}

PySequence_SetItem(l,0,x);

Py_DECREF(x);


Py_Finalize();

返回EXIT_SUCCESS;

}


不幸的是,它没有'不行。它是段错误的,并且



Py_DECREF宏应用于old_value时,list_ass_item()中出现错误,

设置在699行of Objects / listobject.c

with old_value = a-> ob_item [i]; (old_value是

设置为NULL,而Py_DECREF是

应用于NULL ...繁荣!)


我我非常擅长嵌入/扩展python,

所以我不确定我是不是在做一些令人难以置信的事情

愚蠢,但它看起来像PySequence_SetItem ()

期待在所需的索引处已经有一个

项目。


我做了一些愚蠢的事情吗?


-

Bill Pursell

解决方案



Bill Pursell写道:


以下代码非常直接来自

第1.2.1.1节Python / C参考手册:


#include< Python.h>


int

main(void )

{

PyObject * l,* x;


Py_Initialize();


l = PyList_New(3);

x = P. yInt_FromLong(1L);


if(l == NULL || x == NULL){

PyErr_Print();

退出(EXIT_FAILURE);

}

PySequence_SetItem( l,0,x);

Py_DECREF(x);


Py_Finalize();

返回EXIT_SUCCESS;

}



另请注意,如果我将调用PySequence_SetItem替换为

,问题就会消失:

PySequence_SetItem(l,0,PyInt_FromLong(1L));

但如果我在分配x后添加Py_INCREF(x)

,问题仍然存在。 (这似乎是一件完全愚蠢的事情,但是我正在抓住

字符串......)


Bill Pursell写道:


Bill Pursell写道:


另请注意问题是如果我用以下内容替换

调用PySequence_SetItem:

PySequence_SetItem(1,0,PyInt_FromLong(1L));



你确定吗?它应该完全没有区别。


到目前为止我的经验:

1.在调用PySequence_SetItem时崩溃

2.删除py_DECREF,s / Sequence / List / -works OK

3.添加代码来测试PyWhatever_SetItem的返回值[你应该

*总是*测试错误],s / Sequence / Object / -PyObject_SetItem调用

返回-1,PyErr_Print - " SystemError:null参数内部

例程"


看起来像一两​​个bug。我会翻找一下。


干杯,

John


2006年8月16日星期三01:45:44 PM -0700,John Machin写道:


Bill Pursell写道:


Bill Pursell写道:


另请注意,如果我将调用PySequence_SetItem替换为

,问题就会消失:

PySequence_SetItem(1,0,PyInt_FromLong(1L));



你确定吗?它应该完全没有区别。


到目前为止我的经验:

1.在调用PySequence_SetItem时崩溃

2.删除py_DECREF,s / Sequence / List / -works OK

3.添加代码来测试PyWhatever_SetItem的返回值[你应该

*总是*测试错误],s / Sequence / Object / -PyObject_SetItem调用

返回-1,PyErr_Print - " SystemError:null参数内部

例程"



将PySequence_SetItem更改为PyList_SetItem并删除

DECREF也适用于我(PyList函数窃取引用)。我还试图将b + b设置为长度为1,仍然没有骰子。 PySequence

版本的版本低于2.4和2.5。即使将Int更改为
到字符串也会出现问题。


Yikes,我会捅更多。

-Jack



The following code is pretty much straight out of
section 1.2.1.1 of the Python/C reference manual:

#include <Python.h>

int
main(void)
{
PyObject *l, *x;

Py_Initialize();

l = PyList_New(3);
x = PyInt_FromLong(1L);

if (l == NULL || x == NULL) {
PyErr_Print();
exit (EXIT_FAILURE);
}
PySequence_SetItem(l, 0, x);
Py_DECREF(x);

Py_Finalize();
return EXIT_SUCCESS;
}

Unforunately, it doesn''t work. It segfaults, and
the error occurs in list_ass_item() when the
Py_DECREF macro is applied to old_value, which
was set at line 699 of Objects/listobject.c
with old_value = a->ob_item[i]; (old_value is
being set to NULL, and Py_DECREF is
being applied to NULL...boom!)

I''m totally new to embedding/extending python,
so I''m not sure if I''m doing something incredibly
stupid, but it certainly looks like PySequence_SetItem()
was expecting that there should already be an
item at the desired index.

Am I doing something stupid?

--
Bill Pursell

解决方案


Bill Pursell wrote:

The following code is pretty much straight out of
section 1.2.1.1 of the Python/C reference manual:

#include <Python.h>

int
main(void)
{
PyObject *l, *x;

Py_Initialize();

l = PyList_New(3);
x = PyInt_FromLong(1L);

if (l == NULL || x == NULL) {
PyErr_Print();
exit (EXIT_FAILURE);
}
PySequence_SetItem(l, 0, x);
Py_DECREF(x);

Py_Finalize();
return EXIT_SUCCESS;
}

Also note that the problem goes away if I replace
the call to PySequence_SetItem with:
PySequence_SetItem(l, 0, PyInt_FromLong(1L));
but the problem persists if I add a Py_INCREF(x)
after the assignment of x. (Which seems like a
completely silly thing to do, but I''m grasping
at strings...)


Bill Pursell wrote:

Bill Pursell wrote:

Also note that the problem goes away if I replace
the call to PySequence_SetItem with:
PySequence_SetItem(l, 0, PyInt_FromLong(1L));

Are you sure? It should make absolutely no difference.

My experience so far:
1. crashes in call to PySequence_SetItem
2. removed py_DECREF, s/Sequence/List/ -works OK
3. added code to test return value from PyWhatever_SetItem [you should
*always* test for error], s/Sequence/Object/ -PyObject_SetItem call
returns -1, PyErr_Print -"SystemError: null argument to internal
routine"

Looks like a bug or two. I''ll rummage a bit more.

Cheers,
John


On Wed, Aug 16, 2006 at 01:45:44PM -0700, John Machin wrote:

Bill Pursell wrote:

Bill Pursell wrote:

Also note that the problem goes away if I replace
the call to PySequence_SetItem with:
PySequence_SetItem(l, 0, PyInt_FromLong(1L));


Are you sure? It should make absolutely no difference.

My experience so far:
1. crashes in call to PySequence_SetItem
2. removed py_DECREF, s/Sequence/List/ -works OK
3. added code to test return value from PyWhatever_SetItem [you should
*always* test for error], s/Sequence/Object/ -PyObject_SetItem call
returns -1, PyErr_Print -"SystemError: null argument to internal
routine"

Changing the PySequence_SetItem to PyList_SetItem and dropping the
DECREF works for me too (PyList functions steal a reference). I also
tried setting the list to length 1, still no dice. The PySequence
version segs under 2.4 and 2.5. It segs even when the Int is changed
to a String.

Yikes, I''ll poke around some more.

-Jack


这篇关于PySequence_SetItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆