Skip to content

Commit b63d691

Browse files
authored
Merge pull request #259 from markbt/warnings
Fix or suppress warnings and format code
2 parents 8df4c67 + 3c55d95 commit b63d691

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+482
-403
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ endif
5656
doc: build
5757
cargo doc --no-deps $(CARGO_FLAGS)
5858

59+
clippy:
60+
cargo clippy $(CARGO_FLAGS)
61+
5962
extensions: build
6063
make -C extensions/tests PY=$(PY)
6164

build.rs

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,44 @@
11
use std::env;
2-
use std::io::Write;
32

4-
const CFG_KEY: &'static str = "py_sys_config";
3+
const CFG_KEY: &str = "py_sys_config";
54

65
#[cfg(feature = "python27-sys")]
7-
const PYTHONSYS_ENV_VAR: &'static str = "DEP_PYTHON27_PYTHON_FLAGS";
6+
const PYTHONSYS_ENV_VAR: &str = "DEP_PYTHON27_PYTHON_FLAGS";
87

98
#[cfg(feature = "python3-sys")]
10-
const PYTHONSYS_ENV_VAR: &'static str = "DEP_PYTHON3_PYTHON_FLAGS";
9+
const PYTHONSYS_ENV_VAR: &str = "DEP_PYTHON3_PYTHON_FLAGS";
1110

1211
fn main() {
13-
if cfg!(feature = "python27-sys") {
14-
if env::var_os("CARGO_FEATURE_PY_LINK_MODE_DEFAULT").is_some()
15-
|| env::var_os("CARGO_FEATURE_PY_LINK_MODE_UNRESOLVED_STATIC").is_some()
16-
{
17-
writeln!(
18-
std::io::stderr(),
19-
"Cannot use link mode control with Python 2.7"
20-
)
21-
.unwrap();
22-
std::process::exit(1);
23-
}
12+
if cfg!(feature = "python27-sys")
13+
&& (env::var_os("CARGO_FEATURE_PY_LINK_MODE_DEFAULT").is_some()
14+
|| env::var_os("CARGO_FEATURE_PY_LINK_MODE_UNRESOLVED_STATIC").is_some())
15+
{
16+
eprintln!("Cannot use link mode control with Python 2.7");
17+
std::process::exit(1);
2418
}
2519

2620
// python{27,3.x}-sys/build.rs passes python interpreter compile flags via
2721
// environment variable (using the 'links' mechanism in the cargo.toml).
2822
let flags = match env::var(PYTHONSYS_ENV_VAR) {
2923
Ok(flags) => flags,
3024
Err(_) => {
31-
writeln!(
32-
std::io::stderr(),
33-
"Environment variable {} not found - this is supposed to be \
34-
exported from the pythonXX-sys dependency, so the build chain is broken",
25+
eprintln!(
26+
concat!(
27+
"Environment variable {} not found - this is supposed to be ",
28+
"exported from the pythonXX-sys dependency, so the build ",
29+
"chain is broken"
30+
),
3531
PYTHONSYS_ENV_VAR
36-
)
37-
.unwrap();
32+
);
3833
std::process::exit(1);
3934
}
4035
};
4136

42-
if flags.len() > 0 {
43-
for f in flags.split(",") {
37+
if !flags.is_empty() {
38+
for f in flags.split(',') {
4439
// write out flags as --cfg so that the same #cfg blocks can be used
4540
// in rust-cpython as in the -sys libs
46-
let key_and_val: Vec<&str> = f.split("=").collect();
41+
let key_and_val: Vec<&str> = f.split('=').collect();
4742
let key = key_and_val[0];
4843
let val = key_and_val[1];
4944
if key.starts_with("FLAG") {

python27-sys/build.rs

+39-51
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ impl fmt::Display for PythonVersion {
2525
}
2626
}
2727

28-
const CFG_KEY: &'static str = "py_sys_config";
28+
const CFG_KEY: &str = "py_sys_config";
2929

3030
// windows' python writes out lines with the windows crlf sequence;
3131
// posix platforms and mac os should write out lines with just lf.
3232
#[cfg(target_os = "windows")]
33-
static NEWLINE_SEQUENCE: &'static str = "\r\n";
33+
static NEWLINE_SEQUENCE: &str = "\r\n";
3434

3535
#[cfg(not(target_os = "windows"))]
36-
static NEWLINE_SEQUENCE: &'static str = "\n";
36+
static NEWLINE_SEQUENCE: &str = "\n";
3737

3838
// A list of python interpreter compile-time preprocessor defines that
3939
// we will pick up and pass to rustc via --cfg=py_sys_config={varname};
@@ -48,7 +48,7 @@ static NEWLINE_SEQUENCE: &'static str = "\n";
4848
// (hrm, this is sort of re-implementing what distutils does, except
4949
// by passing command line args instead of referring to a python.h)
5050
#[cfg(not(target_os = "windows"))]
51-
static SYSCONFIG_FLAGS: [&'static str; 7] = [
51+
static SYSCONFIG_FLAGS: [&str; 7] = [
5252
"Py_USING_UNICODE",
5353
"Py_UNICODE_WIDE",
5454
"WITH_THREAD",
@@ -58,7 +58,7 @@ static SYSCONFIG_FLAGS: [&'static str; 7] = [
5858
"COUNT_ALLOCS",
5959
];
6060

61-
static SYSCONFIG_VALUES: [&'static str; 1] = [
61+
static SYSCONFIG_VALUES: [&str; 1] = [
6262
// cfg doesn't support flags with values, just bools - so flags
6363
// below are translated into bools as {varname}_{val}
6464
//
@@ -70,7 +70,7 @@ static SYSCONFIG_VALUES: [&'static str; 1] = [
7070
/// the interpreter and printing variables of interest from
7171
/// sysconfig.get_config_vars.
7272
#[cfg(not(target_os = "windows"))]
73-
fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, String> {
73+
fn get_config_vars(python_path: &str) -> Result<HashMap<String, String>, String> {
7474
let mut script = "import sysconfig; \
7575
config = sysconfig.get_config_vars();"
7676
.to_owned();
@@ -81,7 +81,7 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
8181
k,
8282
if is_value(k) { "None" } else { "0" }
8383
));
84-
script.push_str(";");
84+
script.push(';');
8585
}
8686

8787
let mut cmd = Command::new(python_path);
@@ -93,7 +93,7 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
9393

9494
if !out.status.success() {
9595
let stderr = String::from_utf8(out.stderr).unwrap();
96-
let mut msg = format!("python script failed with stderr:\n\n");
96+
let mut msg = "python script failed with stderr:\n\n".to_string();
9797
msg.push_str(&stderr);
9898
return Err(msg);
9999
}
@@ -105,15 +105,14 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
105105
"python stdout len didn't return expected number of lines:
106106
{}",
107107
split_stdout.len()
108-
)
109-
.to_string());
108+
));
110109
}
111110
let all_vars = SYSCONFIG_FLAGS.iter().chain(SYSCONFIG_VALUES.iter());
112111
// let var_map: HashMap<String, String> = HashMap::new();
113112
Ok(all_vars.zip(split_stdout.iter()).fold(
114113
HashMap::new(),
115114
|mut memo: HashMap<String, String>, (&k, &v)| {
116-
if !(v.to_owned() == "None" && is_value(k)) {
115+
if !(v == "None" && is_value(k)) {
117116
memo.insert(k.to_owned(), v.to_owned());
118117
}
119118
memo
@@ -122,7 +121,7 @@ fn get_config_vars(python_path: &String) -> Result<HashMap<String, String>, Stri
122121
}
123122

124123
#[cfg(target_os = "windows")]
125-
fn get_config_vars(_: &String) -> Result<HashMap<String, String>, String> {
124+
fn get_config_vars(_: &str) -> Result<HashMap<String, String>, String> {
126125
// sysconfig is missing all the flags on windows, so we can't actually
127126
// query the interpreter directly for its build flags.
128127
//
@@ -151,7 +150,7 @@ fn get_config_vars(_: &String) -> Result<HashMap<String, String>, String> {
151150
}
152151

153152
fn is_value(key: &str) -> bool {
154-
SYSCONFIG_VALUES.iter().find(|x| **x == key).is_some()
153+
SYSCONFIG_VALUES.iter().any(|x| *x == key)
155154
}
156155

157156
fn cfg_line_for_var(key: &str, val: &str) -> Option<String> {
@@ -185,17 +184,17 @@ fn run_python_script(interpreter: &str, script: &str) -> Result<String, String>
185184

186185
if !out.status.success() {
187186
let stderr = String::from_utf8(out.stderr).unwrap();
188-
let mut msg = format!("python script failed with stderr:\n\n");
187+
let mut msg = "python script failed with stderr:\n\n".to_string();
189188
msg.push_str(&stderr);
190189
return Err(msg);
191190
}
192191

193-
let out = String::from_utf8(out.stdout).unwrap();
194-
return Ok(out);
192+
Ok(String::from_utf8(out.stdout).unwrap())
195193
}
196194

197195
#[cfg(not(target_os = "macos"))]
198196
#[cfg(not(target_os = "windows"))]
197+
#[allow(clippy::unnecessary_wraps)]
199198
fn get_rustc_link_lib(
200199
_: &PythonVersion,
201200
ld_version: &str,
@@ -278,7 +277,7 @@ fn find_interpreter_and_get_config(
278277
let (executable, interpreter_version, lines) =
279278
get_config_from_interpreter(interpreter_path)?;
280279
if matching_version(expected_version, &interpreter_version) {
281-
return Ok((interpreter_version, executable.to_owned(), lines));
280+
return Ok((interpreter_version, executable, lines));
282281
} else {
283282
return Err(format!(
284283
"Wrong python version in PYTHON_SYS_EXECUTABLE={}\n\
@@ -297,11 +296,9 @@ fn find_interpreter_and_get_config(
297296
}
298297

299298
for name in possible_names.iter() {
300-
if let Some((executable, interpreter_version, lines)) =
301-
get_config_from_interpreter(name).ok()
302-
{
299+
if let Ok((executable, interpreter_version, lines)) = get_config_from_interpreter(name) {
303300
if matching_version(expected_version, &interpreter_version) {
304-
return Ok((interpreter_version, executable.to_owned(), lines));
301+
return Ok((interpreter_version, executable, lines));
305302
}
306303
}
307304
}
@@ -371,20 +368,18 @@ fn configure_from_path(expected_version: &PythonVersion) -> Result<String, Strin
371368
println!("cargo:rustc-link-search=native={}\\libs", exec_prefix);
372369
}
373370
}
374-
} else if link_mode_unresolved_static {
375-
if cfg!(target_os = "windows") {
376-
// static-nobundle requires a Nightly rustc up to at least
377-
// Rust 1.39 (https://github.com/rust-lang/rust/issues/37403).
378-
//
379-
// We need to use static linking on Windows to prevent symbol
380-
// name mangling. Otherwise Rust will prefix extern {} symbols
381-
// with __imp_. But if we used normal "static," we need a
382-
// pythonXY.lib at build time to package into the rlib.
383-
//
384-
// static-nobundle removes the build-time library requirement,
385-
// allowing a downstream consumer to provide the pythonXY library.
386-
println!("cargo:rustc-link-lib=static-nobundle=pythonXY");
387-
}
371+
} else if link_mode_unresolved_static && cfg!(target_os = "windows") {
372+
// static-nobundle requires a Nightly rustc up to at least
373+
// Rust 1.39 (https://github.com/rust-lang/rust/issues/37403).
374+
//
375+
// We need to use static linking on Windows to prevent symbol
376+
// name mangling. Otherwise Rust will prefix extern {} symbols
377+
// with __imp_. But if we used normal "static," we need a
378+
// pythonXY.lib at build time to package into the rlib.
379+
//
380+
// static-nobundle removes the build-time library requirement,
381+
// allowing a downstream consumer to provide the pythonXY library.
382+
println!("cargo:rustc-link-lib=static-nobundle=pythonXY");
388383
}
389384

390385
if let PythonVersion {
@@ -402,7 +397,7 @@ fn configure_from_path(expected_version: &PythonVersion) -> Result<String, Strin
402397
}
403398
}
404399

405-
return Ok(interpreter_path);
400+
Ok(interpreter_path)
406401
}
407402

408403
/// Determine the python version we're supposed to be building
@@ -418,17 +413,11 @@ fn version_from_env() -> Result<PythonVersion, String> {
418413
let mut vars = env::vars().collect::<Vec<_>>();
419414
vars.sort_by(|a, b| b.cmp(a));
420415
for (key, _) in vars {
421-
match re.captures(&key) {
422-
Some(cap) => {
423-
return Ok(PythonVersion {
424-
major: cap.get(1).unwrap().as_str().parse().unwrap(),
425-
minor: match cap.get(3) {
426-
Some(s) => Some(s.as_str().parse().unwrap()),
427-
None => None,
428-
},
429-
})
430-
}
431-
None => (),
416+
if let Some(cap) = re.captures(&key) {
417+
return Ok(PythonVersion {
418+
major: cap.get(1).unwrap().as_str().parse().unwrap(),
419+
minor: cap.get(3).map(|s| s.as_str().parse().unwrap()),
420+
});
432421
}
433422
}
434423
Err(
@@ -459,9 +448,8 @@ fn main() {
459448
config_map.insert("Py_REF_DEBUG".to_owned(), "1".to_owned()); // Py_TRACE_REFS implies Py_REF_DEBUG.
460449
}
461450
for (key, val) in &config_map {
462-
match cfg_line_for_var(key, val) {
463-
Some(line) => println!("{}", line),
464-
None => (),
451+
if let Some(line) = cfg_line_for_var(key, val) {
452+
println!("{}", line);
465453
}
466454
}
467455

@@ -489,7 +477,7 @@ fn main() {
489477
});
490478
println!(
491479
"cargo:python_flags={}",
492-
if flags.len() > 0 {
480+
if !flags.is_empty() {
493481
&flags[..flags.len() - 1]
494482
} else {
495483
""

python27-sys/src/code.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ extern "C" {
7878
firstlineno: c_int,
7979
) -> *mut PyCodeObject;
8080
pub fn PyCode_Addr2Line(arg1: *mut PyCodeObject, arg2: c_int) -> c_int;
81-
//fn _PyCode_CheckLineNumber(co: *mut PyCodeObject,
82-
// lasti: c_int,
83-
// bounds: *mut PyAddrPair) -> c_int;
81+
82+
ignore! {
83+
fn _PyCode_CheckLineNumber(
84+
co: *mut PyCodeObject,
85+
lasti: c_int,
86+
bounds: *mut PyAddrPair,
87+
) -> c_int;
88+
}
89+
8490
pub fn PyCode_Optimize(
8591
code: *mut PyObject,
8692
consts: *mut PyObject,

python27-sys/src/compile.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ pub struct PyFutureFeatures {
1111
pub ff_lineno: c_int,
1212
}
1313

14-
pub const FUTURE_NESTED_SCOPES: &'static str = "nested_scopes";
15-
pub const FUTURE_GENERATORS: &'static str = "generators";
16-
pub const FUTURE_DIVISION: &'static str = "division";
17-
pub const FUTURE_ABSOLUTE_IMPORT: &'static str = "absolute_import";
18-
pub const FUTURE_WITH_STATEMENT: &'static str = "with_statement";
19-
pub const FUTURE_PRINT_FUNCTION: &'static str = "print_function";
20-
pub const FUTURE_UNICODE_LITERALS: &'static str = "unicode_literals";
14+
pub const FUTURE_NESTED_SCOPES: &str = "nested_scopes";
15+
pub const FUTURE_GENERATORS: &str = "generators";
16+
pub const FUTURE_DIVISION: &str = "division";
17+
pub const FUTURE_ABSOLUTE_IMPORT: &str = "absolute_import";
18+
pub const FUTURE_WITH_STATEMENT: &str = "with_statement";
19+
pub const FUTURE_PRINT_FUNCTION: &str = "print_function";
20+
pub const FUTURE_UNICODE_LITERALS: &str = "unicode_literals";
2121

2222
#[cfg_attr(windows, link(name = "pythonXY"))]
2323
extern "C" {

python27-sys/src/complexobject.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ extern "C" {
5757
pub fn PyComplex_ImagAsDouble(op: *mut PyObject) -> c_double;
5858
pub fn PyComplex_AsCComplex(op: *mut PyObject) -> Py_complex;
5959

60-
//fn _PyComplex_FormatAdvanced(obj: *mut PyObject,
61-
// format_spec: *mut c_char,
62-
// format_spec_len: Py_ssize_t)
63-
// -> *mut PyObject;
60+
ignore! {
61+
fn _PyComplex_FormatAdvanced(
62+
obj: *mut PyObject,
63+
format_spec: *mut c_char,
64+
format_spec_len: Py_ssize_t,
65+
) -> *mut PyObject;
66+
}
6467
}

python27-sys/src/descrobject.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ pub unsafe fn PyDescr_IsData(d: *mut PyObject) -> c_int {
8787

8888
#[cfg_attr(windows, link(name = "pythonXY"))]
8989
extern "C" {
90-
//pub fn PyDictProxy_New(arg1: *mut PyObject) -> *mut PyObject;
91-
// PyDictProxy_New is also defined in dictobject.h
90+
ignore! {
91+
// PyDictProxy_New is also defined in dictobject.h
92+
pub fn PyDictProxy_New(arg1: *mut PyObject) -> *mut PyObject;
93+
}
9294
pub fn PyWrapper_New(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject;
9395
}

0 commit comments

Comments
 (0)