Skip to content

Commit 5aae3a5

Browse files
committed
Rename stdlib _queue init if gevent present
1 parent d66cc29 commit 5aae3a5

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

pyoxidizer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ hex = "0.3"
3636
itertools = "0.8"
3737
lazy_static = "1.3"
3838
libc = "0.2"
39+
memmap = "0.7"
3940
object = { version = "0.15.0", features = ["read", "std", "write"] }
4041
regex = "1"
4142
reqwest = "0.9"

pyoxidizer/src/pyrepackager/repackage.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::fs;
1313
use std::fs::create_dir_all;
1414
use std::io::{BufRead, BufReader, Cursor, Error as IOError, Read, Write};
1515
use std::path::{Path, PathBuf};
16+
use memmap::{Mmap};
1617

1718
use super::bytecode::{BytecodeCompiler, CompileMode};
1819
use super::config::{
@@ -1121,8 +1122,12 @@ fn make_config_c(
11211122
if init_fn == "NULL" {
11221123
continue;
11231124
}
1124-
1125-
lines.push(format!("extern PyObject* {}(void);", init_fn));
1125+
if init_fn == "PyInit__queue" && built_extension_modules.contains_key("gevent._queue") {
1126+
lines.push("extern PyObject* PyInit_stdlib_queue(void);".to_string());
1127+
}
1128+
else {
1129+
lines.push(format!("extern PyObject* {}(void);", init_fn));
1130+
}
11261131
}
11271132
}
11281133

@@ -1146,9 +1151,13 @@ fn make_config_c(
11461151
if init_fn == "NULL" {
11471152
continue;
11481153
}
1149-
1150-
lines.push(format!("{{\"{}\", {}}},", em.module, init_fn));
1151-
ambiguous_init_fns.push(init_fn.to_string());
1154+
if init_fn == "PyInit__queue" && built_extension_modules.contains_key("gevent._queue") {
1155+
lines.push("{\"_queue\", PyInit_stdlib_queue},".to_string());
1156+
}
1157+
else {
1158+
lines.push(format!("{{\"{}\", {}}},", em.module, init_fn));
1159+
ambiguous_init_fns.push(init_fn.to_string());
1160+
}
11521161
}
11531162
}
11541163

@@ -1310,7 +1319,10 @@ pub fn link_libpython(
13101319
);
13111320
for (name, em) in extension_modules {
13121321
if let Some(init_fn) = &em.init_fn {
1313-
if init_fn != "NULL" {
1322+
if init_fn == "PyInit__queue" && built_extension_modules.contains_key("gevent._queue") {
1323+
ambiguous_init_fns.push("PyInit_stdlib_queue".to_string());
1324+
}
1325+
else if init_fn != "NULL" {
13141326
ambiguous_init_fns.push(init_fn.to_string());
13151327
}
13161328
}
@@ -1326,8 +1338,23 @@ pub fn link_libpython(
13261338
name,
13271339
em.object_paths
13281340
);
1341+
13291342
for path in &em.object_paths {
1330-
build.object(path);
1343+
let mut out_path = path.clone();
1344+
if path.ends_with("_queuemodule.o") && built_extension_modules.contains_key("gevent._queue") {
1345+
out_path = temp_dir_path.join(format!("{}_stdlib_prefixed.o", path.display()));
1346+
1347+
let file = fs::File::open(&path).unwrap();
1348+
let object_data = unsafe { Mmap::map(&file).unwrap() };
1349+
1350+
match rename_init(logger, &"stdlib_queue".to_string(), &object_data) {
1351+
Ok(val) => fs::write(&out_path, val).expect("unable to write object file"),
1352+
Err(err) => {
1353+
println!("Failed to rename symbol in '{}': {}", path.display(), err);
1354+
}
1355+
};
1356+
}
1357+
build.object(out_path);
13311358
}
13321359

13331360
for entry in &em.links {

0 commit comments

Comments
 (0)