Skip to content

Commit

Permalink
Try cffi embedding API
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Dec 28, 2021
1 parent 9518ea1 commit 2ad5294
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/_cffi_src/build_openssl.py
Expand Up @@ -114,9 +114,11 @@ def _extra_compile_args(platform):
)

if __name__ == "__main__":
from cffi import recompiler

out_dir = os.getenv("OUT_DIR")
module_name, source, source_extension, kwds = ffi._assigned_source
c_file = os.path.join(out_dir, module_name + source_extension)
recompiler.make_c_source(ffi, module_name, source, c_file)
ffi.embedding_api("""
extern "Python" void _this_is_not_used(void);
""")
ffi.embedding_init_code("")
ffi.emit_c_code(c_file)
6 changes: 6 additions & 0 deletions src/_cffi_src/utils.py
Expand Up @@ -73,6 +73,12 @@ def build_ffi(
verify_source += '\n#define CRYPTOGRAPHY_PACKAGE_VERSION "{}"'.format(
about["__version__"]
)
verify_source += r"""
int make_cryptography_openssl_module(void) {
return cffi_start_python();
}
"""
ffi.cdef(cdef_source)
ffi.set_source(
module_name,
Expand Down
2 changes: 1 addition & 1 deletion src/rust/build.rs
Expand Up @@ -74,7 +74,7 @@ fn main() {
// Enable abi3 mode if we're not using PyPy.
if python_impl != "PyPy" {
// cp36
build.define("Py_LIMITED_API", "0x030600f0");
// build.define("Py_LIMITED_API", "0x030600f0");
}

build.compile("_openssl.a");
Expand Down
9 changes: 5 additions & 4 deletions src/rust/src/lib.rs
Expand Up @@ -11,11 +11,10 @@ extern crate openssl_sys;
mod asn1;
mod x509;

use pyo3::FromPyPointer;
use std::convert::TryInto;

extern "C" {
fn PyInit__openssl() -> *mut pyo3::ffi::PyObject;
fn make_cryptography_openssl_module() -> std::os::raw::c_int;
}

/// Returns the value of the input with the most-significant-bit copied to all
Expand Down Expand Up @@ -101,8 +100,10 @@ fn _rust(py: pyo3::Python<'_>, m: &pyo3::types::PyModule) -> pyo3::PyResult<()>
m.add_submodule(ocsp_mod)?;

let openssl_mod = unsafe {
let ptr = PyInit__openssl();
pyo3::types::PyModule::from_owned_ptr(py, ptr)
// let ptr = PyInit__openssl();
// pyo3::types::PyModule::from_owned_ptr(py, ptr)
make_cryptography_openssl_module();
pyo3::types::PyModule::import(py, "_openssl")?
};
m.add_submodule(openssl_mod)?;
Ok(())
Expand Down

0 comments on commit 2ad5294

Please sign in to comment.