[Python-Dev] Issues about relative& absolute import way for Porting from python2.4 to python2.7 (original) (raw)

北冰洋 wtz_wh at foxmail.com
Wed Mar 19 02:50:50 CET 2014


Dear,

I just wrote a sample like this: testPy/ init.py client.py SoamFactory.c SoamFactory.so soamapi.py sample/testP.py export PYTHONPATH=$(TEST_LOCATION):$(TEST_LOCATION)/testPy Here's the source codes: init.py: import client client.py import soamapi class 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.py import SoamFactory

class ConnectionSecurityCallback(object):
def init(self): print "----class ConnectionSecurityCallback----"

def del(self): pass

def 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): pass

def del(self): pass

def 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.py import testPy

print "========================================" submitter = testPy.client.Client() submitter.create_session() print "========================================"

When I ran it on python2.4, it worked well, and the result was call soamapi after 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 was call soamapi call 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20140319/90f33b25/attachment.html>



More information about the Python-Dev mailing list