Python论坛  - 讨论区

标题:[python-chinese] dict

2006年02月25日 星期六 14:45

Gerald Lee leejd80 at gmail.com
Sat Feb 25 14:45:15 HKT 2006

dict中的update,更新时是使用浅拷贝还是深拷贝?
从源码中看,好像是深拷贝的,哪位大侠确认一把吧,源码附在后面


int
PyDict_Merge(PyObject *a, PyObject *b, int override)
{
    register PyDictObject *mp, *other;
    register int i;
    dictentry *entry;

    /* We accept for the argument either a concrete dictionary object,
     * or an abstract "mapping" object.  For the former, we can do
     * things quite efficiently.  For the latter, we only require that
     * PyMapping_Keys() and PyObject_GetItem() be supported.
     */
    if (a == NULL || !PyDict_Check(a) || b == NULL) {
        PyErr_BadInternalCall();
        return -1;
    }
    mp = (dictobject*)a;
    if (PyDict_Check(b)) {
        other = (dictobject*)b;
        if (other == mp || other->ma_used == 0)
            /* a.update(a) or a.update({}); nothing to do */
            return 0;
        /* Do one big resize at the start, rather than
         * incrementally resizing as we insert new items.  Expect
         * that there will be no (or few) overlapping keys.
         */
        if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
           if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
               return -1;
        }
        for (i = 0; i <= other->ma_mask; i++) {
            entry = &other-;>ma_table[i];
            if (entry->me_value != NULL &&
                (override ||
                 PyDict_GetItem(a, entry->me_key) == NULL)) {
                Py_INCREF(entry->me_key);
                Py_INCREF(entry->me_value);
                insertdict(mp, entry->me_key, entry->me_hash,
                       entry->me_value);
            }
        }
    }
    else {
        /* Do it the generic, slower way */
        PyObject *keys = PyMapping_Keys(b);
        PyObject *iter;
        PyObject *key, *value;
        int status;

        if (keys == NULL)
            /* Docstring says this is equivalent to E.keys() so
             * if E doesn't have a .keys() method we want
             * AttributeError to percolate up.  Might as well
             * do the same for any other error.
             */
            return -1;

        iter = PyObject_GetIter(keys);
        Py_DECREF(keys);
        if (iter == NULL)
            return -1;

        for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
            if (!override && PyDict_GetItem(a, key) != NULL) {
                Py_DECREF(key);
                continue;
            }
            value = PyObject_GetItem(b, key);
            if (value == NULL) {
                Py_DECREF(iter);
                Py_DECREF(key);
                return -1;
            }
            status = PyDict_SetItem(a, key, value);
            Py_DECREF(key);
            Py_DECREF(value);
            if (status < 0) {
                Py_DECREF(iter);
                return -1;
            }
        }
        Py_DECREF(iter);
        if (PyErr_Occurred())
            /* Iterator completed, via error */
            return -1;
    }
    return 0;
}

--
My Blog >> http://leejd.cndev.org
My QQ >> 9847243
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060225/cb4c0a54/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月25日 星期六 15:02

Gerald Lee leejd80 at gmail.com
Sat Feb 25 15:02:40 HKT 2006

自己用Python代码试了一下,好像是浅拷贝的。

在06-2-25,Gerald Lee <leejd80 at gmail.com> 写道:
>
> dict中的update,更新时是使用浅拷贝还是深拷贝?
> 从源码中看,好像是深拷贝的,哪位大侠确认一把吧,源码附在后面
>
>
> int
> PyDict_Merge(PyObject *a, PyObject *b, int override)
> {
>     register PyDictObject *mp, *other;
>     register int i;
>     dictentry *entry;
>
>     /* We accept for the argument either a concrete dictionary object,
>      * or an abstract "mapping" object.  For the former, we can do
>      * things quite efficiently.  For the latter, we only require that
>      * PyMapping_Keys() and PyObject_GetItem() be supported.
>      */
>     if (a == NULL || !PyDict_Check(a) || b == NULL) {
>         PyErr_BadInternalCall();
>         return -1;
>     }
>     mp = (dictobject*)a;
>     if (PyDict_Check(b)) {
>         other = (dictobject*)b;
>         if (other == mp || other->ma_used == 0)
>             /* a.update(a) or a.update({}); nothing to do */
>             return 0;
>         /* Do one big resize at the start, rather than
>          * incrementally resizing as we insert new items.  Expect
>          * that there will be no (or few) overlapping keys.
>          */
>         if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
>            if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
>                return -1;
>         }
>         for (i = 0; i <= other->ma_mask; i++) {
>             entry = &other-;>ma_table[i];
>             if (entry->me_value != NULL &&
>                 (override ||
>                  PyDict_GetItem(a, entry->me_key) == NULL)) {
>                 Py_INCREF(entry->me_key);
>                 Py_INCREF(entry->me_value);
>                 insertdict(mp, entry->me_key, entry->me_hash,
>                        entry->me_value);
>             }
>         }
>     }
>     else {
>         /* Do it the generic, slower way */
>         PyObject *keys = PyMapping_Keys(b);
>         PyObject *iter;
>         PyObject *key, *value;
>         int status;
>
>         if (keys == NULL)
>             /* Docstring says this is equivalent to E.keys () so
>              * if E doesn't have a .keys() method we want
>              * AttributeError to percolate up.  Might as well
>              * do the same for any other error.
>              */
>             return -1;
>
>         iter = PyObject_GetIter(keys);
>         Py_DECREF(keys);
>         if (iter == NULL)
>             return -1;
>
>         for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
>             if (!override && PyDict_GetItem(a, key) != NULL) {
>                 Py_DECREF(key);
>                 continue;
>             }
>             value = PyObject_GetItem(b, key);
>             if (value == NULL) {
>                 Py_DECREF(iter);
>                 Py_DECREF(key);
>                 return -1;
>             }
>             status = PyDict_SetItem(a, key, value);
>             Py_DECREF(key);
>             Py_DECREF(value);
>             if (status < 0) {
>                 Py_DECREF(iter);
>                 return -1;
>             }
>         }
>         Py_DECREF(iter);
>         if (PyErr_Occurred())
>             /* Iterator completed, via error */
>             return -1;
>     }
>     return 0;
> }
>
> --
> My Blog >> http://leejd.cndev.org
> My QQ >> 9847243




--
My Blog >> http://leejd.cndev.org
My QQ >> 9847243
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060225/5d2e5e50/attachment-0001.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月25日 星期六 15:09

xxmplus xxmplus at gmail.com
Sat Feb 25 15:09:45 HKT 2006

不能做deep copy是因为default copy constructor不知道怎么copy

在 06-2-25,Gerald Lee<leejd80 at gmail.com> 写道:
> 自己用Python代码试了一下,好像是浅拷贝的。
>
> 在06-2-25,Gerald Lee <leejd80 at gmail.com> 写道:
> > dict中的update,更新时是使用浅拷贝还是深拷贝?
> > 从源码中看,好像是深拷贝的,哪位大侠确认一把吧,源码附在后面
> >
> >
> > int
> > PyDict_Merge(PyObject *a, PyObject *b, int override)
> > {
> >     register PyDictObject *mp, *other;
> >     register int i;
> >     dictentry *entry;
> >
> >     /* We accept for the argument either a concrete dictionary object,
> >      * or an abstract "mapping" object.  For the former, we can do
> >      * things quite efficiently.  For the latter, we only require that
> >      * PyMapping_Keys() and PyObject_GetItem() be supported.
> >      */
> >     if (a == NULL || !PyDict_Check(a) || b == NULL) {
> >         PyErr_BadInternalCall();
> >         return -1;
> >     }
> >     mp = (dictobject*)a;
> >     if (PyDict_Check(b)) {
> >         other = (dictobject*)b;
> >         if (other == mp || other->ma_used == 0)
> >             /* a.update(a) or a.update({}); nothing to do */
> >             return 0;
> >         /* Do one big resize at the start, rather than
> >          * incrementally resizing as we insert new items.  Expect
> >          * that there will be no (or few) overlapping keys.
> >          */
> >         if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
> >            if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
> >                return -1;
> >         }
> >         for (i = 0; i <= other->ma_mask; i++) {
> >             entry = &other-;>ma_table[i];
> >             if (entry->me_value != NULL &&
> >                 (override ||
> >                  PyDict_GetItem(a, entry->me_key) == NULL)) {
> >                 Py_INCREF(entry->me_key);
> >                 Py_INCREF(entry->me_value);
> >                 insertdict(mp, entry->me_key, entry->me_hash,
> >                        entry->me_value);
> >             }
> >         }
> >     }
> >     else {
> >         /* Do it the generic, slower way */
> >         PyObject *keys = PyMapping_Keys(b);
> >         PyObject *iter;
> >         PyObject *key, *value;
> >         int status;
> >
> >         if (keys == NULL)
> >             /* Docstring says this is equivalent to E.keys () so
> >              * if E doesn't have a .keys() method we want
> >              * AttributeError to percolate up.  Might as well
> >              * do the same for any other error.
> >              */
> >             return -1;
> >
> >         iter = PyObject_GetIter(keys);
> >         Py_DECREF(keys);
> >         if (iter == NULL)
> >             return -1;
> >
> >         for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
> >             if (!override && PyDict_GetItem(a, key) != NULL) {
> >                 Py_DECREF(key);
> >                 continue;
> >             }
> >             value = PyObject_GetItem(b, key);
> >             if (value == NULL) {
> >                 Py_DECREF(iter);
> >                 Py_DECREF(key);
> >                 return -1;
> >             }
> >             status = PyDict_SetItem(a, key, value);
> >             Py_DECREF(key);
> >             Py_DECREF(value);
> >             if (status < 0) {
> >                 Py_DECREF(iter);
> >                 return -1;
> >             }
> >         }
> >         Py_DECREF(iter);
> >         if (PyErr_Occurred())
> >             /* Iterator completed, via error */
> >             return -1;
> >     }
> >     return 0;
> > }
> >
> > --
> > My Blog >> http://leejd.cndev.org
> > My QQ >> 9847243
>
>
>
> --
> My Blog >> http://leejd.cndev.org
> My QQ >> 9847243
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月26日 星期日 21:07

Robert Chen search.pythoner at gmail.com
Sun Feb 26 21:07:30 HKT 2006

从代码看,确实应该是浅拷贝。关于PyDictObject的源代码的一些分析,可以参考
 Python源码剖析[12] ――
字典对象PyDictObject(1)<http://blog.donews.com/lemur/archive/2005/12/31/678431.aspx>
 Python源码剖析[13] ――
字典对象PyDictObject(2)<http://blog.donews.com/lemur/archive/2005/12/31/678438.aspx>
 Python源码剖析[14] ――
字典对象PyDictObject(3)<http://blog.donews.com/lemur/archive/2006/01/04/683315.aspx>



On 2/25/06, xxmplus <xxmplus at gmail.com> wrote:
>
> 不能做deep copy是因为default copy constructor不知道怎么copy
>
> 在 06-2-25,Gerald Lee<leejd80 at gmail.com> 写道:
> > 自己用Python代码试了一下,好像是浅拷贝的。
> >
> > 在06-2-25,Gerald Lee <leejd80 at gmail.com> 写道:
> > > dict中的update,更新时是使用浅拷贝还是深拷贝?
> > > 从源码中看,好像是深拷贝的,哪位大侠确认一把吧,源码附在后面
> > >
> > >
> > > int
> > > PyDict_Merge(PyObject *a, PyObject *b, int override)
> > > {
> > >     register PyDictObject *mp, *other;
> > >     register int i;
> > >     dictentry *entry;
> > >
> > >     /* We accept for the argument either a concrete dictionary object,
> > >      * or an abstract "mapping" object.  For the former, we can do
> > >      * things quite efficiently.  For the latter, we only require that
> > >      * PyMapping_Keys() and PyObject_GetItem() be supported.
> > >      */
> > >     if (a == NULL || !PyDict_Check(a) || b == NULL) {
> > >         PyErr_BadInternalCall();
> > >         return -1;
> > >     }
> > >     mp = (dictobject*)a;
> > >     if (PyDict_Check(b)) {
> > >         other = (dictobject*)b;
> > >         if (other == mp || other->ma_used == 0)
> > >             /* a.update(a) or a.update({}); nothing to do */
> > >             return 0;
> > >         /* Do one big resize at the start, rather than
> > >          * incrementally resizing as we insert new items.  Expect
> > >          * that there will be no (or few) overlapping keys.
> > >          */
> > >         if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
> > >            if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
> > >                return -1;
> > >         }
> > >         for (i = 0; i <= other->ma_mask; i++) {
> > >             entry = &other-;>ma_table[i];
> > >             if (entry->me_value != NULL &&
> > >                 (override ||
> > >                  PyDict_GetItem(a, entry->me_key) == NULL)) {
> > >                 Py_INCREF(entry->me_key);
> > >                 Py_INCREF(entry->me_value);
> > >                 insertdict(mp, entry->me_key, entry->me_hash,
> > >                        entry->me_value);
> > >             }
> > >         }
> > >     }
> > >     else {
> > >         /* Do it the generic, slower way */
> > >         PyObject *keys = PyMapping_Keys(b);
> > >         PyObject *iter;
> > >         PyObject *key, *value;
> > >         int status;
> > >
> > >         if (keys == NULL)
> > >             /* Docstring says this is equivalent to E.keys () so
> > >              * if E doesn't have a .keys() method we want
> > >              * AttributeError to percolate up.  Might as well
> > >              * do the same for any other error.
> > >              */
> > >             return -1;
> > >
> > >         iter = PyObject_GetIter(keys);
> > >         Py_DECREF(keys);
> > >         if (iter == NULL)
> > >             return -1;
> > >
> > >         for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
> > >             if (!override && PyDict_GetItem(a, key) != NULL) {
> > >                 Py_DECREF(key);
> > >                 continue;
> > >             }
> > >             value = PyObject_GetItem(b, key);
> > >             if (value == NULL) {
> > >                 Py_DECREF(iter);
> > >                 Py_DECREF(key);
> > >                 return -1;
> > >             }
> > >             status = PyDict_SetItem(a, key, value);
> > >             Py_DECREF(key);
> > >             Py_DECREF(value);
> > >             if (status < 0) {
> > >                 Py_DECREF(iter);
> > >                 return -1;
> > >             }
> > >         }
> > >         Py_DECREF(iter);
> > >         if (PyErr_Occurred())
> > >             /* Iterator completed, via error */
> > >             return -1;
> > >     }
> > >     return 0;
> > > }
> > >
> > > --
> > > My Blog >> http://leejd.cndev.org
> > > My QQ >> 9847243
> >
> >
> >
> > --
> > My Blog >> http://leejd.cndev.org
> > My QQ >> 9847243
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
Robert
Python源码剖析――http://blog.donews.com/lemur/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060226/a5d2bcb5/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月27日 星期一 17:08

taodev taodev at gmail.com
Mon Feb 27 17:08:52 HKT 2006

请教一下, boost::python里面,如何取得python运行时的异常,比如代码书写错误,调用错误等等





taodev
2006-02-27



发件人: Gerald Lee
发送时间: 2006-02-25 14:46:33
收件人: python-cn at googlegroups.com; python-chinese at lists.python.cn
抄送: 
主题: [python-chinese] dict

dict中的update,更新时是使用浅拷贝还是深拷贝?
从源码中看,好像是深拷贝的,哪位大侠确认一把吧,源码附在后面


int
PyDict_Merge(PyObject *a, PyObject *b, int override)
{
    register PyDictObject *mp, *other;
    register int i;
    dictentry *entry; 

    /* We accept for the argument either a concrete dictionary object,
     * or an abstract "mapping" object.  For the former, we can do
     * things quite efficiently.  For the latter, we only require that 
     * PyMapping_Keys() and PyObject_GetItem() be supported.
     */
    if (a == NULL || !PyDict_Check(a) || b == NULL) {
        PyErr_BadInternalCall();
        return -1;
    }
    mp = (dictobject*)a; 
    if (PyDict_Check(b)) {
        other = (dictobject*)b;
        if (other == mp || other->ma_used == 0)
            /* a.update(a) or a.update({}); nothing to do */
            return 0;
        /* Do one big resize at the start, rather than 
         * incrementally resizing as we insert new items.  Expect
         * that there will be no (or few) overlapping keys.
         */
        if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) { 
           if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
               return -1;
        }
        for (i = 0; i <= other->ma_mask; i++) {
            entry = &other-;>ma_table[i]; 
            if (entry->me_value != NULL &&
                (override ||
                 PyDict_GetItem(a, entry->me_key) == NULL)) {
                Py_INCREF(entry->me_key);
                Py_INCREF(entry->me_value); 
                insertdict(mp, entry->me_key, entry->me_hash,
                       entry->me_value);
            }
        }
    }
    else {
        /* Do it the generic, slower way */
        PyObject *keys = PyMapping_Keys(b);
        PyObject *iter;
        PyObject *key, *value;
        int status;

        if (keys == NULL)
            /* Docstring says this is equivalent to E.keys () so
             * if E doesn't have a .keys() method we want
             * AttributeError to percolate up.  Might as well
             * do the same for any other error.
             */
            return -1; 

        iter = PyObject_GetIter(keys);
        Py_DECREF(keys);
        if (iter == NULL)
            return -1;

        for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
            if (!override && PyDict_GetItem(a, key) != NULL) { 
                Py_DECREF(key);
                continue;
            }
            value = PyObject_GetItem(b, key);
            if (value == NULL) {
                Py_DECREF(iter);
                Py_DECREF(key); 
                return -1;
            }
            status = PyDict_SetItem(a, key, value);
            Py_DECREF(key);
            Py_DECREF(value);
            if (status < 0) {
                Py_DECREF(iter); 
                return -1;
            }
        }
        Py_DECREF(iter);
        if (PyErr_Occurred())
            /* Iterator completed, via error */
            return -1;
    }
    return 0; 
}

-- 
My Blog >> http://leejd.cndev.org
My QQ >> 9847243 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060227/bb0a94e4/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月27日 星期一 18:24

刘鑫 march.liu at gmail.com
Mon Feb 27 18:24:49 HKT 2006

以下来自Boost.Python文档:

Exception handling

If an exception occurs in the execution of some Python code, the
PyRun_String <http://www.python.org/doc/current/api/veryhigh.html#l2h-55>function
returns a null pointer. Constructing a
handle out of this null pointer throws
error_already_set<file:///D:/boost_1_33_1/libs/python/doc/v2/errors.html#error_already_set-spec>,
so basically, the Python exception is automatically translated into a C++
exception when using handle:

try
{
    object result((handle(PyRun_String
<http://www.python.org/doc/current/api/veryhigh.html#l2h-55>(
        "5/0"
      , Py_eval_input
<http://www.python.org/doc/current/api/veryhigh.html#l2h-58>
      , main_namespace.ptr()
      , main_namespace.ptr()))
    ));

    // execution will never get here:
    int five_divided_by_zero = extract(result);
}
catch(error_already_set)
{
    // handle the exception in some way
}

 The error_already_set exception class doesn't carry any information in
itself. To find out more about the Python exception that occurred, you need
to use the exception handling
functions<http://www.python.org/doc/api/exceptionHandling.html>of the
Python/C API in your catch-statement. This can be as simple as
calling PyErr_Print()<http://www.python.org/doc/api/exceptionHandling.html#l2h-70>to
print the exception's traceback to the console, or comparing the type
of
the exception with those of the standard
exceptions<http://www.python.org/doc/api/standardExceptions.html>
:

catch(error_already_set)
{
    if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError))
    {
        // handle ZeroDivisionError specially
    }
    else
    {
        // print all other errors to stderr
        PyErr_Print();
    }
}

 (To retrieve even more information from the exception you can use some of
the other exception handling functions listed
here<http://www.python.org/doc/api/exceptionHandling.html>
.)

If you'd rather not have handle throw a C++ exception when it is
constructed, you can use the
allow_null<file:///D:/boost_1_33_1/libs/python/doc/v2/handle.html#allow_null-spec>function
in the same way you'd use borrowed:

handle result((allow_null(PyRun_String
<http://www.python.org/doc/current/api/veryhigh.html#l2h-55>(
    "5/0"
   , Py_eval_input <http://www.python.org/doc/current/api/veryhigh.html#l2h-58>
   , main_namespace.ptr()
   , main_namespace.ptr()))));

if (!result)
    // Python exception occurred
else
    // everything went okay, it's safe to use the result



在06-2-27,taodev <taodev at gmail.com> 写道:
>
> 请教一下, boost::python里面,如何取得python运行时的异常,比如代码书写错误,调用错误等等
>
>
>  ------------------------------
>  taodev
> 2006-02-27
>  ------------------------------
>  *发件人:* Gerald Lee
> *发送时间:* 2006-02-25 14:46:33
> *收件人:* python-cn at googlegroups.com; python-chinese at lists.python.cn
> *抄送:*
> *主题:* [python-chinese] dict
>
> dict中的update,更新时是使用浅拷贝还是深拷贝?
> 从源码中看,好像是深拷贝的,哪位大侠确认一把吧,源码附在后面
>
>
> int
> PyDict_Merge(PyObject *a, PyObject *b, int override)
> {
>     register PyDictObject *mp, *other;
>     register int i;
>     dictentry *entry;
>
>     /* We accept for the argument either a concrete dictionary object,
>      * or an abstract "mapping" object.  For the former, we can do
>      * things quite efficiently.  For the latter, we only require that
>      * PyMapping_Keys() and PyObject_GetItem() be supported.
>      */
>     if (a == NULL || !PyDict_Check(a) || b == NULL) {
>         PyErr_BadInternalCall();
>         return -1;
>     }
>     mp = (dictobject*)a;
>     if (PyDict_Check(b)) {
>         other = (dictobject*)b;
>         if (other == mp || other->ma_used == 0)
>             /* a.update(a) or a.update({}); nothing to do */
>             return 0;
>         /* Do one big resize at the start, rather than
>          * incrementally resizing as we insert new items.  Expect
>          * that there will be no (or few) overlapping keys.
>          */
>         if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
>            if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
>                return -1;
>         }
>         for (i = 0; i <= other->ma_mask; i++) {
>             entry = &other-;>ma_table[i];
>             if (entry->me_value != NULL &&
>                 (override ||
>                  PyDict_GetItem(a, entry->me_key) == NULL)) {
>                 Py_INCREF(entry->me_key);
>                 Py_INCREF(entry->me_value);
>                 insertdict(mp, entry->me_key, entry->me_hash,
>                        entry->me_value);
>             }
>         }
>     }
>     else {
>         /* Do it the generic, slower way */
>         PyObject *keys = PyMapping_Keys(b);
>         PyObject *iter;
>         PyObject *key, *value;
>         int status;
>
>         if (keys == NULL)
>             /* Docstring says this is equivalent to E.keys () so
>              * if E doesn't have a .keys() method we want
>              * AttributeError to percolate up.  Might as well
>              * do the same for any other error.
>              */
>             return -1;
>
>         iter = PyObject_GetIter(keys);
>         Py_DECREF(keys);
>         if (iter == NULL)
>             return -1;
>
>         for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
>             if (!override && PyDict_GetItem(a, key) != NULL) {
>                 Py_DECREF(key);
>                 continue;
>             }
>             value = PyObject_GetItem(b, key);
>             if (value == NULL) {
>                 Py_DECREF(iter);
>                 Py_DECREF(key);
>                 return -1;
>             }
>             status = PyDict_SetItem(a, key, value);
>             Py_DECREF(key);
>             Py_DECREF(value);
>             if (status < 0) {
>                 Py_DECREF(iter);
>                 return -1;
>             }
>         }
>         Py_DECREF(iter);
>         if (PyErr_Occurred())
>             /* Iterator completed, via error */
>             return -1;
>     }
>     return 0;
> }
>
> --
> My Blog >> http://leejd.cndev.org
> My QQ >> 9847243
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
欢迎访问:
http://blog.csdn.net/ccat

刘鑫
March.Liu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060227/45b47a39/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月27日 星期一 20:22

taodev taodev at gmail.com
Mon Feb 27 20:22:49 HKT 2006

在控制台模式下面,可以PyErr_Print显示出错误信息,但在Win32程序里面,如何取得PyErr_Print所打印的内容呢?
麻烦各位了
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060227/232727b1/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月27日 星期一 23:16

Gerald Lee leejd80 at gmail.com
Mon Feb 27 23:16:10 HKT 2006

重定向stderr输出

在06-2-27,taodev <taodev at gmail.com> 写道:
>
> 在控制台模式下面,可以PyErr_Print显示出错误信息,但在Win32程序里面,如何取得PyErr_Print所打印的内容呢?
> 麻烦各位了
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
My Blog >> http://leejd.cndev.org
My QQ >> 9847243
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060227/f07112be/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 09:58

taodev taodev at gmail.com
Tue Feb 28 09:58:47 HKT 2006

我自己写了一个python文件,需要在PlayerPrint里面调用并且修改playerCount, 但在从另外一个文件里面import这个模块时,就提示: UnboundLocalError: local variable 'playerCount' referenced before assignment
就是申明了global也不行

源代码如下:
myplayer.py
from testDerived import *

myVersion = '0.1'
playerCount = 0

def PlayerPrint():
      playerCount += 1
      myDebugString("player userd: " + str(playerCount))


test.py
import myplayer

PlayerPrint()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060228/50b7e7fe/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 10:07

limodou limodou at gmail.com
Tue Feb 28 10:07:52 HKT 2006

On 2/28/06, taodev <taodev at gmail.com> wrote:
>
> 我自己写了一个python文件,需要在PlayerPrint里面调用并且修改playerCount,
> 但在从另外一个文件里面import这个模块时,就提示: UnboundLocalError: local variable 'playerCount'
> referenced before assignment
> 就是申明了global也不行
>
> 源代码如下:
> myplayer.py
> from testDerived import *
>
> myVersion = '0.1'
> playerCount = 0

这只是表明playerCount是一个全局变量,但在局部作用域中如果只是引用,则没有问题,一旦有赋值就不同了。
>
> def PlayerPrint():
>       playerCount += 1

这实际上相当于:

   playerCount = playerCount + 1

一个赋值语句就是创建变量引用的地方,它要检查在当前的作用域中是否已经有定义。而def是一个函数,则说明为局部作用域。而playerCount
+ 1说明需要先存在一个playerCount的定义,但在函数中并不存在。如果你想使用全部变量,需要先使用global 声明一下,如:

def PlayerPrint():
      global playerCount
      playerCount += 1

这样就会去全局变量中查找playerCount了。

这是python作用域中很重要的部分。


>       myDebugString("player userd: " + str(playerCount))
>
>
> test.py
> import myplayer
>
> PlayerPrint()
>



--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 11:11

taodev taodev at gmail.com
Tue Feb 28 11:11:00 HKT 2006

limodou大侠,偶再问一下如果取得Python C接口中的PyErr_Print所打印出来的数据.

我想取得PyErr_Print想要打印的字符串数据, 然后在我的win32工程里面,直接显示一个messageBox来提示
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060228/f8568bdb/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 11:14

limodou limodou at gmail.com
Tue Feb 28 11:14:34 HKT 2006

On 2/28/06, taodev <taodev at gmail.com> wrote:
>
> limodou大侠,偶再问一下如果取得Python C接口中的PyErr_Print所打印出来的数据.
>
> 我想取得PyErr_Print想要打印的字符串数据,
> 然后在我的win32工程里面,直接显示一个messageBox来提示

我对嵌入式不了解,看看别人有知道的吗?

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 11:58

taodev taodev at gmail.com
Tue Feb 28 11:58:01 HKT 2006

好的,谢谢




taodev
2006-02-28



发件人: limodou
发送时间: 2006-02-28 11:15:48
收件人: python-chinese at lists.python.cn
抄送: 
主题: Re: [python-chinese] limodou大侠,偶再问一下如果取得Python C接口中的PyErr_Print所打印出来的数据.

On  2/28/06,  taodev   <taodev at gmail.com >  wrote:
>
>  limodou大侠,偶再问一下如果取得Python  C接口中的PyErr_Print所打印出来的数据.
>
>  我想取得PyErr_Print想要打印的字符串数据,
>  然后在我的win32工程里面,直接显示一个messageBox来提示

我对嵌入式不了解,看看别人有知道的吗?

--
I  like  python!
My  Blog:  http://www.donews.net/limodou
NewEdit  Maillist:  http://groups.google.com/group/NewEdit
_______________________________________________
python-chinese
Post:  send  python-chinese at lists.python.cn
Subscribe:  send  subscribe  to  python-chinese-request at lists.python.cn
Unsubscribe:  send  unsubscribe  to    python-chinese-request at lists.python.cn
Detail  Info:  http://python.cn/mailman/listinfo/python-chinese
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060228/2e483037/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 12:12

Robert Chen search.pythoner at gmail.com
Tue Feb 28 12:12:54 HKT 2006

呵呵,我提供一个思路,直接去修改PyErr_Print的源代码,让PyErr_Print调用你的输出函数。当然这个就要求别人使用你的程序的时候也必须使用你修改后的Python了
:)


On 2/28/06, taodev <taodev at gmail.com> wrote:
>
> 好的,谢谢
>
>  ------------------------------
>  taodev
> 2006-02-28
>  ------------------------------
>  *发件人:* limodou
> *发送时间:* 2006-02-28 11:15:48
> *收件人:* python-chinese at lists.python.cn
> *抄送:*
> *主题:* Re: [python-chinese] limodou大侠,偶再问一下如果取得Python
> C接口中的PyErr_Print所打印出来的数据.
>
>  On  2/28/06,  taodev   <taodev at gmail.com >  wrote:
> >
> >  limodou大侠,偶再问一下如果取得Python  C接口中的PyErr_Print所打印出来的数据.
> >
> >  我想取得PyErr_Print想要打印的字符串数据,
> >  然后在我的win32工程里面,直接显示一个messageBox来提示
>
> 我对嵌入式不了解,看看别人有知道的吗?
>
> --
> I  like  python!
> My  Blog:  http://www.donews.net/limodou
> NewEdit  Maillist:  http://groups.google.com/group/NewEdit
> _______________________________________________
> python-chinese
> Post:  send  python-chinese at lists.python.cn
> Subscribe:  send  subscribe  to  python-chinese-request at lists.python.cn
> Unsubscribe:  send  unsubscribe  to
> python-chinese-request at lists.python.cn
> Detail  Info:  http://python.cn/mailman/listinfo/python-chinese
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
Robert
Python源码剖析――http://blog.donews.com/lemur/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060228/33e54846/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 20:56

FireBird ygonic at gmail.com
Tue Feb 28 20:56:07 HKT 2006

重新定义sys.stderr应该就可以得到字符串了。


在 06-2-28,Robert Chen<search.pythoner at gmail.com> 写道:
> 呵呵,我提供一个思路,直接去修改PyErr_Print的源代码,让PyErr_Print调用你的输出函数。当然这个就要求别人使用你的程序的时候也必须使用你修改后的Python了
> :)
>
>
>
> On 2/28/06, taodev <taodev at gmail.com> wrote:
> >
> > 好的,谢谢
> >
> >
> > ________________________________
>
> > taodev
> > 2006-02-28
> > ________________________________
>
> > 发件人: limodou
> > 发送时间: 2006-02-28 11:15:48
> > 收件人: python-chinese at lists.python.cn
> > 抄送:
> > 主题: Re: [python-chinese] limodou大侠,偶再问一下如果取得Python
> C接口中的PyErr_Print所打印出来的数据.
> >
> >
> >
> > On  2/28/06,  taodev   <taodev at gmail.com >  wrote:
> > >
> > >  limodou大侠,偶再问一下如果取得Python  C接口中的PyErr_Print所打印出来的数据.
> > >
> > >  我想取得PyErr_Print想要打印的字符串数据,
> > >  然后在我的win32工程里面,直接显示一个messageBox来提示
> >
> > 我对嵌入式不了解,看看别人有知道的吗?
> >
> > --
> > I  like  python!
> > My  Blog:  http://www.donews.net/limodou
> > NewEdit  Maillist:
> http://groups.google.com/group/NewEdit
> > _______________________________________________
> > python-chinese
> > Post:  send  python-chinese at lists.python.cn
> > Subscribe:  send  subscribe  to
> python-chinese-request at lists.python.cn
> > Unsubscribe:  send  unsubscribe  to
> python-chinese-request at lists.python.cn
> > Detail  Info:
> http://python.cn/mailman/listinfo/python-chinese
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
>
> --
> Robert
> Python源码剖析――http://blog.donews.com/lemur/
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 20:59

FireBird ygonic at gmail.com
Tue Feb 28 20:59:16 HKT 2006

我想应该是调用PlayerPrint()的方式用问题吧,试试
myplayer.PlayerPrint()


> test.py
> import myplayer
>
> PlayerPrint()
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月28日 星期二 22:33

Peng Xiaoyu peng.xiaoyu at gmail.com
Tue Feb 28 22:33:01 HKT 2006

对于作用域我也不太清楚,比如下面的例子,a到底是属于那个作用域,inc是对哪个a操作

(~) pxy at equaker $ cat test.py
a = 1

def inc():
    global a
    a += 1

>>> from test import a
>>> from test import inc
>>> a
1
>>> inc()
>>> a
1
>>> import test
>>> a
1
>>> test.a
2





在06-2-28,FireBird <ygonic at gmail.com> 写道:
>
> 我想应该是调用PlayerPrint()的方式用问题吧,试试
> myplayer.PlayerPrint()
>
>
> > test.py
> > import myplayer
> >
> > PlayerPrint()
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060228/81830e36/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号