Skip to content

std::string_view adds characters to strings in Python #19476

@jmcarcell

Description

@jmcarcell

Check duplicate issues.

  • Checked for duplicates

Description

With cppyy 3.5.0, having a std::string_view will append [N], with N being the number of characters of the string when printing a std::string_view.

There is s.data() that will give the expected string, but that means if getting the value from a function, I need to know if it's a string or a string view, while this is not needed in C++. I would say this is unexpected, I would expect comparisons and usage of std::string_view to be similar to std::string.

This seems not be an issue from cppyy standalone: wlav/cppyy#313 (comment)

Reproducer

import cppyy
cppyy.cppdef(
"""
std::string_view s = "Hello, World!";
""")

from cppyy.gbl import s
print(s)

The answer is "Hello, World!"[13]. But then, this means I can not compare a std::string_view to a string in python, because this will not work without the [N]:

print(s == "Hello, World!")  # Should print True, prints False
print(s == "Hello, World![13]")  # Also False
print(s == '"Hello, World!"[13]')  # Also False
print(str(s) == '"Hello, World!"[13]')  # This is True
cppyy.cppdef(
"""
bool is_equal_cpp = s == "Hello, World!";
"""
)
from cppyy.gbl import is_equal_cpp
print(is_equal_cpp)  # Should print True, prints True

ROOT version

   ------------------------------------------------------------------
  | Welcome to ROOT 6.36.02                        https://root.cern |
  | (c) 1995-2025, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Jul 26 2025, 08:01:37                 |
  | From tags/v6-36-02@v6-36-02                                      |
  | With g++ (GCC) 15.1.1 20250425                                   |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

Installation method

Built from source, Spack for the Key4hep stack, LCG stacks

Operating system

Linux, Alma 9, Arch Linux

Additional context

No response

Metadata

Metadata

Assignees

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions