non-integer index slicing failure in Python 3.2

Issue #103 resolved
Pavel Vondřička created an issue

A problem caused by calling the function "cqi_list_corpora()" from the "rcqp" library for R:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.2/dist-packages/rpy2/robjects/vectors.py", line 303, in __repr__
    self.__repr_content__()
  File "/usr/local/lib/python3.2/dist-packages/rpy2/robjects/vectors.py", line 292, in __repr_content__
    ', '.join((p_str(x, max_width = 52 / l) for x in self[ : 8])) +\
  File "/usr/local/lib/python3.2/dist-packages/rpy2/robjects/vectors.py", line 292, in <genexpr>
    ', '.join((p_str(x, max_width = 52 / l) for x in self[ : 8])) +\
  File "/usr/local/lib/python3.2/dist-packages/rpy2/robjects/vectors.py", line 286, in p_str
    res = "%s..." %x[ : (max_width - 3)]
TypeError: slice indices must be integers or None or have an __index__ method

A solution seems to be using int() either on line 293 in vectors.py:

', '.join((p_str(x, max_width = int(52 / l)) for x in self[ : 8])) +\

or on line 286:

res = "%s..." %x[ : (int(max_width) - 3)]

Comments (10)

  1. Pavel Vondřička reporter

    Reproduce easilly even in Python 3.3:

    Python 3.3.0 (default, Sep 29 2012, 17:14:58) 
    [GCC 4.7.2] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from rpy2.robjects import r
    >>> r('c("X", "XXXXXXXXXX", "XXXXXXXXXX", "XXXXXXXXXXX")')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.3/dist-packages/rpy2/robjects/vectors.py", line 307, in __repr__
        self.__repr_content__()
      File "/usr/local/lib/python3.3/dist-packages/rpy2/robjects/vectors.py", line 296, in __repr_content__
        ', '.join((p_str(x, max_width = 52 / l) for x in self[ : 8])) +\
      File "/usr/local/lib/python3.3/dist-packages/rpy2/robjects/vectors.py", line 296, in <genexpr>
        ', '.join((p_str(x, max_width = 52 / l) for x in self[ : 8])) +\
      File "/usr/local/lib/python3.3/dist-packages/rpy2/robjects/vectors.py", line 290, in p_str
        res = "%s..." %x[ : (max_width - 3)]
    TypeError: slice indices must be integers or None or have an __index__ method
    
  2. Laurent Gautier

    I can reproduce as well with rpy2-2.3.0alpha1, Python 3.3, and R-2.15.1. No problem with Python 2.7.

  3. Grayson Badgley

    Even with the new code I am running into the same problem -- using forecast package.

    Out[59]: ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-59-acdffb2774ed> in <module>()
    ----> 1 rpy2.robjects.r('dshw(myX,24,168)')
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/IPython/core/displayhook.pyc in __call__(self, result)
        236             self.start_displayhook()
        237             self.write_output_prompt()
    --> 238             format_dict = self.compute_format_data(result)
        239             self.write_format_data(format_dict)
        240             self.update_user_ns(result)
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/IPython/core/displayhook.pyc in compute_format_data(self, result)
        148             MIME type representation of the object.
        149         """
    --> 150         return self.shell.display_formatter.format(result)
        151 
        152     def write_format_data(self, format_dict):
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/IPython/core/formatters.pyc in format(self, obj, include, exclude)
        124                     continue
        125             try:
    --> 126                 data = formatter(obj)
        127             except:
        128                 # FIXME: log the exception
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
        445                 type_pprinters=self.type_printers,
        446                 deferred_pprinters=self.deferred_printers)
    --> 447             printer.pretty(obj)
        448             printer.flush()
        449             return stream.getvalue()
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
        358                             if callable(meth):
        359                                 return meth(obj, self, cycle)
    --> 360             return _default_pprint(obj, self, cycle)
        361         finally:
        362             self.end_group()
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _default_pprint(obj, p, cycle)
        478     if getattr(klass, '__repr__', None) not in _baseclass_reprs:
        479         # A user-provided repr.
    --> 480         p.text(repr(obj))
        481         return
        482     p.begin_group(1, '<')
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/rpy2/robjects/vectors.py in __repr__(self)
        510             for i, x in enumerate(self):
        511                 if isinstance(x, ListVector):
    --> 512                     res.append(super(ListVector, self).__repr__())
        513                 else:
        514                     try:
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/rpy2/robjects/vectors.py in __repr__(self)
        304     def __repr__(self):
        305         return super(Vector, self).__repr__() + os.linesep + \
    --> 306             self.__repr_content__()
        307 
        308 #name alias if Python 3 (iteritems no longer existing for dict objects)
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/rpy2/robjects/vectors.py in __repr_content__(self)
        293         if l < 7:
        294             s = '[' + \
    --> 295                 ', '.join((p_str(x, max_width = math.floor(52 / l)) for x in self[ : 8])) +\
        296                 ']'
        297         else:
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/rpy2/robjects/vectors.py in <genexpr>((x,))
        293         if l < 7:
        294             s = '[' + \
    --> 295                 ', '.join((p_str(x, max_width = math.floor(52 / l)) for x in self[ : 8])) +\
        296                 ']'
        297         else:
    
    /Users/grayson/.virtualenvs/pandas-0.8.2-dev/lib/python2.7/site-packages/rpy2/robjects/vectors.py in p_str(x, max_width)
        287                     res = x
        288                 else:
    --> 289                     res = "%s..." % (x[ : (max_width - 3)])
        290             return res
        291 
    
  4. Laurent Gautier

    Different problem. Now that's an issue when calling repr() on what dshw () is returning. For instance the following will not generate an error:

    x = rpy2.robjects.r('dshw(myX,24,168)')
    

    Anyway, this is fixed the branches default and version_2.3.x.

  5. Log in to comment