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


Groups > comp.lang.python > #80195

Re: Python extension using a C library with one 'hello' function

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jason.swails@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'error:': 0.07; 'importerror:': 0.07; 'incompatible': 0.07; 'init': 0.07; 'received:198': 0.07; "subject:' ": 0.07; 'setup.py': 0.09; 'subject:using': 0.09; 'undefined': 0.09; 'url:github': 0.09; 'python': 0.11; 'accepting': 0.14; 'changes': 0.15; '(note': 0.16; '-o2': 0.16; 'build_ext': 0.16; 'sentinel': 0.16; 'subject:library': 0.16; 'wrote:': 0.18; 'module': 0.19; 'have:': 0.19; '>>>': 0.22; 'code,': 0.22; 'import': 0.22; 'creating': 0.23; 'integer': 0.24; 'pointer': 0.24; '(or': 0.24; "i've": 0.25; 'define': 0.26; 'extension': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'array': 0.29; 'character': 0.29; "doesn't": 0.30; 'work.': 0.31; '"",': 0.31; 'constant': 0.31; 'fixing': 0.31; 'gcc': 0.31; 'file': 0.32; 'linux': 0.33; 'url:python': 0.33; 'running': 0.33; '(most': 0.33; 'computer.': 0.33; 'skip:b 30': 0.33; 'sources': 0.33; 'subject:with': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'building': 0.35; 'there': 0.35; 'acceptable': 0.36; 'subject:one': 0.36; 'method': 0.36; 'too': 0.37; 'skip:- 20': 0.37; 'list': 0.37; 'message- id:@gmail.com': 0.38; 'problems': 0.38; 'jason': 0.38; 'skip:. 20': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'recent': 0.39; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'up,': 0.60; 'hope': 0.61; 'subject:hello': 0.83; 'url:master': 0.84; 'cast': 0.91; 'do:': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:subject:from:to:date:in-reply-to:references:content-type :mime-version:content-transfer-encoding; bh=s2vX6cMVzc5nMcpXsaf73KWTjn/RbfDm23F3JaFzb74=; b=iwew1/m/V7jXP/V4hhG9zdGsA0bALTtBUEhVWGWT3A3WHYmNbMRZEECE5OlUw8RPtJ XyG+7paNI6SESXgKjF12a308xgb3DffuwxkmUFUF7Zdp9e6fFPAq03tPDwQP8E78D41h G9+pTuzH5Q5UNSmotDmpUw6l5vwKfRyL4952dh0R0SlY16rzGeeZy+qXuQ5hPwTuN/nO hSlyfBx+4HaZSrrdJ5miDTFxeYBE+ZJwS5Gf3Kz5+x76rTNw+NIV0ryyjfdApYiU0sgD 8vXrWW85VLM/IX3FsNbWiRweHYg/V68ffnCLZUc6fpHJvZJmKXHvoYQTn4wkLVRGRQQa pSJw==
X-Received by 10.229.107.136 with SMTP id b8mr76847694qcp.6.1415114999949; Tue, 04 Nov 2014 07:29:59 -0800 (PST)
Subject Re: Python extension using a C library with one 'hello' function
From Jason Swails <jason.swails@gmail.com>
To python-list@python.org
Date Tue, 04 Nov 2014 10:30:00 -0500
In-Reply-To <m3ab5o$kuo$1@dont-email.me>
References <m3ab5o$kuo$1@dont-email.me>
Content-Type text/plain; charset="UTF-8"
X-Mailer Evolution 3.12.4
Mime-Version 1.0
Content-Transfer-Encoding 7bit
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.15452.1415115002.18130.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415115002 news.xs4all.nl 2881 [2001:888:2000:d::a6]:39592
X-Complaints-To abuse@xs4all.nl
X-Received-Body-CRC 2478477612
X-Received-Bytes 6589
Xref csiph.com comp.lang.python:80195

Show key headers only | View raw


On Tue, 2014-11-04 at 16:22 +0630, Veek M wrote:
> https://github.com/Veek/Python/tree/master/junk/hello
> doesn't work.
> I have:
> hello.c which contains: int hello(void);
> hello.h
> 
> To wrap that up, i have:
> hello.py -> _hello (c extension) -> pyhello.c -> method py_hello()
> 
> People using this will do:
> python3.2>> import hello
> python3.2>> hello.hello()
> 
> It doesn't compile/work. 
> 
> deathstar> python setup.py build_ext --inplace
> running build_ext
> building '_hello' extension
> creating build
> creating build/temp.linux-x86_64-3.2
> gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -
> D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -
> Werror=format-security -fPIC -I/usr/include/python3.2mu -c pyhello.c -o 
> build/temp.linux-x86_64-3.2/pyhello.o
> pyhello.c:15:6: warning: character constant too long for its type [enabled 
> by default]
> pyhello.c:15:5: warning: initialization makes pointer from integer without a 
> cast [enabled by default]
> pyhello.c:15:5: warning: (near initialization for 'hellomethods[0].ml_name') 
> [enabled by default]
> pyhello.c:15:5: warning: initialization from incompatible pointer type 
> [enabled by default]
> pyhello.c:15:5: warning: (near initialization for 'hellomethods[0].ml_meth') 
> [enabled by default]
> pyhello.c:15:5: warning: initialization makes integer from pointer without a 
> cast [enabled by default]
> pyhello.c:15:5: warning: (near initialization for 
> 'hellomethods[0].ml_flags') [enabled by default]
> gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro 
> build/temp.linux-x86_64-3.2/pyhello.o -o 
> /root/github/junk/hello/_hello.cpython-32mu.so
> 
> 
> >>> import hello
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "hello.py", line 1, in <module>
>     from _hello import *
> ImportError: ./_hello.cpython-32mu.so: undefined symbol: hello

When I try your code, I get the error:

ImportError: dynamic module does not define init function (PyInit__hello)

There were a couple other problems as well.  Like 'hello' in
hellomethods instead of "hello" (note the double quotes).  Also, NULL is
no longer acceptable as a METH_XXXARGS replacement, you need to set it
to METH_NOARGS (or MET_VARARGS if you plan on accepting arguments).

I've found that you also need a NULL sentinel in the hellomethods array
to avoid segfaults on my Linux box.

After fixing these problems, you still need to add "hello.c" to the list
of sources in setup.py to make sure that module is built.

I've submitted a PR to your github repo showing you the changes
necessary to get your module working on my computer.

Hope this helps,
Jason

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


Thread

Python extension using a C library with one 'hello' function Veek M <vek.m1234@gmail.com> - 2014-11-04 16:22 +0630
  Re: Python extension using a C library with one 'hello' function Chris Angelico <rosuav@gmail.com> - 2014-11-05 01:57 +1100
  Re: Python extension using a C library with one 'hello' function Søren <sd@syntonetic.com> - 2014-11-04 13:11 +0100
    Re: Python extension using a C library with one 'hello' function Veek M <vek.m1234@gmail.com> - 2014-11-04 21:46 +0630
  Re: Python extension using a C library with one 'hello' function Jason Swails <jason.swails@gmail.com> - 2014-11-04 10:30 -0500
    Re: Python extension using a C library with one 'hello' function Veek M <vek.m1234@gmail.com> - 2014-11-04 21:45 +0630
      Re: Python extension using a C library with one 'hello' function Jason Swails <jason.swails@gmail.com> - 2014-11-04 11:28 -0500
        Re: Python extension using a C library with one 'hello' function Veek M <vek.m1234@gmail.com> - 2014-11-04 22:13 +0630
        Re: Python extension using a C library with one 'hello' function Veek M <vek.m1234@gmail.com> - 2014-11-04 22:39 +0630
          Re: Python extension using a C library with one 'hello' function Jason Swails <jason.swails@gmail.com> - 2014-11-04 12:28 -0500
    Re: Python extension using a C library with one 'hello' function Veek M <vek.m1234@gmail.com> - 2014-11-04 23:03 +0630
      Re: Python extension using a C library with one 'hello' function Jason Swails <jason.swails@gmail.com> - 2014-11-04 13:28 -0500

csiph-web