Skip to content

Commit 934e5ec

Browse files
authored
setup.py Use ctypes To Retrieve Core Version from tiledb_version (#1191)
* Use `ctypes` To Retrieve Core Version from `tiledb_version` * Replaces parsing the `tiledb/tiledh_version.h` * Free the CDLL on Windows * Define `FreeLibrary`'s Arg Type * Microsoft uses a 32-bit `long`. Passing a pointer as a Python integer (64-bit) may either throw an `OverflowError` or more fatally segfault the process, so it is necessary to define the argtype
1 parent e8e0f0d commit 934e5ec

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# In Progress
2+
3+
## Improvements
4+
* `setup.py` retrieves core version by using `ctypes` to call `tiledb_version` rather than parsing `tiledb_version.h` [#1191](https://github.com/TileDB-Inc/TileDB-Py/pull/1191)
5+
16
# TileDB-Py 0.16.1 Release Notes
27

38
## TileDB Embedded updates:

setup.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import ctypes
1+
from ctypes import *
22
import io
33
import glob
44
import multiprocessing
55
import os
6-
import platform
76
import shutil
87
import subprocess
98
import sys
109
import zipfile
11-
from sysconfig import get_config_var
1210
from urllib.error import URLError
1311
from urllib.request import urlopen
1412

@@ -93,14 +91,12 @@ def _libtiledb_exists(library_dirs):
9391
)
9492
)
9593
# If no explicit path is given check to see if TileDB is globally installed.
96-
import ctypes
97-
9894
lib_name = names[0]
9995

10096
try:
10197
# note: this is a relative path on linux
10298
# https://bugs.python.org/issue21042
103-
ctypes.CDLL(lib_name)
99+
CDLL(lib_name)
104100
return lib_name
105101
except:
106102
pass
@@ -290,7 +286,7 @@ def find_or_install_libtiledb(setuptools_cmd):
290286
if wheel_build and is_windows() and lib_exists:
291287
do_install = True
292288

293-
print("prefix_dir: ", main_ext)
289+
print("prefix_dir: ", prefix_dir or None)
294290
print("do_install: ", do_install)
295291

296292
if do_install:
@@ -356,7 +352,7 @@ def do_copy(src, dest):
356352
test_path = os.path.join(TILEDB_PKG_DIR, shared_obj)
357353
# should only ever be 1, not sure why libtiledb_library_names -> List
358354
try:
359-
ctypes.CDLL(test_path)
355+
CDLL(test_path)
360356
except:
361357
print("\n-------------------")
362358
print("Failed to load shared library: {}".format(test_path))
@@ -383,20 +379,35 @@ def do_copy(src, dest):
383379
print("-------------------\n")
384380
setuptools_cmd.distribution.package_data.update({"tiledb": libtiledb_objects})
385381

386-
version_header = os.path.join(prefix_dir, "include", "tiledb", "tiledb_version.h")
387-
with open(version_header) as header:
388-
lines = list(header)[-3:]
389-
major, minor, patch = [int(l.split()[-1]) for l in lines]
382+
libtiledbso = CDLL(
383+
lib_exists or os.path.join(dest_dir, libtiledb_library_names()[0])
384+
)
385+
386+
libtiledbso.tiledb_version.argtypes = [
387+
POINTER(c_int),
388+
POINTER(c_int),
389+
POINTER(c_int),
390+
]
391+
major, minor, patch = c_int(), c_int(), c_int()
392+
libtiledbso.tiledb_version(major, minor, patch)
393+
390394
ext_attr_update(
391395
"cython_compile_time_env",
392396
{
393397
"TILEDBPY_MODULAR": TILEDBPY_MODULAR,
394-
"LIBTILEDB_VERSION_MAJOR": major,
395-
"LIBTILEDB_VERSION_MINOR": minor,
396-
"LIBTILEDB_VERSION_PATCH": patch,
398+
"LIBTILEDB_VERSION_MAJOR": major.value,
399+
"LIBTILEDB_VERSION_MINOR": minor.value,
400+
"LIBTILEDB_VERSION_PATCH": patch.value,
397401
},
398402
)
399403

404+
if is_windows():
405+
from ctypes.wintypes import HMODULE
406+
407+
kernel32 = WinDLL("kernel32", use_last_error=True)
408+
kernel32.FreeLibrary.argtypes = [HMODULE]
409+
kernel32.FreeLibrary(libtiledbso._handle)
410+
400411

401412
class LazyCommandClass(dict):
402413
"""

0 commit comments

Comments
 (0)