Skip to content

Commit

Permalink
Merge pull request numpy#28035 from mtsokol/refactor-lib-and-ctypeslib
Browse files Browse the repository at this point in the history
MAINT: Move `lib.format` and `ctypeslib` to submodules/private files
  • Loading branch information
mattip authored Dec 23, 2024
2 parents 70bb9e6 + a3a009c commit 33563d5
Show file tree
Hide file tree
Showing 16 changed files with 1,173 additions and 1,049 deletions.
13 changes: 13 additions & 0 deletions numpy/ctypeslib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ._ctypeslib import (
__all__,
__doc__,
as_array,
as_ctypes,
as_ctypes_type,
ctypes,
c_intp,
load_library,
ndpointer,
_concrete_ndptr,
_ndptr,
)
15 changes: 15 additions & 0 deletions numpy/ctypeslib/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ctypes
from ctypes import c_int64 as _c_intp

from ._ctypeslib import (
__all__ as __all__,
__doc__ as __doc__,
as_array as as_array,
as_ctypes as as_ctypes,
as_ctypes_type as as_ctypes_type,
c_intp as c_intp,
load_library as load_library,
ndpointer as ndpointer,
_concrete_ndptr as _concrete_ndptr,
_ndptr as _ndptr,
)
20 changes: 15 additions & 5 deletions numpy/ctypeslib.py → numpy/ctypeslib/_ctypeslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@

import os
import numpy as np
from numpy._core.multiarray import _flagdict, flagsobj
import numpy._core.multiarray as mu
from numpy._utils import set_module

try:
import ctypes
except ImportError:
ctypes = None

if ctypes is None:
@set_module("numpy.ctypeslib")
def _dummy(*args, **kwds):
"""
Dummy object that raises an ImportError if ctypes is not available.
Expand All @@ -75,7 +77,9 @@ def _dummy(*args, **kwds):
raise ImportError("ctypes is not available.")
load_library = _dummy
as_ctypes = _dummy
as_ctypes_type = _dummy
as_array = _dummy
ndpointer = _dummy
from numpy import intp as c_intp
_ndptr_base = object
else:
Expand All @@ -85,6 +89,7 @@ def _dummy(*args, **kwds):
_ndptr_base = ctypes.c_void_p

# Adapted from Albert Strasheim
@set_module("numpy.ctypeslib")
def load_library(libname, loader_path):
"""
It is possible to load a library using
Expand Down Expand Up @@ -162,7 +167,7 @@ def load_library(libname, loader_path):
def _num_fromflags(flaglist):
num = 0
for val in flaglist:
num += _flagdict[val]
num += mu._flagdict[val]
return num


Expand All @@ -171,7 +176,7 @@ def _num_fromflags(flaglist):
def _flags_fromnum(num):
res = []
for key in _flagnames:
value = _flagdict[key]
value = mu._flagdict[key]
if (num & value):
res.append(key)
return res
Expand Down Expand Up @@ -227,8 +232,10 @@ def contents(self):


# Factory for an array-checking class with from_param defined for
# use with ctypes argtypes mechanism
# use with ctypes argtypes mechanism
_pointer_type_cache = {}

@set_module("numpy.ctypeslib")
def ndpointer(dtype=None, ndim=None, shape=None, flags=None):
"""
Array-checking restype/argtypes.
Expand Down Expand Up @@ -293,7 +300,7 @@ def ndpointer(dtype=None, ndim=None, shape=None, flags=None):
elif isinstance(flags, (int, np.integer)):
num = flags
flags = _flags_fromnum(num)
elif isinstance(flags, flagsobj):
elif isinstance(flags, mu.flagsobj):
num = flags.num
flags = _flags_fromnum(num)
if num is None:
Expand Down Expand Up @@ -453,6 +460,7 @@ def _ctype_from_dtype(dtype):
else:
return _ctype_from_dtype_scalar(dtype)

@set_module("numpy.ctypeslib")
def as_ctypes_type(dtype):
r"""
Convert a dtype into a ctypes type.
Expand Down Expand Up @@ -509,6 +517,7 @@ def as_ctypes_type(dtype):
"""
return _ctype_from_dtype(np.dtype(dtype))

@set_module("numpy.ctypeslib")
def as_array(obj, shape=None):
"""
Create a numpy array from a ctypes array or POINTER.
Expand Down Expand Up @@ -549,6 +558,7 @@ def as_array(obj, shape=None):

return np.asarray(obj)

@set_module("numpy.ctypeslib")
def as_ctypes(obj):
"""
Create and return a ctypes object from a numpy array. Actually
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions numpy/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"""

# Public submodules
# Note: recfunctions and (maybe) format are public too, but not imported
# Note: recfunctions is public, but not imported
from . import array_utils
from . import format
from . import introspect
from . import mixins
from . import npyio
Expand Down Expand Up @@ -44,8 +45,8 @@

__all__ = [
"Arrayterator", "add_docstring", "add_newdoc", "array_utils",
"introspect", "mixins", "NumpyVersion", "npyio", "scimath",
"stride_tricks", "tracemalloc_domain"
"format", "introspect", "mixins", "NumpyVersion", "npyio", "scimath",
"stride_tricks", "tracemalloc_domain",
]

add_newdoc.__module__ = "numpy.lib"
Expand Down
Loading

0 comments on commit 33563d5

Please sign in to comment.