Skip to content

Commit

Permalink
Get cargo test py passing on Windows. (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfk authored Feb 19, 2021
1 parent 934bc02 commit 16a3fa0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion uniffi/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn ensure_compiled_cdylib(pkg_dir: &str) -> Result<String> {
.filenames
.iter()
.filter(|nm| match nm.extension().unwrap_or_default().to_str() {
Some("dylib") | Some("so") => true,
Some(std::env::consts::DLL_EXTENSION) => true,
_ => false,
})
.collect();
Expand Down
9 changes: 5 additions & 4 deletions uniffi_bindgen/src/bindings/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ pub fn generate_python_bindings(config: &Config, ci: &ComponentInterface) -> Res
/// Execute the specifed python script, with environment based on the generated
/// artifacts in the given output directory.
pub fn run_script(out_dir: &Path, script_file: &Path) -> Result<()> {
let mut pythonpath = env::var_os("PYTHONPATH").unwrap_or_else(|| OsString::from(""));
// This lets python find the compiled library for the rust component.
pythonpath.push(":");
pythonpath.push(out_dir);
let mut cmd = Command::new("python3");
// This helps python find the generated .py wrapper for rust component.
let pythonpath = env::var_os("PYTHONPATH").unwrap_or_else(|| OsString::from(""));
let pythonpath =
env::join_paths(env::split_paths(&pythonpath).chain(vec![out_dir.to_path_buf()]))?;
cmd.env("PYTHONPATH", pythonpath);
// We should now be able to execute the tests successfully.
cmd.arg(script_file);
let status = cmd
.spawn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ def loadIndirect():
elif sys.platform == "darwin":
libname = "lib{}.dylib"
elif sys.platform.startswith("win"):
libname = "lib_{}.dll"
# As of python3.8, ctypes does not seem to search $PATH when loading DLLs.
# We could use `os.add_dll_directory` to configure the search path, but
# it doesn't feel right to mess with application-wide settings. Let's
# assume that the `.dll` is next to the `.py` file and load by full path.
libname = os.path.join(
os.path.dirname(__file__),
"{}.dll",
)
return getattr(ctypes.cdll, libname.format("{{ config.cdylib_name() }}"))

# A ctypes library to expose the extern-C FFI definitions.
Expand All @@ -23,4 +30,4 @@ def loadIndirect():
{%- call py::arg_list_ffi_decl(func) -%}
)
_UniFFILib.{{ func.name() }}.restype = {% match func.return_type() %}{% when Some with (type_) %}{{ type_|type_ffi }}{% when None %}None{% endmatch %}
{%- endfor %}
{%- endfor %}
1 change: 1 addition & 0 deletions uniffi_bindgen/src/bindings/python/templates/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# compile the rust component. The easiest way to ensure this is to bundle the Python
# helpers directly inline like we're doing here.

import os
import sys
import ctypes
import enum
Expand Down

0 comments on commit 16a3fa0

Please sign in to comment.