This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: int() half-accepts UserString
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: UserString can not be used as string in calls to C routines
View: 232493
Assigned To: Nosy List: chris.jerdonek, esc24, ezio.melotti, mark.dickinson, serhiy.storchaka
Priority: normal Keywords:

Created on 2012-12-24 20:15 by serhiy.storchaka, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg178096 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-12-24 20:15
In Python 3 int() accepts UserString argument without explicit base and reject it with explicit base.

>>> from collections import UserString
>>> int(UserString('100'))
100
>>> int(UserString('100'), 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() can't convert non-string with explicit base


In 2.7 the same behavior also for MutableString and bytearray.
msg178593 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-12-30 17:51
Not sure this is worth fixing, unless the fix is trivial.
msg178605 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-12-30 19:36
This is definitely non-trivial. 1-argument int() works with UserString because __int__() method is defined for UserString (as __float__(), as __complex__()). I.e. UserString looks as number-like for it. Unfortunately there is no way to make a class be string-like without inheriting it from str class.
msg225259 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-08-13 06:47
Looks as there is no easy fix of this issue. This issue is particular case of issue232493.
History
Date User Action Args
2022-04-11 14:57:39adminsetgithub: 60977
2014-08-13 06:47:14serhiy.storchakasetstatus: open -> closed
superseder: UserString can not be used as string in calls to C routines
messages: + msg225259

resolution: duplicate
stage: needs patch -> resolved
2012-12-30 19:36:27serhiy.storchakasetmessages: + msg178605
2012-12-30 17:51:48ezio.melottisetmessages: + msg178593
2012-12-25 20:48:05mark.dickinsonsetnosy: + mark.dickinson
2012-12-25 08:36:50esc24setnosy: + esc24
2012-12-24 20:22:45chris.jerdoneksetnosy: + chris.jerdonek
2012-12-24 20:18:07ezio.melottisetnosy: + ezio.melotti
2012-12-24 20:15:15serhiy.storchakacreate