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.

Title: 2to3 doesn't convert 'types.InstanceType' to 'object'
Type: Stage:
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.1
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, ericp, loewis
Priority: normal Keywords:

Created on 2010-03-22 22:45 by ericp, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg101543 - (view) Author: Eric Promislow (ericp) Date: 2010-03-22 22:45
Title should be self-explanatory.
msg101547 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-03-22 22:58
I don't think this conversion is correct. If there is a test for InstanceType, there is (IMO) a fifty-fifty-chance that it is there to distinguish old-style and new-style classes, so converting it to object is most likely to break the code.

People who actually want this conversion to happen could replace types.InstanceType with

  getattr(types, 'InstanceType', object)

or add

  from types import InstanceType
except ImportError:
  InstanceType = object

to the top of the module; this would then work for both 2.x and 3.x (assuming that replacing InstanceType with object is actually correct in the code in question).
msg101553 - (view) Author: Eric Promislow (ericp) Date: 2010-03-22 23:38
I'm working on a debugger, trying to identify instances of
old-style classes in Python 2, and any class in Python 3.
The getattr formulation will work, but because I already
need to maintain an "is_v3" flag, I might as well use it

As a side note, how are instances of new-style classes in
v2 categorized?  This v2 code prints 'None':

import types
def get_type_name(target_type):
  for t in dir(types):
    if target_type == getattr(types, t):
      return t

class C(object):

c = C()
print get_type_name(type(c))
msg101554 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-03-22 23:52
In a sense, *all* objects are instances of new-style classes in 2.x, including instances of old-style classes (which are instances of the InstanceType type, which is a type, and hence a new-style class).

You may want to look at the __flags__ property of the type object. If it is a heap type, there is a good chance that it was created through a class statement. Alternatively, you can look at the type's __module__, and find out whether the module is a Python module.
msg101601 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-03-23 21:18
Closing 2to3 request.
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52453
2010-03-23 21:18:50benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg101601

resolution: works for me
2010-03-22 23:52:26loewissetmessages: + msg101554
2010-03-22 23:38:02ericpsetmessages: + msg101553
2010-03-22 22:58:38loewissetnosy: + loewis
messages: + msg101547
2010-03-22 22:45:16ericpcreate