Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68567

[Python-Dev] Issues about relative& absolute import way for Portingfrom python2.4 to python2.7

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <wtz_wh@foxmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'package,': 0.03; 'static': 0.04; 'subject:Python': 0.06; 'modify': 0.07; '#include': 0.09; 'feature.': 0.09; 'idea?': 0.09; 'imported': 0.09; 'def': 0.12; 'wrote': 0.14; '__future__': 0.16; '__init__.py': 0.16; "module's": 0.16; 'porting': 0.16; 'subject:Dev': 0.16; 'subject:import': 0.16; 'subject:python2.7': 0.16; 'sys.path': 0.16; 'tuple': 0.16; 'fix': 0.17; 'thanks,': 0.17; 'module': 0.19; 'subject:] ': 0.20; 'import': 0.22; 'print': 0.22; 'module,': 0.24; 'skip:{ 20': 0.24; 'source': 0.25; 'skip:" 40': 0.26; 'this:': 0.26; 'pass': 0.26; 'skip:" 20': 0.27; 'correct': 0.29; 'skip:p 30': 0.29; 'related': 0.29; 'absolute': 0.30; 'null;': 0.31; 'python2.7': 0.31; 'relative;': 0.31; 'skip:= 40': 0.31; 'skip:d 70': 0.31; 'skip:s 70': 0.31; 'void': 0.31; 'file': 0.32; 'class': 0.32; 'skip:c 30': 0.32; 'worked': 0.33; 'to:name:python- list': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'skip:s 30': 0.35; 'beyond': 0.35; 'but': 0.35; 'there': 0.35; 'skip:" 50': 0.36; 'should': 0.36; 'two': 0.37; 'skip:& 10': 0.38; 'to:addr :python-list': 0.38; 'skip:& 20': 0.39; 'subject:[': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'name': 0.63; 'skip:n 10': 0.64; 'sample': 0.67; 'export': 0.74; '2.7.': 0.84; 'investigated': 0.84; 'message-id:@qq.com': 0.84; 'received:qq.com': 0.84; 'subject:& ': 0.84; 'x-mailer:qqmail 2.x': 0.84
X-QQ-FEAT QbRhzDPP8Q5r7veQR8BNqKSWMOpcWZMxTcMRBXn2GvyJ3Bi3zAcfSRipr2O94 5r65+pKgAKMyuaUvHXp4WDZTkvFwxoujTxuP8OSGR8tx63aMLRkO8APjhcXDfdriKnRN7F0 LrWV4TkdiURDpmKURQ==
X-QQ-SSF 000000000000001000000000000000Z
X-HAS-ATTACH no
X-QQ-BUSINESS-ORIGIN 2
X-Originating-IP 202.108.26.25
X-QQ-STYLE
X-QQ-mid webmail404t1395294701t613410
From "北冰洋" <wtz_wh@foxmail.com>
To "python-list" <python-list@python.org>
Subject [Python-Dev] Issues about relative& absolute import way for Portingfrom python2.4 to python2.7
Mime-Version 1.0
Content-Type multipart/alternative; boundary="----=_NextPart_532A81ED_08D30A10_1EFA1B3F"
Content-Transfer-Encoding 8Bit
Date Thu, 20 Mar 2014 13:51:41 +0800
X-Priority 3
X-QQ-MIME TCMime 1.0 by Tencent
X-Mailer QQMail 2.x
X-QQ-Mailer QQMail 2.x
X-QQ-SENDSIZE 520
X-QQ-Bgrelay 1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8288.1395294714.18130.python-list@python.org> (permalink)
Lines 228
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1395294714 news.xs4all.nl 2893 [2001:888:2000:d::a6]:39212
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68567

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

[Python-Dev] Issues about relative& absolute import way for Portingfrom python2.4 to python2.7 "北冰洋" <wtz_wh@foxmail.com> - 2014-03-20 13:51 +0800

csiph-web