Skip to content
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

Add support for UCS-2 strings #648

Merged
merged 4 commits into from
Feb 27, 2024
Merged

Conversation

nariman
Copy link
Contributor

@nariman nariman commented Jan 4, 2024

Trying to fix an issue, when py-spy would not parse a stack trace containing functions with cyrillic names.

(2, _) => {
// UCS2 strings aren't used internally after v3.3: https://www.python.org/dev/peps/pep-0393/
// TODO: however with python 2.7 they could be added with --enable-unicode=ucs2 configure flag.
// or with python 3.2 --with-wide-unicode=ucs2
Err(format_err!("ucs2 strings aren't supported yet!"))
},

Seems like UCS-2 strings are in use by some (maybe all?) pre-built Python binaries (tested on mcr.microsoft.com/devcontainers/rust:1-1-bullseye Dev Container, our company's CentOS environment, as well as my local environment with Python 3.7 and 3.11 versions installed using pyenv).

If there's a function with a UCS-2 encoded name on the first recorded stack trace, py-spy just fails.

Example
import time

def кириллица(seconds):
    time.sleep(seconds)

if __name__ == "__main__":
    кириллица(10)

Outputs (profile.svg will not be created in this case):

$ py-spy record -o profile.svg -- python3 tests/scripts/cyrillic.py 
Error: Failed to find a python interpreter in the .data section

If py-spy successfully started the recorder, we'll get a flamegraph built without stack traces with UCS-2 strings, so final data/graph will look strange or even misleading.

Example
import time

def function1(seconds):
    time.sleep(seconds)

def кириллица(seconds):
    time.sleep(seconds)

if __name__ == "__main__":
    function1(10)
    кириллица(10)

Outputs (profile.svg will be created):

$ py-spy record -o profile.svg -- python3 tests/scripts/cyrillic.py 
py-spy> Sampling process 100 times a second. Press Control-C to exit.


py-spy> Stopped sampling because process exited
py-spy> Wrote flamegraph data to 'profile.svg'. Samples: 12 Errors: 1025

Copy link
Owner

@benfred benfred left a comment

Choose a reason for hiding this comment

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

thanks for the change!

@benfred benfred merged commit 8dd5492 into benfred:master Feb 27, 2024
182 checks passed
@benfred benfred added the enhancement New feature or request label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants