Skip to content

Add: _uuid module #3974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Aug 7, 2022
Merged

Add: _uuid module #3974

merged 21 commits into from
Aug 7, 2022

Conversation

rimi0108
Copy link
Contributor

  • Add _uuid module
  • Add _uuid function generate_time_safe()
  • uuid v1 = generate_time_safe
  • Return mac address to node_id if you have mac address or return random generated bytes to node_id if you do not have mac address

@@ -63,6 +63,8 @@ ahash = "0.7.6"
libz-sys = { version = "1.1.5", optional = true }
num_enum = "0.5.7"
ascii = "1.0.0"
once_cell = "1.13.0"
mac_address = "1.1.3"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this line instead of dependencies.uuid section
will be more easy to read

uuid = { version = "1.1.2", features = ["v1", "fast-rng", "macro-diagnostics"] }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mac_address needs more cfg guard for more OS.
See mmap case. By referring CI failure, we (at least) need to exclude iOS and android.

@@ -51,6 +51,8 @@ mod select;
mod ssl;
#[cfg(all(unix, not(target_os = "redox")))]
mod termios;
#[cfg(target_os = "macos")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the blocker for linux and windows?


#[pymodule]
mod _uuid {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment on lines 19 to 32
match get_mac_address() {
Ok(Some(_ma)) => {
let node_id = NODE_ID.get_or_init(|| get_mac_address().unwrap().unwrap().bytes());
node_id
}
Ok(None) => {
let node_id = NODE_ID.get_or_init(|| rand::thread_rng().gen::<[u8; 6]>());
node_id
}
Err(_e) => {
let node_id = NODE_ID.get_or_init(|| rand::thread_rng().gen::<[u8; 6]>());
node_id
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is checking get_mac_address every time. That probably is not expectation for node id. NODE_ID.get_or_init must be the outmost wrapper of this function

@youknowone
Copy link
Member

_uuid.has_uuid_generate_time_safe seems required to let the module run

@youknowone youknowone added the z-ca-2022 Tag to track contrubution-academy 2022 label Jul 27, 2022
Comment on lines 22 to 29
Ok(None) => {
let node_id = rand::thread_rng().gen::<[u8; 6]>();
node_id
}
Err(_e) => {
let node_id = rand::thread_rng().gen::<[u8; 6]>();
node_id
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Ok(None) => {
let node_id = rand::thread_rng().gen::<[u8; 6]>();
node_id
}
Err(_e) => {
let node_id = rand::thread_rng().gen::<[u8; 6]>();
node_id
}
_ => {
let node_id = rand::thread_rng().gen::<[u8; 6]>();
node_id
}

Comment on lines 43 to 44
let now = now_unix_duration();
static CONTEXT: Context = Context::new(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let now = now_unix_duration();
static CONTEXT: Context = Context::new(0);
static CONTEXT: Context = Context::new(0);
let now = now_unix_duration();

technically this is ok but I feel uncomfortable to have static below without a good reason

static CONTEXT: Context = Context::new(0);
let ts = Timestamp::from_unix(&CONTEXT, now.as_secs(), now.subsec_nanos());

let node_id = get_node_id();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we will not use the value from second call, why do we call this function every time?
You can move this line in get_or_init().

}

#[pyfunction]
fn generate_time_safe(_vm: &VirtualMachine) -> (Vec<u8>, PyNone) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn generate_time_safe(_vm: &VirtualMachine) -> (Vec<u8>, PyNone) {
fn generate_time_safe() -> (Vec<u8>, PyNone) {

}

#[pyattr]
fn has_uuid_generate_time_safe(_vm: &VirtualMachine) -> PyInt {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn has_uuid_generate_time_safe(_vm: &VirtualMachine) -> PyInt {
fn has_uuid_generate_time_safe() -> u32 {


#[pyattr]
fn has_uuid_generate_time_safe(_vm: &VirtualMachine) -> PyInt {
PyInt::from(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting. What this value is meaning? 🤔

Copy link
Contributor Author

@rimi0108 rimi0108 Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote the code like that because it was printed below the cpython
>>> _uuid.has_uuid_generate_time_safe
0

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem didn't set git email correctly. Check git log and if it matches your actual email (and github email)

@@ -63,6 +63,8 @@ ahash = "0.7.6"
libz-sys = { version = "1.1.5", optional = true }
num_enum = "0.5.7"
ascii = "1.0.0"
once_cell = "1.13.0"
mac_address = "1.1.3"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mac_address needs more cfg guard for more OS.
See mmap case. By referring CI failure, we (at least) need to exclude iOS and android.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thank you for long time effort

@youknowone youknowone merged commit 42290b1 into RustPython:main Aug 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
z-ca-2022 Tag to track contrubution-academy 2022
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants