Skip to content

Commit f2ebd79

Browse files
authored
Rename native_internal to librt.internal (#20014)
Also adjust build/test logic slightly to prepare for more modules in `librt`. This PR should fix #20006
1 parent 139071c commit f2ebd79

19 files changed

+99
-86
lines changed

mypy-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ typing_extensions>=4.6.0
44
mypy_extensions>=1.0.0
55
pathspec>=0.9.0
66
tomli>=1.1.0; python_version<'3.11'
7-
librt==0.1.1
7+
librt>=0.2.1

mypy/cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from collections.abc import Sequence
44
from typing import Final
55

6-
from mypy_extensions import u8
7-
from native_internal import (
6+
from librt.internal import (
87
Buffer as Buffer,
98
read_bool as read_bool,
109
read_float as read_float,
@@ -17,6 +16,7 @@
1716
write_str as write_str,
1817
write_tag as write_tag,
1918
)
19+
from mypy_extensions import u8
2020

2121
# Always use this type alias to refer to type tags.
2222
Tag = u8
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "0.1.*"
1+
version = "0.2.*"

mypy/typeshed/stubs/librt/librt/__init__.pyi

Whitespace-only changes.
File renamed without changes.

mypyc/build.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
from mypyc.namegen import exported_name
4343
from mypyc.options import CompilerOptions
4444

45+
LIBRT_MODULES = [("librt.internal", "librt_internal.c")]
46+
4547
try:
4648
# Import setuptools so that it monkey-patch overrides distutils
4749
import setuptools
@@ -492,8 +494,8 @@ def mypycify(
492494
strict_dunder_typing: bool = False,
493495
group_name: str | None = None,
494496
log_trace: bool = False,
495-
depends_on_native_internal: bool = False,
496-
install_native_libs: bool = False,
497+
depends_on_librt_internal: bool = False,
498+
install_librt: bool = False,
497499
) -> list[Extension]:
498500
"""Main entry point to building using mypyc.
499501
@@ -544,11 +546,11 @@ def mypycify(
544546
mypyc_trace.txt (derived from executed operations). This is
545547
useful for performance analysis, such as analyzing which
546548
primitive ops are used the most and on which lines.
547-
depends_on_native_internal: This is True only for mypy itself.
548-
install_native_libs: If True, also build the native extension modules. Normally,
549-
those are build and published on PyPI separately, but during
550-
tests, we want to use their development versions (i.e. from
551-
current commit).
549+
depends_on_librt_internal: This is True only for mypy itself.
550+
install_librt: If True, also build the librt extension modules. Normally,
551+
those are build and published on PyPI separately, but during
552+
tests, we want to use their development versions (i.e. from
553+
current commit).
552554
"""
553555

554556
# Figure out our configuration
@@ -562,7 +564,7 @@ def mypycify(
562564
strict_dunder_typing=strict_dunder_typing,
563565
group_name=group_name,
564566
log_trace=log_trace,
565-
depends_on_native_internal=depends_on_native_internal,
567+
depends_on_librt_internal=depends_on_librt_internal,
566568
)
567569

568570
# Generate all the actual important C code
@@ -661,21 +663,25 @@ def mypycify(
661663
build_single_module(group_sources, cfilenames + shared_cfilenames, cflags)
662664
)
663665

664-
if install_native_libs:
665-
for name in ["native_internal.c"] + RUNTIME_C_FILES:
666+
if install_librt:
667+
os.makedirs("librt", exist_ok=True)
668+
for name in RUNTIME_C_FILES:
666669
rt_file = os.path.join(build_dir, name)
667670
with open(os.path.join(include_dir(), name), encoding="utf-8") as f:
668671
write_file(rt_file, f.read())
669-
extensions.append(
670-
get_extension()(
671-
"native_internal",
672-
sources=[
673-
os.path.join(build_dir, file)
674-
for file in ["native_internal.c"] + RUNTIME_C_FILES
675-
],
676-
include_dirs=[include_dir()],
677-
extra_compile_args=cflags,
672+
for mod, file_name in LIBRT_MODULES:
673+
rt_file = os.path.join(build_dir, file_name)
674+
with open(os.path.join(include_dir(), file_name), encoding="utf-8") as f:
675+
write_file(rt_file, f.read())
676+
extensions.append(
677+
get_extension()(
678+
mod,
679+
sources=[
680+
os.path.join(build_dir, file) for file in [file_name] + RUNTIME_C_FILES
681+
],
682+
include_dirs=[include_dir()],
683+
extra_compile_args=cflags,
684+
)
678685
)
679-
)
680686

681687
return extensions

mypyc/codegen/emitmodule.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,12 @@ def generate_c_for_modules(self) -> list[tuple[str, str]]:
601601
ext_declarations.emit_line(f"#define MYPYC_NATIVE{self.group_suffix}_H")
602602
ext_declarations.emit_line("#include <Python.h>")
603603
ext_declarations.emit_line("#include <CPy.h>")
604-
if self.compiler_options.depends_on_native_internal:
605-
ext_declarations.emit_line("#include <native_internal.h>")
604+
if self.compiler_options.depends_on_librt_internal:
605+
ext_declarations.emit_line("#include <librt_internal.h>")
606606

607607
declarations = Emitter(self.context)
608-
declarations.emit_line(f"#ifndef MYPYC_NATIVE_INTERNAL{self.group_suffix}_H")
609-
declarations.emit_line(f"#define MYPYC_NATIVE_INTERNAL{self.group_suffix}_H")
608+
declarations.emit_line(f"#ifndef MYPYC_LIBRT_INTERNAL{self.group_suffix}_H")
609+
declarations.emit_line(f"#define MYPYC_LIBRT_INTERNAL{self.group_suffix}_H")
610610
declarations.emit_line("#include <Python.h>")
611611
declarations.emit_line("#include <CPy.h>")
612612
declarations.emit_line(f'#include "__native{self.short_group_suffix}.h"')
@@ -1029,8 +1029,8 @@ def emit_module_exec_func(
10291029
declaration = f"int CPyExec_{exported_name(module_name)}(PyObject *module)"
10301030
module_static = self.module_internal_static_name(module_name, emitter)
10311031
emitter.emit_lines(declaration, "{")
1032-
if self.compiler_options.depends_on_native_internal:
1033-
emitter.emit_line("if (import_native_internal() < 0) {")
1032+
if self.compiler_options.depends_on_librt_internal:
1033+
emitter.emit_line("if (import_librt_internal() < 0) {")
10341034
emitter.emit_line("return -1;")
10351035
emitter.emit_line("}")
10361036
emitter.emit_line("PyObject* modname = NULL;")

mypyc/ir/rtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def __hash__(self) -> int:
514514

515515
KNOWN_NATIVE_TYPES: Final = {
516516
name: RPrimitive(name, is_unboxed=False, is_refcounted=True)
517-
for name in ["native_internal.Buffer"]
517+
for name in ["librt.internal.Buffer"]
518518
}
519519

520520

mypyc/lib-rt/native_internal.c renamed to mypyc/lib-rt/librt_internal.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include <Python.h>
33
#include <stdint.h>
44
#include "CPy.h"
5-
#define NATIVE_INTERNAL_MODULE
6-
#include "native_internal.h"
5+
#define LIBRT_INTERNAL_MODULE
6+
#include "librt_internal.h"
77

88
#define START_SIZE 512
99
#define MAX_SHORT_INT_TAGGED (255 << 1)
@@ -558,7 +558,7 @@ write_tag(PyObject *self, PyObject *const *args, size_t nargs, PyObject *kwnames
558558
return Py_None;
559559
}
560560

561-
static PyMethodDef native_internal_module_methods[] = {
561+
static PyMethodDef librt_internal_module_methods[] = {
562562
{"write_bool", (PyCFunction)write_bool, METH_FASTCALL | METH_KEYWORDS, PyDoc_STR("write a bool")},
563563
{"read_bool", (PyCFunction)read_bool, METH_FASTCALL | METH_KEYWORDS, PyDoc_STR("read a bool")},
564564
{"write_str", (PyCFunction)write_str, METH_FASTCALL | METH_KEYWORDS, PyDoc_STR("write a string")},
@@ -574,11 +574,11 @@ static PyMethodDef native_internal_module_methods[] = {
574574

575575
static int
576576
NativeInternal_ABI_Version(void) {
577-
return NATIVE_INTERNAL_ABI_VERSION;
577+
return LIBRT_INTERNAL_ABI_VERSION;
578578
}
579579

580580
static int
581-
native_internal_module_exec(PyObject *m)
581+
librt_internal_module_exec(PyObject *m)
582582
{
583583
if (PyType_Ready(&BufferType) < 0) {
584584
return -1;
@@ -604,32 +604,32 @@ native_internal_module_exec(PyObject *m)
604604
(void *)read_tag_internal,
605605
(void *)NativeInternal_ABI_Version,
606606
};
607-
PyObject *c_api_object = PyCapsule_New((void *)NativeInternal_API, "native_internal._C_API", NULL);
607+
PyObject *c_api_object = PyCapsule_New((void *)NativeInternal_API, "librt.internal._C_API", NULL);
608608
if (PyModule_Add(m, "_C_API", c_api_object) < 0) {
609609
return -1;
610610
}
611611
return 0;
612612
}
613613

614-
static PyModuleDef_Slot native_internal_module_slots[] = {
615-
{Py_mod_exec, native_internal_module_exec},
614+
static PyModuleDef_Slot librt_internal_module_slots[] = {
615+
{Py_mod_exec, librt_internal_module_exec},
616616
#ifdef Py_MOD_GIL_NOT_USED
617617
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
618618
#endif
619619
{0, NULL}
620620
};
621621

622-
static PyModuleDef native_internal_module = {
622+
static PyModuleDef librt_internal_module = {
623623
.m_base = PyModuleDef_HEAD_INIT,
624-
.m_name = "native_internal",
624+
.m_name = "internal",
625625
.m_doc = "Mypy cache serialization utils",
626626
.m_size = 0,
627-
.m_methods = native_internal_module_methods,
628-
.m_slots = native_internal_module_slots,
627+
.m_methods = librt_internal_module_methods,
628+
.m_slots = librt_internal_module_slots,
629629
};
630630

631631
PyMODINIT_FUNC
632-
PyInit_native_internal(void)
632+
PyInit_internal(void)
633633
{
634-
return PyModuleDef_Init(&native_internal_module);
634+
return PyModuleDef_Init(&librt_internal_module);
635635
}

mypyc/lib-rt/native_internal.h renamed to mypyc/lib-rt/librt_internal.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#ifndef NATIVE_INTERNAL_H
2-
#define NATIVE_INTERNAL_H
1+
#ifndef LIBRT_INTERNAL_H
2+
#define LIBRT_INTERNAL_H
33

4-
#define NATIVE_INTERNAL_ABI_VERSION 0
4+
#define LIBRT_INTERNAL_ABI_VERSION 0
55

6-
#ifdef NATIVE_INTERNAL_MODULE
6+
#ifdef LIBRT_INTERNAL_MODULE
77

88
static PyObject *Buffer_internal(PyObject *source);
99
static PyObject *Buffer_internal_empty(void);
@@ -40,17 +40,21 @@ static void **NativeInternal_API;
4040
#define NativeInternal_ABI_Version (*(int (*)(void)) NativeInternal_API[13])
4141

4242
static int
43-
import_native_internal(void)
43+
import_librt_internal(void)
4444
{
45-
NativeInternal_API = (void **)PyCapsule_Import("native_internal._C_API", 0);
45+
PyObject *mod = PyImport_ImportModule("librt.internal");
46+
if (mod == NULL)
47+
return -1;
48+
Py_DECREF(mod); // we import just for the side effect of making the below work.
49+
NativeInternal_API = (void **)PyCapsule_Import("librt.internal._C_API", 0);
4650
if (NativeInternal_API == NULL)
4751
return -1;
48-
if (NativeInternal_ABI_Version() != NATIVE_INTERNAL_ABI_VERSION) {
49-
PyErr_SetString(PyExc_ValueError, "ABI version conflict for native_internal");
52+
if (NativeInternal_ABI_Version() != LIBRT_INTERNAL_ABI_VERSION) {
53+
PyErr_SetString(PyExc_ValueError, "ABI version conflict for librt.internal");
5054
return -1;
5155
}
5256
return 0;
5357
}
5458

5559
#endif
56-
#endif // NATIVE_INTERNAL_H
60+
#endif // LIBRT_INTERNAL_H

0 commit comments

Comments
 (0)