cpmech / plotpy

Rust plotting library using Python (Matplotlib)
MIT License
65 stars 6 forks source link

Does not automatically sanitize plot names that have the character ' in them #44

Closed hiibolt closed 4 months ago

hiibolt commented 4 months ago

Error

# cat 56c63c8b-4a91-4ed7-9e9a-1df0e7565434.log
  File "//assets/56c63c8b-4a91-4ed7-9e9a-1df0e7565434.py", line 85
    plt.title(r'SIX INVITATIONAL ATTENDEE '18 (Charm)')
                                                     ^
SyntaxError: unterminated string literal (detected at line 85)

Steps to reproduce

Use plotpy to create a graph with a title containing a single quotation mark.

Potential Solution

Change the following in src/plot.rs:

/// Adds a title to the plot or sub-plot
pub fn set_title(&mut self, title: &str) -> &mut Self {
    write!(&mut self.buffer, "plt.title(r'{}')\n", title).unwrap();
    self
}

...to...

/// Adds a title to the plot or sub-plot
pub fn set_title(&mut self, title: &str) -> &mut Self {
    let sanitized_title = title.replace("'", "\\'");

    write!(&mut self.buffer, "plt.title(r'{}')\n", sanitized_title).unwrap();
    self
}

Love this crate by the way, I use it daily ❤️❤️

cpmech commented 4 months ago

Hi, thank you very much for the feedback.

This would be fixed now, according to https://github.com/cpmech/plotpy/pull/47/files

Please let me know if you encounter further problems.

cpmech commented 4 months ago

By the way, the crate version with the fix is now 1.0.0

hiibolt commented 4 months ago

Thank you for the quick resolution!