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

[BUG] unexpected type Py_UCS4 and base type Python object for indexing #4829

Closed
NilsBrause opened this issue Jun 9, 2022 · 4 comments
Closed

Comments

@NilsBrause
Copy link

NilsBrause commented Jun 9, 2022

Hi!

When trying to cythonize the following source code using cythonize -3:

str(0)[0]

the following error is printed:

Traceback (most recent call last):
  File "/home/nils/.local/bin/cythonize", line 8, in <module>
    sys.exit(main())
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Build/Cythonize.py", line 224, in main
    cython_compile(path, options)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Build/Cythonize.py", line 68, in cython_compile
    ext_modules = cythonize(
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Build/Dependencies.py", line 1117, in cythonize
    cythonize_one(*args)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Build/Dependencies.py", line 1263, in cythonize_one
    result = compile_single(pyx_file, options, full_module_name=full_module_name)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/Main.py", line 576, in compile_single
    return run_pipeline(source, options, full_module_name)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/Main.py", line 504, in run_pipeline
    err, enddata = Pipeline.run_pipeline(pipeline, source)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/Pipeline.py", line 394, in run_pipeline
    data = run(phase, data)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/Pipeline.py", line 371, in run
    return phase(data)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/Pipeline.py", line 52, in generate_pyx_code_stage
    module_node.process_implementation(options, result)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/ModuleNode.py", line 206, in process_implementation
    self.generate_c_code(env, options, result)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/ModuleNode.py", line 518, in generate_c_code
    self.generate_module_init_func(modules[:-1], env, globalstate['init_module'])
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/ModuleNode.py", line 3101, in generate_module_init_func
    self.body.generate_execution_code(code)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 409, in generate_execution_code
    stat.generate_execution_code(code)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/Nodes.py", line 5700, in generate_execution_code
    self.expr.generate_evaluation_code(code)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/ExprNodes.py", line 834, in generate_evaluation_code
    self.generate_result_code(code)
  File "/home/nils/.local/lib/python3.10/site-packages/Cython/Compiler/ExprNodes.py", line 4213, in generate_result_code
    assert False, "unexpected type %s and base type %s for indexing" % (
AssertionError: unexpected type Py_UCS4 and base type Python object for indexing

To my understanding, str(0)[0] should evaluate to '0', which is the first character of the string representation of 0.
Surprisingly, I don't get any error using cythonize --3str or cythonize -2. It also works to first assign the result of str(0) to a variable:

a = str(0)
a[0]

Environment:

  • OpenSUSE Leap 15.3
  • Python 3.10
  • Cython 3.0.0a10
@matthewdeanmartin
Copy link

I got the same for Cython==3.0.8, windows 11, python 3.11.1.

I switched to --3str and now I get different errors on fstrings

return f"{self.prefix}-{value}"
               ^
helpers\requests.py:58:15: Cannot convert Unicode string to 'str' implicitly. This is not portable and requires explicit encoding.

@scoder
Copy link
Contributor

scoder commented Jan 31, 2024

Thanks for the reports, I could reproduce the original issue on the latest master. It's easy to fix there, because it's limited to Python 3.x. A backport to Cython 3.0 might be less easy.

@scoder scoder added this to the 3.1 milestone Jan 31, 2024
@scoder
Copy link
Contributor

scoder commented Jan 31, 2024

I switched to --3str and now I get different errors on fstrings

return f"{self.prefix}-{value}"
               ^
helpers\requests.py:58:15: Cannot convert Unicode string to 'str' implicitly. This is not portable and requires explicit encoding.

You probably declared your return type as str. However, f-strings always return unicode strings, not str (which is bytes in Py2). Declare the return type as unicode instead.

@numbworks
Copy link

numbworks commented Apr 16, 2024

Thank you @scoder for your hint! It solved the issue for me!

I used to get the same error message on the following method:

  def create_numbered_file_path(self, folder_path : str, number : int, extension : str) -> str:

      r'''Creates a numbered file path. Example: ("C:\\", 1, "html") => "C:\\1.html"'''

      file_name : str = f"{number}.{extension}"
      file_path : str = self.create_file_path(folder_path = folder_path, file_name = file_name)    

      return file_path

Using str() around the f-string solved the issue:

      # ...
      file_name : str = str(f"{number}.{extension}")
      # ...      

Information:

  • Windows 10
  • Python 3.12.1
  • cython-3.0.10
  • setuptools-69.2.0
  • Command: python demolibrary1_setup.py build_ext --inplace
  • Error Message (truncated):
...
    def create_numbered_file_path(self, folder_path : str, number : int, extension : str) -> str:

        r'''Creates a numbered file path. Example: ("C:\\", 1, "html") => "C:\\1.html"'''

        file_name : str = f"{number}.{extension}"
                          ^
------------------------------------------------------------

demolibrary1.py:18:26: Cannot convert Unicode string to 'str' implicitly. This is not portable and requires explicit encoding.
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants