(original) (raw)
This mailing list is for the development of Python, the the use of Python. Your best option for getting an answer is to ask on python-list or python-help.
On Tue, Mar 18, 2014 at 9:50 PM, 北冰洋 <wtz\_wh@foxmail.com> wrote:
Dear,I just wrote a sample like this:testPy/\_\_init\_\_.pyclient.pySoamFactory.cSoamFactory.sosoamapi.pysample/testP.pyexport PYTHONPATH=$(TEST\_LOCATION):$(TEST\_LOCATION)/testPyHere's the source codes:\_\_init\_\_.py:import clientclient.pyimport soamapiclass Client(soamapi.SessionCallback):def \_\_init\_\_(self):print "----class Client----"super(Client, self).\_\_init\_\_()soamapi.initialize()def create\_session(self):sec\_cb = soamapi.DefaultSecurityCallback()self.connection = soamapi.connect(sec\_cb)soamapi.pyimport SoamFactoryclass ConnectionSecurityCallback(object):def \_\_init\_\_(self):print "----class ConnectionSecurityCallback----"def \_\_del\_\_(self):passdef test\_P(self):print "test\_P in class ConnectionSecurityCallback"class DefaultSecurityCallback(ConnectionSecurityCallback):def \_\_init\_\_(self):super(DefaultSecurityCallback, self).\_\_init\_\_()print "----class DefaultSecurityCallback----"def \_\_del\_\_(self):super(DefaultSecurityCallback, self).\_\_del\_\_()def test(self):print "test in class DefaultSecurityCallback"class SessionCallback(object):def \_\_init\_\_(self):passdef \_\_del\_\_(self):passdef connect(security\_callback):return SoamFactory.SoamFactory\_connect(security\_callback)def initialize():SoamFactory.SoamFactory\_initialize()print "call soamapi"SoamFactory.c#include "Python.h"#include "PythonLoop.c"PyObject\* SOAM\_API\_MODULE = NULL;PyObject\* pyModule = NULL;static PyObject\* SoamFactory\_initialize(PyObject\* self, PyObject\* args){PyEval\_InitThreads();init();Py\_RETURN\_NONE;}static PyObject\* SoamFactory\_connect(PyObject\* self, PyObject\* args){PyObject\* pySecCallback = NULL;int ok = PyArg\_ParseTuple(args, "O", &pySecCallback);if (!ok){printf("parse tuple error!\\n");Py\_RETURN\_NONE;}if (! PyObject\_IsInstance(pySecCallback, connectSecCallback)){printf("pySecCallback is not an instance of ConnectionSecurityCallback!\\n");Py\_RETURN\_NONE;}printf("Successful!\\n");Py\_RETURN\_NONE;}static PyMethodDef SoamFactory\[\] = {{"SoamFactory\_connect", SoamFactory\_connect, METH\_VARARGS, "connection function"},{"SoamFactory\_initialize", SoamFactory\_initialize, METH\_VARARGS, "SoamFactory\_initialize"},{NULL, NULL}};void initSoamFactory(){PyEval\_InitThreads();Py\_Initialize();pyModule = Py\_InitModule("SoamFactory", SoamFactory);SOAM\_API\_MODULE = PyImport\_ImportModule("soamapi");}sample/testP.pyimport testPyprint "========================================"submitter = testPy.client.Client()submitter.create\_session()print "========================================"When I ran it on python2.4, it worked well, and the result wascall soamapiafter import soamapi------client.py========================================----class Client--------class ConnectionSecurityCallback--------class DefaultSecurityCallback----Successful!========================================But when I ran it on python2.7, it worked beyond my expectation, the result wascall soamapicall soamapi========================================----class Client--------class ConnectionSecurityCallback--------class DefaultSecurityCallback----pySecCallback is not an instance of ConnectionSecurityCallback!========================================I found that soamapi was imported twice, and I investigated this is related to absolute&relative import way. 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.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.But there are two Pre-conditions:1) Should not modify testP.py;2) Should be ran on both python2.4 and 2.7.I don't know how to fix it. Is there any official way about how to porting this scenario or better idea?Thanks,Vatel
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/brett%40python.org