Py3 mode renames "str" type to "unicode" in auto-embedded signature
See original GitHub issueConsider this:
$ cat mylib.pyx
#cython: embedsignature=True
def listdir(str path):
'''Print out *path*'''
print(path)
$ cython --version
Cython version 0.19
$ cython mylib.pyx; grep 'Print out' mylib.c | head -1
static char __pyx_doc_5mylib_listdir[= "listdir(str path)\nPrint out *path*";
$ cython -3 mylib.pyx; grep 'Print out' mylib.c | head -1
static char __pyx_doc_5mylib_listdir[](]) = "listdir(unicode path)\nPrint out *path*";
In other words, when running with -3
, the str
type in the function signature becomes unicode
.
I guess the idea is that when compiled for Python 2, the extension will expect a unicode object. However, when compiled under Python 3 (which is probably the more likely case), the signature is just confusing as there is no unicode type.
Moreover, the guess that under Python 2.x the function expects an unicode object is not necessarily correct. In my case, the function truly expects str in both Python 2.x and 3.x. Therefore, I think it would be just not rename any types when generating the docstring signature.
Migrated from http://trac.cython.org/ticket/812
Issue Analytics
- State:
- Created 10 years ago
- Reactions:2
- Comments:10 (8 by maintainers)
Top Results From Across the Web
Unicode HOWTO — Python 3.11.1 documentation
Python's string type uses the Unicode Standard for representing characters, which lets Python programs work with all these different possible characters.
Read more >Unicode & Character Encodings in Python: A Painless Guide
Encoding and Decoding in Python 3. Python 3's str type is meant to represent human-readable text and can contain any Unicode character.
Read more >NameError: global name 'unicode' is not defined - in Python 3
Python 3 renamed the unicode type to str , the old str type has been replaced by bytes . if isinstance(unicode_or_str, str): text...
Read more >Porting to Python 3 | Django documentation
Python 2's unicode() type was renamed str() in Python 3, str() was renamed bytes(), and basestring() disappeared. six provides tools to deal with...
Read more >Byte string, Unicode string, Raw string — A Guide to all strings ...
However, do you know there are at least four types of… ... Byte string and Unicode String (default Python3 string) — It's all...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This bug also affects the
inspect.signature()
result:This seems to cause errors with py3 typing code that uses
str
as a type.Original source code (some YAML handling code):
Error at runtime:
The code runs under CPython3, but fails due to this rewriting. I’d be happy to send a PR up, though I can’t find where the str->unicode translation is happening. I looked at AutoDocTransforms though I don’t see how that does the translation, nor how I would use it to avoid the translation.