Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbcf470e

Browse files
Merge pull requestRustPython#1111 from RustPython/sys-version
Add git information in sys and platform modules.
2 parents69980c2 +ef649be commitbcf470e

File tree

5 files changed

+119
-6
lines changed

5 files changed

+119
-6
lines changed

‎vm/build.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::process::Command;
2+
3+
fnmain(){
4+
println!("cargo:rustc-env=RUSTPYTHON_GIT_HASH={}", git_hash());
5+
println!(
6+
"cargo:rustc-env=RUSTPYTHON_GIT_TIMESTAMP={}",
7+
git_timestamp()
8+
);
9+
println!("cargo:rustc-env=RUSTPYTHON_GIT_BRANCH={}", git_branch());
10+
}
11+
12+
fngit_hash() ->String{
13+
git(&["rev-parse","HEAD"])
14+
}
15+
16+
fngit_timestamp() ->String{
17+
git(&["log","-1","--format=%cd"])
18+
}
19+
20+
fngit_branch() ->String{
21+
git(&["rev-parse","--abbrev-ref","HEAD"])
22+
}
23+
24+
fngit(args:&[&str]) ->String{
25+
command("git", args)
26+
}
27+
28+
fncommand(cmd:&str,args:&[&str]) ->String{
29+
matchCommand::new(cmd).args(args).output(){
30+
Ok(output) =>matchString::from_utf8(output.stdout){
31+
Ok(s) => s,
32+
Err(err) =>format!("(output error: {})", err),
33+
},
34+
Err(err) =>format!("(command error: {})", err),
35+
}
36+
}

‎vm/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub mod stdlib;
6060
mod sysmodule;
6161
mod traceback;
6262
pubmod util;
63+
mod version;
6364
mod vm;
6465

6566
// pub use self::pyobject::Executor;

‎vm/src/stdlib/platform.rs‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
usecrate::function::PyFuncArgs;
22
usecrate::pyobject::{PyObjectRef,PyResult};
3+
usecrate::version;
34
usecrate::vm::VirtualMachine;
45

56
pubfnmake_module(vm:&VirtualMachine) ->PyObjectRef{
67
let ctx =&vm.ctx;
78
py_module!(vm,"platform",{
9+
"python_branch" => ctx.new_rustfunc(platform_python_branch),
10+
"python_build" => ctx.new_rustfunc(platform_python_build),
811
"python_compiler" => ctx.new_rustfunc(platform_python_compiler),
912
"python_implementation" => ctx.new_rustfunc(platform_python_implementation),
13+
"python_revision" => ctx.new_rustfunc(platform_python_revision),
1014
"python_version" => ctx.new_rustfunc(platform_python_version),
1115
})
1216
}
@@ -18,12 +22,28 @@ fn platform_python_implementation(vm: &VirtualMachine, args: PyFuncArgs) -> PyRe
1822

1923
fnplatform_python_version(vm:&VirtualMachine,args:PyFuncArgs) ->PyResult{
2024
arg_check!(vm, args);
21-
// TODO: fetch version from somewhere.
22-
Ok(vm.new_str("4.0.0".to_string()))
25+
Ok(vm.new_str(version::get_version_number()))
2326
}
2427

2528
fnplatform_python_compiler(vm:&VirtualMachine,args:PyFuncArgs) ->PyResult{
2629
arg_check!(vm, args);
27-
let version = rustc_version_runtime::version_meta();
28-
Ok(vm.new_str(format!("rustc {}", version.semver)))
30+
Ok(vm.new_str(version::get_compiler()))
31+
}
32+
33+
fnplatform_python_build(vm:&VirtualMachine,args:PyFuncArgs) ->PyResult{
34+
arg_check!(vm, args);
35+
let(git_hash, git_timestamp) = version::get_build_info();
36+
Ok(vm
37+
.ctx
38+
.new_tuple(vec![vm.new_str(git_hash), vm.new_str(git_timestamp)]))
39+
}
40+
41+
fnplatform_python_branch(vm:&VirtualMachine,args:PyFuncArgs) ->PyResult{
42+
arg_check!(vm, args);
43+
Ok(vm.new_str(version::get_git_branch()))
44+
}
45+
46+
fnplatform_python_revision(vm:&VirtualMachine,args:PyFuncArgs) ->PyResult{
47+
arg_check!(vm, args);
48+
Ok(vm.new_str(version::get_git_revision()))
2949
}

‎vm/src/sysmodule.rs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::frame::FrameRef;
55
usecrate::function::{OptionalArg,PyFuncArgs};
66
usecrate::obj::objstr::PyStringRef;
77
usecrate::pyobject::{IntoPyObject,ItemProtocol,PyClassImpl,PyContext,PyObjectRef,PyResult};
8+
usecrate::version;
89
usecrate::vm::VirtualMachine;
910

1011
/*
@@ -160,6 +161,8 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef, builtins: PyObjectR
160161
"unknown".to_string()
161162
};
162163

164+
let copyright ="Copyright (c) 2019 RustPython Team";
165+
163166
let sys_doc ="This module provides access to some objects used or maintained by the
164167
interpreter and to functions that interact strongly with the interpreter.
165168
@@ -229,16 +232,23 @@ setprofile() -- set the global profiling function
229232
setrecursionlimit() -- set the max recursion depth for the interpreter
230233
settrace() -- set the global debug tracing function
231234
";
232-
letmut module_names:Vec<_> = vm.stdlib_inits.borrow().keys().cloned().collect();
235+
letmut module_names:Vec<String> = vm.stdlib_inits.borrow().keys().cloned().collect();
233236
module_names.push("sys".to_string());
234237
module_names.push("builtins".to_string());
235238
module_names.sort();
239+
let builtin_module_names = ctx.new_tuple(
240+
module_names
241+
.iter()
242+
.map(|v| v.into_pyobject(vm).unwrap())
243+
.collect(),
244+
);
236245
let modules = ctx.new_dict();
237246
extend_module!(vm, module,{
238247
"__name__" => ctx.new_str(String::from("sys")),
239248
"argv" => argv(ctx),
240-
"builtin_module_names" =>ctx.new_tuple(module_names.iter().map(|v| v.into_pyobject(vm).unwrap()).collect()),
249+
"builtin_module_names" =>builtin_module_names,
241250
"byteorder" => ctx.new_str(bytorder),
251+
"copyright" => ctx.new_str(copyright.to_string()),
242252
"flags" => flags,
243253
"getrefcount" => ctx.new_rustfunc(sys_getrefcount),
244254
"getsizeof" => ctx.new_rustfunc(sys_getsizeof),
@@ -260,6 +270,7 @@ settrace() -- set the global debug tracing function
260270
"path_importer_cache" => ctx.new_dict(),
261271
"pycache_prefix" => vm.get_none(),
262272
"dont_write_bytecode" => vm.new_bool(true),
273+
"version" => vm.new_str(version::get_version()),
263274
});
264275

265276
modules.set_item("sys", module.clone(), vm).unwrap();

‎vm/src/version.rs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Several function to retrieve version information.
2+
*/
3+
4+
pubfnget_version() ->String{
5+
format!(
6+
"{} {:?} {}",
7+
get_version_number(),
8+
get_build_info(),
9+
get_compiler()
10+
)
11+
}
12+
13+
pubfnget_version_number() ->String{
14+
format!(
15+
"{}.{}.{}{}",
16+
env!("CARGO_PKG_VERSION_MAJOR"),
17+
env!("CARGO_PKG_VERSION_MINOR"),
18+
env!("CARGO_PKG_VERSION_PATCH"),
19+
option_env!("CARGO_PKG_VERSION_PRE").unwrap_or("")
20+
)
21+
}
22+
23+
pubfnget_compiler() ->String{
24+
let rustc_version = rustc_version_runtime::version_meta();
25+
format!("rustc {}", rustc_version.semver)
26+
}
27+
28+
pubfnget_build_info() ->(String,String){
29+
let git_hash =get_git_revision();
30+
// See: https://reproducible-builds.org/docs/timestamps/
31+
let git_timestamp =option_env!("RUSTPYTHON_GIT_TIMESTAMP")
32+
.unwrap_or("")
33+
.to_string();
34+
(git_hash, git_timestamp)
35+
}
36+
37+
pubfnget_git_revision() ->String{
38+
option_env!("RUSTPYTHON_GIT_HASH").unwrap_or("").to_string()
39+
}
40+
41+
pubfnget_git_branch() ->String{
42+
option_env!("RUSTPYTHON_GIT_BRANCH")
43+
.unwrap_or("")
44+
.to_string()
45+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp