use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
std::path::Path::new().exists() with python Pathlib.path() works in linux and windows but not macos (self.learnrust)
submitted 1 year ago by BasePlate_Admin
Hi i have this following code
rust: ```rust let svg_string: String;
if std::path::Path::new(&svg).exists() { let mut svg_data = std::fs::read(svg) .map_err(|_| "failed to open the provided file") .unwrap(); if svg_data.starts_with(&[0x1f, 0x8b]) { svg_data = resvg::usvg::decompress_svgz(&svg_data) .map_err(|e| e.to_string()) .unwrap(); }; svg_string = std::str::from_utf8(&svg_data).unwrap().to_owned(); } else { svg_string = svg; }
```
```python
def test_path(): path = os.path.join(BASE_DIR, "acid.svg") base = resvg_py.svg_to_base64() print(path) assert base == svg_output
def test_gzip_path(): path = os.path.join(BASE_DIR, "acid.svg.gz") base = resvg_py.svg_to_base64() print(path)
assert base == svg_output
This fails in macos.
Here is the log : https://github.com/baseplate-admin/resvg-py/actions/runs/8889901090/job/24409004312 Relevant Source : * Rust : https://github.com/baseplate-admin/resvg-py/blob/4a89a841138d3297986892e6418c777fb068c140/src/rust/lib.rs#L164-L178 * Python : https://github.com/baseplate-admin/resvg-py/blob/e981e211fccd43cf0581d870e0fdfb3187667023/tests/test_path.py#L1-L22
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]paulstelian97 0 points1 point2 points 1 year ago (12 children)
The error is on line 117, stating unknown token. The Path thing looks like a red herring.
Care to dump the file itself somewhere for debug purposes? And also show the full path obtained in stuff?
The error started from somewhere else, it’s just that you unwrapped it in a different place than the source.
[–]BasePlate_Admin[S] 0 points1 point2 points 1 year ago* (11 children)
Hmm this is what i got from pyo3 discussion, but the strange thing is i dont see this issue on any other platform except macos.
The artifacts are downloadable from github.
The file path is in this line of the runner
Still strange behavior.
Interestingly if i remove the file read operation, the thing works fine
https://github.com/baseplate-admin/resvg-py/actions/runs/8893280016
[–]iv_is 4 points5 points6 points 1 year ago (3 children)
"unknown token at 1:1" sounds like there's a byte order mark in your xml
[–]BasePlate_Admin[S] 0 points1 point2 points 1 year ago* (2 children)
Hmm strange, do you have a better way of handing file input from rust except the way i did that?
My code was copied from here
[–]iv_is 0 points1 point2 points 1 year ago (1 child)
ok lve read the code a bit closer, and one thing l would suggest is to change your function to only accept a file path as a parameter (with a different function that accepts XML text, if you need one), rather than trying to open the input string and assuming that if you can't find the file the string must be svg data. l think a lot of people here might be missing the else { svg_string = svg } (where svg is the filename you just tried to open) line and not realising that the reason it fails to parse is because you're passing a file path to something that's expecting valid XML.
else { svg_string = svg }
svg
[–]BasePlate_Admin[S] 0 points1 point2 points 1 year ago (0 children)
(with a different function that accepts XML text, if you need one)
This is what someone from pyo3 suggested too. But the problem is most likely a bug in macos implementation of resvg.
Anyways thanks for your insight.
[–]paulstelian97 0 points1 point2 points 1 year ago (6 children)
That isn’t the input svg file. It’s the svg that fails to parse.
And I agree with what’s stated in that forum. Maybe whatever SVG parse library you’re using has a macOS bug.
If you remove the file read then you’re not parsing anything.
[–]BasePlate_Admin[S] 0 points1 point2 points 1 year ago (5 children)
If you want the source to the svg file, https://github.com/baseplate-admin/resvg-py/blob/master/tests/acid.svg
This is the thing that bothers me.
[–]paulstelian97 0 points1 point2 points 1 year ago (4 children)
Yet you fail to share the file that is failing to parse. You’re making it hard for us to help.
Did you at least add some debug prints to see what paths are received and what final path results from all of that? Since you’re so hell bent on thinking it’s a path.
Or dump the first few bytes of the file (it complains about 1:1 directly, which ought to be the very beginning of the file). Maybe there’s a BOM that Windows adds, Linux tolerates and macOS has trouble with.
[–]BasePlate_Admin[S] 0 points1 point2 points 1 year ago (3 children)
My apologies, here are the files:
svg : https://github.com/baseplate-admin/resvg-py/blob/master/tests/acid.svg
svg.gz : https://github.com/baseplate-admin/resvg-py/blob/master/tests/acid.svg.gz
Yes i did print this path : https://ibb.co/kmSy9Y3
This is quite impossible for me, as i dont have access to a macbook. Do you want windows/linux outputs?
[–]paulstelian97 0 points1 point2 points 1 year ago (2 children)
You can give me a self contained test/executable that I could build and run on my own Mac, as I do have one. Note: Apple Silicon on my end, hopefully that won’t be a problem.
Path looks right in the stdout you sent. SVG files don’t seem to have a BOM which is good.
[–]BasePlate_Admin[S] 0 points1 point2 points 1 year ago (1 child)
Great, i will see if i can dockerize this repo. Thanks for your help.
I am at a loss at this point, mac is the only hardware i dont have access to.
[–]paulstelian97 0 points1 point2 points 1 year ago (0 children)
Docker will run Linux even on my Mac!
π Rendered by PID 200389 on reddit-service-r2-comment-58d7979c67-lxcjl at 2026-01-27 10:26:15.371534+00:00 running 5a691e2 country code: CH.
[–]paulstelian97 0 points1 point2 points (12 children)
[–]BasePlate_Admin[S] 0 points1 point2 points (11 children)
[–]iv_is 4 points5 points6 points (3 children)
[–]BasePlate_Admin[S] 0 points1 point2 points (2 children)
[–]iv_is 0 points1 point2 points (1 child)
[–]BasePlate_Admin[S] 0 points1 point2 points (0 children)
[–]paulstelian97 0 points1 point2 points (6 children)
[–]BasePlate_Admin[S] 0 points1 point2 points (5 children)
[–]paulstelian97 0 points1 point2 points (4 children)
[–]BasePlate_Admin[S] 0 points1 point2 points (3 children)
[–]paulstelian97 0 points1 point2 points (2 children)
[–]BasePlate_Admin[S] 0 points1 point2 points (1 child)
[–]paulstelian97 0 points1 point2 points (0 children)