Skip to content

Commit

Permalink
Support pythonapi in Python 3.
Browse files Browse the repository at this point in the history
ref:9c5d84662565c76ac32d466bdee3159479d23a61
  • Loading branch information
orivej committed Oct 26, 2018
1 parent 649d8b0 commit f13034f
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions contrib/tools/python/src/Lib/ctypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from _ctypes import __version__ as _ctypes_version
from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
from _ctypes import ArgumentError
from .util import find_library

from struct import calcsize as _calcsize

Expand Down Expand Up @@ -467,8 +468,7 @@ def LoadLibrary(self, name):
try:
pythonapi = PyDLL(None)
except OSError:
from library.python.pythonapi import PythonAPI
pythonapi = PythonAPI()
pythonapi = PyDLL(find_library('python'))


if _os.name in ("nt", "ce"):
Expand Down
6 changes: 3 additions & 3 deletions contrib/tools/python/src/Lib/ctypes/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ def find_library(name):

try:
from library.python.symbols.module import find_library as _find_library

except ImportError:
pass
else:
_next_find_library = find_library

def find_library(name):
return _find_library(name, _next_find_library)
except ImportError:
pass

################################################################
# test code
Expand Down
19 changes: 16 additions & 3 deletions contrib/tools/python3/src/Lib/ctypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from _ctypes import __version__ as _ctypes_version
from _ctypes import RTLD_LOCAL, RTLD_GLOBAL
from _ctypes import ArgumentError
from .util import find_library

from struct import calcsize as _calcsize

Expand Down Expand Up @@ -345,7 +346,12 @@ class _FuncPtr(_CFuncPtr):
self._FuncPtr = _FuncPtr

if handle is None:
self._handle = _dlopen(self._name, mode)
if isinstance(self._name, dict):
self._builtin = self._name['symbols']
self._name = self._name['name']
self._handle = 0
else:
self._handle = _dlopen(self._name, mode)
else:
self._handle = handle

Expand All @@ -363,7 +369,11 @@ def __getattr__(self, name):
return func

def __getitem__(self, name_or_ordinal):
func = self._FuncPtr((name_or_ordinal, self))
if self._builtin:
func = self._FuncPtr(self._builtin[name_or_ordinal])
else:
func = self._FuncPtr((name_or_ordinal, self))

if not isinstance(name_or_ordinal, int):
func.__name__ = name_or_ordinal
return func
Expand Down Expand Up @@ -433,7 +443,10 @@ def LoadLibrary(self, name):
elif _sys.platform == "cygwin":
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
else:
pythonapi = PyDLL(None)
try:
pythonapi = PyDLL(None)
except OSError:
pythonapi = PyDLL(find_library('python'))


if _os.name == "nt":
Expand Down
10 changes: 10 additions & 0 deletions contrib/tools/python3/src/Lib/ctypes/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ def find_library(name):
return _findSoname_ldconfig(name) or \
_get_soname(_findLib_gcc(name) or _findLib_ld(name))

try:
from library.python.symbols.module import find_library as _find_library
except ImportError:
pass
else:
_next_find_library = find_library

def find_library(name):
return _find_library(name, _next_find_library)

################################################################
# test code

Expand Down
10 changes: 5 additions & 5 deletions library/python/ctypes/syms.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ static void DictSetStringPtr(PyObject* dict, const char* name, void* value) {
extern void SYM_NAME(); \
SYM(SYM_NAME)

#define END_SYMS() \
if (!PyObject_SetAttrString(m, "syms", d)) \
m = NULL; \
Py_DECREF(d); \
return m; \
#define END_SYMS() \
if (PyObject_SetAttrString(m, "syms", d)) \
m = NULL; \
Py_DECREF(d); \
return m; \
}
2 changes: 1 addition & 1 deletion library/python/ctypes/ya.make
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PY_LIBRARY()
PY23_LIBRARY()



Expand Down
13 changes: 8 additions & 5 deletions library/python/runtime_py3/main/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ LIBRARY()

PEERDIR(
library/python/runtime_py3
library/python/symbols/module
library/python/symbols/libc
library/python/symbols/uuid
)

#IF (MUSL)
# PEERDIR(
# library/python/pythonapi
# )
#ENDIF()
IF (MUSL)
PEERDIR(
library/python/pythonapi
)
ENDIF()

USE_PYTHON3()

Expand Down
4 changes: 2 additions & 2 deletions library/python/symbols/module/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ using namespace NPrivate;
#undef SYM
#undef ESYM

#include <util/generic/string.h>

#include "Python.h"

#include <util/generic/string.h>

extern "C" {
#include <library/python/ctypes/syms.h>
}
Expand Down
2 changes: 1 addition & 1 deletion library/python/symbols/module/ya.make
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PY_LIBRARY()
PY23_LIBRARY()



Expand Down

0 comments on commit f13034f

Please sign in to comment.