8000 BUG: deal with shared lib extension in a single place. Fix ctypes.loa… · numpy/numpy@9a287ba · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9a287ba

Browse files
author
rgommers
committed
BUG: deal with shared lib extension in a single place. Fix ctypes.load_library.
This is related to PEP 3149, tags in shared library extensions. Only applies to Linux (for now). See also #1749.
1 parent 8d451bc commit 9a287ba

File tree

5 files changed

+62
-31
lines changed

5 files changed

+62
-31
lines changed

numpy/ctypeslib.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import sys, os
5656
from numpy import integer, ndarray, dtype as _dtype, deprecate, array
5757
from numpy.core.multiarray import _flagdict, flagsobj
58+
from numpy.distutils.misc_util import get_shared_lib_extension
5859

5960
try:
6061
import ctypes
@@ -97,7 +98,15 @@ def load_library(libname, loader_path):
9798
# Try to load library with platform-specific name, otherwise
9899
# default to libname.[so|pyd]. Sometimes, these files are built
99100
# erroneously on non-linux platforms.
100-
libname_ext = ['%s.so' % libname, '%s.pyd' % libname]
101+
so_ext = get_shared_lib_extension()
102+
libname_ext = [libname + so_ext]
103+
if sys.version[:3] >= '3.2':
104+
# For Python >= 3.2 a tag may be added to lib extension
105+
# (platform dependent). If we find such a tag, try both with
106+
# and without it.
107+
so_ext2 = get_shared_lib_extension(is_python_ext=True)
108+
if not so_ext2 == so_ext:
109+
libname_ext.insert(0, libname + so_ext2)
101110
if sys.platform == 'win32':
102111
libname_ext.insert(0, '%s.dll' % libname)
103112
elif sys.platform == 'darwin':

numpy/distutils/fcompiler/__init__.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@
2727

2828
from numpy.compat import open_latin1
2929

30-
from distutils.sysconfig import get_config_var, get_config_vars, get_python_lib
30+
from distutils.sysconfig import get_python_lib
3131
from distutils.fancy_getopt import FancyGetopt
3232
from distutils.errors import DistutilsModuleError, \
3333
DistutilsExecError, CompileError, LinkError, DistutilsPlatformError
3434
from distutils.util import split_quoted, strtobool
3535

3636
from numpy.distutils.ccompiler import CCompiler, gen_lib_options
3737
from numpy.distutils import log
38-
from numpy.distutils.misc_util import is_string, all_strings, is_sequence, make_temp_file
38+
from numpy.distutils.misc_util import is_string, all_strings, is_sequence, \
39+
make_temp_file, get_shared_lib_extension
3940
from numpy.distutils.environment import EnvironmentConfig
4041
from numpy.distutils.exec_command import find_executable
4142
from numpy.distutils.compat import get_exception
@@ -195,10 +196,8 @@ class FCompiler(CCompiler):
195196

196197
src_extensions = ['.for','.ftn','.f77','.f','.f90','.f95','.F','.F90']
197198
obj_extension = ".o"
198-
shared_lib_extension = get_config_var('SO') # or .dll
199-
# fix long extension for Python >=3.2, see PEP 3149.
200-
if 'SOABI' in get_config_vars():
201-
shared_lib_extension = shared_lib_extension.replace('.'+get_config_var('SOABI'), '', 1)
199+
200+
shared_lib_extension = get_shared_lib_extension()
202201
static_lib_extension = ".a" # or .lib
203202
static_lib_format = "lib%s%s" # or %s%s
204203
shared_lib_format = "%s%s"

numpy/distutils/misc_util.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import subprocess
1010
import shutil
1111

12+
import distutils
1213
from distutils.errors import DistutilsError
1314

1415
try:
@@ -583,6 +584,35 @@ def get_lib_source_files(lib):
583584
filenames.append(d)
584585
return filenames
585586

587+
def get_shared_lib_extension(is_python_ext=False):
588+
"""Return the correct file extension for shared libraries.
589+
590+
Parameters
591+
----------
592+
is_python_ext : bool, optional
593+
Whether the shared library is a Python extension. Default is False.
594+
595+
Returns
596+
-------
597+
so_ext : str
598+
The shared library extension.
599+
600+
Notes
601+
-----
602+
For Python shared libs, `so_ext` will typically be '.so' on Linux and OS X,
603+
and '.pyd' on Windows. For Python >= 3.2 `so_ext` has a tag prepended on
604+
POSIX systems according to PEP 3149. For Python 3.2 this is implemented on
605+
Linux, but not on OS X.
606+
607+
"""
608+
so_ext = distutils.sysconfig.get_config_var('SO') or ''
609+
# fix long extension for Python >=3.2, see PEP 3149.
610+
if (not is_python_ext) and 'SOABI' in distutils.sysconfig.get_config_vars():
611+
# Does nothing unless SOABI config var exists
612+
so_ext = so_ext.replace('.' + distutils.sysconfig.get_config_var('SOABI'), '', 1)
613+
614+
return so_ext
615+
586616
def get_data_files(data):
587617
if is_string(data):
588618
return [data]
@@ -772,7 +802,7 @@ def __init__(self,
772802
def todict(self):
773803
"""
774804
Return a dictionary compatible with the keyword arguments of distutils
775-
setup function.
805+
setup function.
776806
777807
Examples
778808
--------
@@ -947,8 +977,8 @@ def get_subpackage(self,subpackage_name,
947977
def add_subpackage(self,subpackage_name,
948978
subpackage_path=None,
949979
standalone = False):
950-
"""Add a sub-package to the current Configuration instance.
951-
980+
"""Add a sub-package to the current Configuration instance.
981+
952982
This is useful in a setup.py script for adding sub-packages to a
953983
package.
954984
@@ -994,7 +1024,7 @@ def add_data_dir(self,data_path):
9941024
installed (and distributed). The data_path can be either a relative
9951025
path-name, or an absolute path-name, or a 2-tuple where the first
9961026
argument shows where in the install directory the data directory
997-
should be installed to.
1027+
should be installed to.
9981028
9991029
Parameters
10001030
----------
@@ -1389,7 +1419,7 @@ def add_extension(self,name,sources,**kw):
13891419
Notes
13901420
-----
13911421
The self.paths(...) method is applied to all lists that may contain
1392-
paths.
1422+
paths.
13931423
"""
13941424
ext_args = copy.copy(kw)
13951425
ext_args['name'] = dot_join(self.name,name)
@@ -1863,7 +1893,7 @@ def _get_hg_revision(self,path):
18631893
return revision
18641894
branch_fn = njoin(path,'.hg','branch')
18651895
branch_cache_fn = njoin(path,'.hg','branch.cache')
1866-
1896+
18671897
if os.path.isfile(branch_fn):
18681898
branch0 = None
18691899
f = open(branch_fn)
@@ -1889,8 +1919,8 @@ def get_version(self, version_file=None, version_variable=None):
18891919
"""Try to get version string of a package.
18901920
18911921
Return a version string of the current package or None if the version
1892-
information could not be detected.
1893-
1922+
information could not be detected.
1923+
18941924
Notes
18951925
-----
18961926
This method scans files named
@@ -1956,8 +1986,8 @@ def get_version(self, version_file=None, version_variable=None):
19561986

19571987
def make_svn_version_py(self, delete=True):
19581988
"""Appends a data function to the data_files list that will generate
1959-
__svn_version__.py file to the current package directory.
1960-
1989+
__svn_version__.py file to the current package directory.
1990+
19611991
Generate package __svn_version__.py file from SVN revision number,
19621992
it will be removed after python exits but will be available
19631993
when sdist, etc commands are executed.
@@ -1999,8 +2029,8 @@ def rm_file(f=target,p=self.info):
19992029

20002030
def make_hg_version_py(self, delete=True):
20012031
"""Appends a data function to the data_files list that will generate
2002-
__hg_version__.py file to the current package directory.
2003-
2032+
__hg_version__.py file to the current package directory.
2033+
20042034
Generate package __hg_version__.py file from Mercurial revision,
20052035
it will be removed after python exits but will be available
20062036
when sdist, etc commands are executed.

numpy/distutils/system_info.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@
128128

129129
from numpy.distutils.exec_command import \
130130
find_executable, exec_command, get_pythonexe
131-
from numpy.distutils.misc_util import is_sequence, is_string
131+
from numpy.distutils.misc_util import is_sequence, is_string, \
132+
get_shared_lib_extension
132133
from numpy.distutils.command.config import config as cmd_config
133134
from numpy.distutils.compat import get_exception
134135

@@ -210,11 +211,7 @@ def libpaths(paths,bits):
210211
default_include_dirs = filter(os.path.isdir, default_include_dirs)
211212
default_src_dirs = filter(os.path.isdir, default_src_dirs)
212213

213-
so_ext = distutils.sysconfig.get_config_vars('SO')[0] or ''
214-
# fix long extension for Python >=3.2, see PEP 3149.
215-
if 'SOABI' in distutils.sysconfig.get_config_vars():
216-
so_ext = so_ext.replace('.'+distutils.sysconfig.get_config_var('SOABI'), '', 1)
217-
214+
so_ext = get_shared_lib_extension()
218215

219216
def get_standard_file(fname):
220217
"""Returns a list of files named 'fname' from

numpy/tests/test_ctypeslib.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
from numpy.ctypeslib import ndpointer, load_library
5+
from numpy.distutils.misc_util import get_shared_lib_extension
56
from numpy.testing import *
67

78
try:
@@ -29,12 +30,7 @@ def test_basic2(self):
2930
(including extension) does not work."""
3031
try:
3132
try:
32-
from distutils import sysconfig
33-
so = sysconfig.get_config_var('SO')
34-
# fix long extension for Python >=3.2, see PEP 3149.
35-
if 'SOABI' in sysconfig.get_config_vars():
36-
so = so.replace('.'+sysconfig.get_config_var('SOABI'), '', 1)
37-
33+
so = get_shared_lib_extension(is_python_ext=True)
3834
cdll = load_library('multiarray%s' % so,
3935
np.core.multiarray.__file__)
4036
except ImportError:

0 commit comments

Comments
 (0)
0