<div dir="ltr">This mailing list is for the development <b>of</b> Python, the the <b>use</b> of Python. Your best option for getting an answer is to ask on python-list or python-help.</div><div class="gmail_extra"><br><br>
<div class="gmail_quote">On Tue, Mar 18, 2014 at 9:50 PM, 北冰洋 <span dir="ltr"><<a href="mailto:wtz_wh@foxmail.com" target="_blank">wtz_wh@foxmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><span style="line-height:1.5">Dear,</span></div><div><div><div><br></div><div>I just wrote a sample like this:</div><div>testPy/</div><div> __init__.py</div><div> client.py</div><div> SoamFactory.c</div>
<div> SoamFactory.so</div><div> soamapi.py</div><div> sample/testP.py</div><div>export PYTHONPATH=$(TEST_LOCATION):$(TEST_LOCATION)/testPy</div><div>Here's the source codes:</div><div><b>__init__.py:</b></div>
<div>import client</div><div><b>client.py</b></div><div><div>import soamapi</div><div>class Client(soamapi.SessionCallback):</div><div><br></div><div> def __init__(self):</div><div> print "----class Client----"</div>
<div> super(Client, self).__init__()</div><div> soamapi.initialize()</div><div><br></div><div> def create_session(self):</div><div> sec_cb = soamapi.DefaultSecurityCallback()</div><div> self.connection = soamapi.connect(sec_cb)</div>
</div><div><b>soamapi.py</b></div><div><div>import SoamFactory</div><div><br></div><div>class ConnectionSecurityCallback(object): </div><div> def __init__(self):</div><div> print "----class ConnectionSecurityCallback----"</div>
<div><br></div><div> def __del__(self):</div><div> pass</div><div><br></div><div> def test_P(self):</div><div> print "test_P in class ConnectionSecurityCallback"</div><div><br></div><div>class DefaultSecurityCallback(ConnectionSecurityCallback):</div>
<div> def __init__(self):</div><div> super(DefaultSecurityCallback, self).__init__()</div><div> print "----class DefaultSecurityCallback----"</div><div><br></div><div> def __del__(self):</div><div> super(DefaultSecurityCallback, self).__del__()</div>
<div><br></div><div> def test(self):</div><div> print "test in class DefaultSecurityCallback"</div><div><br></div><div>class SessionCallback(object):</div><div> def __init__(self):</div><div> pass</div>
<div>
</div><div> def __del__(self):</div><div> pass</div><div><br></div><div>def connect(security_callback):</div><div> return SoamFactory.SoamFactory_connect(security_callback)</div><div><br></div><div>def initialize():</div>
<div> SoamFactory.SoamFactory_initialize()</div><div><br></div><div>print "call soamapi"</div></div><div><b>SoamFactory.c</b></div><div><div>#include "Python.h"</div><div>#include "PythonLoop.c"</div>
<div><br></div><div>PyObject* SOAM_API_MODULE = NULL;</div><div>PyObject* pyModule = NULL;</div><div><br></div><div>static PyObject* SoamFactory_initialize(PyObject* self, PyObject* args){</div><div> PyEval_InitThreads();</div>
<div> init();</div><div> Py_RETURN_NONE;</div><div>}</div><div><br></div><div>static PyObject* SoamFactory_connect(PyObject* self, PyObject* args){</div><div> PyObject* pySecCallback = NULL;</div><div> int ok = PyArg_ParseTuple(args, "O", &pySecCallback);</div>
<div> if (!ok){</div><div> printf("parse tuple error!\n");</div><div> Py_RETURN_NONE;</div><div> }</div><div> if (! PyObject_IsInstance(pySecCallback, connectSecCallback)){</div><div> printf("pySecCallback is not an instance of ConnectionSecurityCallback!\n");</div>
<div> Py_RETURN_NONE;</div><div> }</div><div> printf("Successful!\n");</div><div> Py_RETURN_NONE;</div><div>}</div><div><br></div><div>static PyMethodDef SoamFactory[] = {</div><div> {"SoamFactory_connect", SoamFactory_connect, METH_VARARGS, "connection function"},</div>
<div> {"SoamFactory_initialize", SoamFactory_initialize, METH_VARARGS, "SoamFactory_initialize"},</div><div> {NULL, NULL}</div><div>};</div><div><br></div><div>void initSoamFactory(){</div><div>
PyEval_InitThreads();</div>
<div> Py_Initialize();</div><div> pyModule = Py_InitModule("SoamFactory", SoamFactory);</div><div> SOAM_API_MODULE = PyImport_ImportModule("soamapi");</div><div>}</div></div><div><b>sample/testP.py</b></div>
<div><div>import testPy</div><div><br></div><div>print "========================================"</div><div>submitter = testPy.client.Client()</div><div>submitter.create_session()</div><div>print "========================================"</div>
</div><div><br></div><div>When I ran it on python2.4, it worked well, and the result was</div><div><i>call soamapi</i></div><div><i>after import soamapi------client.py</i></div><div><i>========================================</i></div>
<div><i>----class Client----</i></div><div><i>----class ConnectionSecurityCallback----</i></div><div><i>----class DefaultSecurityCallback----</i></div><div><i>Successful!</i></div><div><span style="line-height:1.5"><i>========================================</i></span> </div>
<div> </div><div><span style="line-height:1.5">But when I ran it on python2.7, it worked beyond my expectation, the result was </span></div><div><div>call soamapi</div><div>call soamapi</div><div>========================================</div>
<div>----class Client----</div><div>----class ConnectionSecurityCallback----</div><div>----class DefaultSecurityCallback----</div><div>pySecCallback is not an instance of ConnectionSecurityCallback!</div><div>========================================</div>
</div><div><br></div><div>I found that soamapi was imported twice, and I investigated this is related to absolute&relative import way. <span style="line-height:1.5">PyImport_ImportModule in python2.7 uses absolute import way, it will look up sys.path to get soamapi module, and when testP.py file import testPy module, it will find local module soamapi under testPy package, and binds module's name to package, as testPy.soamapi.</span></div>
<div>There are two ways to correct it for python2.7, 1) Don't use import testPy, use import client directly to avoid using relative; 2) Use from __future__ import absolute_import to enable absolute import feature.</div>
<div><br></div><div>But there are two Pre-conditions:</div><div>1) Should not modify testP.py;</div><div>2) Should be ran on both python2.4 and 2.7.</div><div><br></div><div>I don't know how to fix it. Is there any official way about how to porting this scenario or better idea?</div>
<div><br></div><div>Thanks,</div><div>Vatel</div></div></div><br>_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/brett%40python.org" target="_blank">https://mail.python.org/mailman/options/python-dev/brett%40python.org</a><br>
<br></blockquote></div><br></div>