Compare commits

...

7 Commits

Author SHA1 Message Date
Tanisha Vasudeva
b20033888e
Merge 6cc3d8023350163e72b83d49981ad2c4b08d39b4 into 0676b21da2827b827ee7cac89e9b27c4aa3c005e 2026-03-22 15:18:12 +09:00
Jason Matthew Suhari
0676b21da2
fix: save stack checkpoint before pushing args in operator() (#479)
* fix: capture stack checkpoint before pushing args in operator() (#469)

* test: add regression test for operator() python error propagation (#469)
2026-03-20 13:22:25 +08:00
blueloveTH
a2f16e5f1f pre-release 2026-03-18 15:10:24 +08:00
blueloveTH
a69cca59f4 post fix
Update dll.c

Update dll.c
2026-03-18 15:04:26 +08:00
kushagra-1809
7614bdcc4a
Wrong formulas (#470)
The correct formulas for complex trigonometry require
cos(z) = (exp(iz) + exp(-iz)) / 2
sin(z) = (exp(iz) - exp(-iz)) / (2i)
2026-03-18 14:58:23 +08:00
wdskuki
984c0eefcc
Fix C extension module import on Linux (#472)
* Fix C extension module import on Linux

* minor fix

---------

Co-authored-by: wdsmini <wdsmini@wdsmini.local>
Co-authored-by: blueloveTH <blueloveTH@foxmail.com>
2026-03-18 14:55:38 +08:00
tanisha
6cc3d80233 Added readline() 2026-02-28 20:28:04 +05:30
7 changed files with 175 additions and 33 deletions

View File

@ -70,6 +70,7 @@ args_proxy interface<Derived>::operator* () const {
template <typename Derived>
template <return_value_policy policy, typename... Args>
object interface<Derived>::operator() (Args&&... args) const {
py_StackRef p0 = py_peek(0); // checkpoint before pushing so py_clearexc can safely rewind
py_push(ptr());
py_pushnil();
@ -108,7 +109,13 @@ object interface<Derived>::operator() (Args&&... args) const {
(foreach(std::forward<Args>(args)), ...);
raise_call<py_vectorcall>(argc, kwargsc);
if(!py_vectorcall(argc, kwargsc)) {
py_matchexc(tp_Exception);
object e = object::from_ret();
auto what = py_formatexc();
py_clearexc(p0);
throw python_error(what, std::move(e));
}
return object::from_ret();
}

View File

@ -79,3 +79,18 @@ TEST_F(PYBIND11_TEST, exception_cpp_to_python) {
TEST_EXCEPTION(attribute_error, AttributeError);
TEST_EXCEPTION(runtime_error, RuntimeError);
}
// Regression test: operator() must throw python_error instead of crashing when Python raises (#469)
TEST_F(PYBIND11_TEST, operator_call_propagates_python_error) {
py::exec("def f(x):\n raise ValueError('intentional error')");
py::object fn = py::eval("f");
bool caught = false;
try {
fn(py::int_(1));
} catch(py::python_error& e) {
caught = true;
EXPECT_TRUE(e.match(tp_ValueError));
}
EXPECT_TRUE(caught);
}

View File

@ -1530,9 +1530,9 @@ class PocketpyBindings {
ffi.NativeFunction<
ffi.Bool Function(py_Ref self, py_Name name)>>)>();
/// Get the current `function` object on the stack.
/// Get the current `Callable` object on the stack of the most recent vectorcall.
/// Return `NULL` if not available.
/// NOTE: This function should be placed at the beginning of your decl-based bindings.
/// NOTE: This function should be placed at the beginning of your bindings or you will get wrong result.
py_StackRef py_inspect_currentfunction() {
return _py_inspect_currentfunction();
}
@ -3635,6 +3635,22 @@ class PocketpyBindings {
late final _py_newvec3i =
_py_newvec3iPtr.asFunction<void Function(py_OutRef, c11_vec3i)>();
void py_newvec4i(
py_OutRef out,
c11_vec4i arg1,
) {
return _py_newvec4i(
out,
arg1,
);
}
late final _py_newvec4iPtr =
_lookup<ffi.NativeFunction<ffi.Void Function(py_OutRef, c11_vec4i)>>(
'py_newvec4i');
late final _py_newvec4i =
_py_newvec4iPtr.asFunction<void Function(py_OutRef, c11_vec4i)>();
void py_newcolor32(
py_OutRef out,
c11_color32 arg1,
@ -3715,6 +3731,19 @@ class PocketpyBindings {
late final _py_tovec3i =
_py_tovec3iPtr.asFunction<c11_vec3i Function(py_Ref)>();
c11_vec4i py_tovec4i(
py_Ref self,
) {
return _py_tovec4i(
self,
);
}
late final _py_tovec4iPtr =
_lookup<ffi.NativeFunction<c11_vec4i Function(py_Ref)>>('py_tovec4i');
late final _py_tovec4i =
_py_tovec4iPtr.asFunction<c11_vec4i Function(py_Ref)>();
ffi.Pointer<c11_mat3x3> py_tomat3x3(
py_Ref self,
) {
@ -3932,10 +3961,32 @@ final class UnnamedUnion1 extends ffi.Union {
@ffi.Int64()
external int _i64;
@ffi.Double()
external double _f64;
@ffi.Bool()
external bool _bool;
external py_CFunction _cfunc;
external ffi.Pointer<ffi.Void> _obj;
external ffi.Pointer<ffi.Void> _ptr;
@ffi.Array.multi([16])
external ffi.Array<ffi.Char> _chars;
}
/// Native function signature.
/// @param argc number of arguments.
/// @param argv array of arguments. Use `py_arg(i)` macro to get the i-th argument.
/// @return `true` if the function is successful or `false` if an exception is raised.
typedef py_CFunction = ffi.Pointer<
ffi.NativeFunction<ffi.Bool Function(ffi.Int argc, py_StackRef argv)>>;
/// A specific location in the value stack of the VM.
typedef py_StackRef = ffi.Pointer<py_TValue>;
/// A string view type. It is helpful for passing strings which are not null-terminated.
final class c11_sv extends ffi.Struct {
external ffi.Pointer<ffi.Char> data;
@ -4027,22 +4078,12 @@ typedef py_TraceFunc = ffi.Pointer<
/// An output reference for returning a value. Only use this for function arguments.
typedef py_OutRef = ffi.Pointer<py_TValue>;
/// A specific location in the value stack of the VM.
typedef py_StackRef = ffi.Pointer<py_TValue>;
/// A 64-bit integer type. Corresponds to `int` in python.
typedef py_i64 = ffi.Int64;
/// A 64-bit floating-point type. Corresponds to `float` in python.
typedef py_f64 = ffi.Double;
/// Native function signature.
/// @param argc number of arguments.
/// @param argv array of arguments. Use `py_arg(i)` macro to get the i-th argument.
/// @return `true` if the function is successful or `false` if an exception is raised.
typedef py_CFunction = ffi.Pointer<
ffi.NativeFunction<ffi.Bool Function(ffi.Int argc, py_StackRef argv)>>;
/// A pointer that represents a python identifier. For fast name resolution.
typedef py_Name = ffi.Pointer<py_OpaqueName>;
@ -4125,9 +4166,30 @@ final class UnnamedStruct4 extends ffi.Struct {
external int z;
}
final class c11_color32 extends ffi.Union {
final class c11_vec4i extends ffi.Union {
external UnnamedStruct5 unnamed;
@ffi.Array.multi([4])
external ffi.Array<ffi.Int> data;
}
final class UnnamedStruct5 extends ffi.Struct {
@ffi.Int()
external int x;
@ffi.Int()
external int y;
@ffi.Int()
external int z;
@ffi.Int()
external int w;
}
final class c11_color32 extends ffi.Union {
external UnnamedStruct6 unnamed;
@ffi.Array.multi([4])
external ffi.Array<ffi.UnsignedChar> data;
@ -4135,7 +4197,7 @@ final class c11_color32 extends ffi.Union {
external int u32;
}
final class UnnamedStruct5 extends ffi.Struct {
final class UnnamedStruct6 extends ffi.Struct {
@ffi.UnsignedChar()
external int r;
@ -4150,7 +4212,7 @@ final class UnnamedStruct5 extends ffi.Struct {
}
final class c11_mat3x3 extends ffi.Union {
external UnnamedStruct6 unnamed;
external UnnamedStruct7 unnamed;
@ffi.Array.multi([3, 3])
external ffi.Array<ffi.Array<ffi.Float>> m;
@ -4159,7 +4221,7 @@ final class c11_mat3x3 extends ffi.Union {
external ffi.Array<ffi.Float> data;
}
final class UnnamedStruct6 extends ffi.Struct {
final class UnnamedStruct7 extends ffi.Struct {
@ffi.Float()
external double _11;
@ -4306,13 +4368,14 @@ abstract class py_PredefinedType {
static const int tp_vec3 = 71;
static const int tp_vec2i = 72;
static const int tp_vec3i = 73;
static const int tp_mat3x3 = 74;
static const int tp_color32 = 75;
static const int tp_vec4i = 74;
static const int tp_mat3x3 = 75;
static const int tp_color32 = 76;
/// array2d
static const int tp_array2d_like = 76;
static const int tp_array2d_like_iterator = 77;
static const int tp_array2d = 78;
static const int tp_array2d_view = 79;
static const int tp_chunked_array2d = 80;
static const int tp_array2d_like = 77;
static const int tp_array2d_like_iterator = 78;
static const int tp_array2d = 79;
static const int tp_array2d_view = 80;
static const int tp_chunked_array2d = 81;
}

View File

@ -134,10 +134,10 @@ def atan(z: complex):
return 1j / 2 * log((1 - 1j * z) / (1 + 1j * z))
def cos(z: complex):
return (exp(z) + exp(-z)) / 2
return (exp(1j * z) + exp(-1j * z)) / 2
def sin(z: complex):
return (exp(z) - exp(-z)) / (2 * 1j)
return (exp(1j * z) - exp(-1j * z)) / (2 * 1j)
def tan(z: complex):
return sin(z) / cos(z)

File diff suppressed because one or more lines are too long

View File

@ -10,6 +10,8 @@
#else
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#endif
typedef bool (*py_module_initialize_t)() PY_RAISE PY_RETURN;
@ -21,14 +23,44 @@ int load_module_from_dll_desktop_only(const char* path) PY_RAISE PY_RETURN {
if(dll == NULL) return 0;
py_module_initialize_t f_init = (py_module_initialize_t)GetProcAddress(dll, f_init_name);
#else
void* dll = dlopen(path, RTLD_LAZY);
void* dll = NULL;
// On Linux, dlopen doesn't automatically add .so suffix like Windows does with .dll
// Also, CMake typically generates libXxx.so instead of Xxx.so
// Try: path.so, libpath.so, then the original path
char* path_with_so = NULL;
char* path_with_lib = NULL;
size_t path_len = strlen(path);
// Try path.so
path_with_so = py_malloc(path_len + 4); // .so + null terminator
if(path_with_so != NULL) {
strcpy(path_with_so, path);
strcat(path_with_so, ".so");
dll = dlopen(path_with_so, RTLD_LAZY);
py_free(path_with_so);
}
// Try libpath.so if path.so didn't work
if(dll == NULL) {
path_with_lib = py_malloc(path_len + 7); // lib + .so + null terminator
if(path_with_lib != NULL) {
strcpy(path_with_lib, "lib");
strcat(path_with_lib, path);
strcat(path_with_lib, ".so");
dll = dlopen(path_with_lib, RTLD_LAZY);
py_free(path_with_lib);
}
}
// Fallback to original path
if(dll == NULL) {
dll = dlopen(path, RTLD_LAZY);
}
if(dll == NULL) return 0;
py_module_initialize_t f_init = (py_module_initialize_t)dlsym(dll, f_init_name);
#endif
if(f_init == NULL) {
RuntimeError("%s() not found in '%s'", f_init_name, path);
return -1;
}
if(f_init == NULL) return 0;
bool success = f_init();
if(!success) return -1;
return 1;

View File

@ -215,7 +215,31 @@ static bool io_FileIO_flush(int argc, py_Ref argv) {
py_newnone(py_retval());
return true;
}
static bool io_FileIO_readlines(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
io_FileIO* ud = py_touserdata(py_arg(0));
py_newlist(py_retval());
char* buf = NULL;
size_t buf_size = 0;
size_t len = 0;
int c;
while(true) {
len = 0;
while((c = fgetc(ud->file)) != EOF) {
if(len + 1 >= buf_size) {
buf_size = (buf_size == 0) ? 64 : buf_size * 2;
buf = PK_REALLOC(buf, buf_size);
}
buf[len++] = (char)c;
if(c == '\n') break;
}
if(len == 0) break;
py_newstrv(py_getreg(0), (c11_sv){buf, len});
py_list_append(py_retval(), py_getreg(0));
}
if(buf) PK_FREE(buf);
return true;
}
void pk__add_module_io() {
py_Ref mod = py_newmodule("io");
@ -230,6 +254,7 @@ void pk__add_module_io() {
py_bindmethod(FileIO, "tell", io_FileIO_tell);
py_bindmethod(FileIO, "seek", io_FileIO_seek);
py_bindmethod(FileIO, "flush", io_FileIO_flush);
py_bindmethod(FileIO, "readlines", io_FileIO_readlines);
py_newint(py_emplacedict(mod, py_name("SEEK_SET")), SEEK_SET);
py_newint(py_emplacedict(mod, py_name("SEEK_CUR")), SEEK_CUR);