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

file.tell() returns Long usually, Int if subclassed #46864

Closed
esreverotua mannequin opened this issue Apr 11, 2008 · 4 comments
Closed

file.tell() returns Long usually, Int if subclassed #46864

esreverotua mannequin opened this issue Apr 11, 2008 · 4 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@esreverotua
Copy link
Mannequin

esreverotua mannequin commented Apr 11, 2008

BPO 2612
Nosy @birkenfeld, @benjaminp

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-04-11.02:58:39.108>
created_at = <Date 2008-04-11.02:45:14.424>
labels = ['invalid', 'type-bug', 'library']
title = 'file.tell() returns Long usually, Int if subclassed'
updated_at = <Date 2008-04-11.11:57:17.652>
user = 'https://bugs.python.org/esreverotua'

bugs.python.org fields:

activity = <Date 2008-04-11.11:57:17.652>
actor = 'benjamin.peterson'
assignee = 'none'
closed = True
closed_date = <Date 2008-04-11.02:58:39.108>
closer = 'benjamin.peterson'
components = ['Library (Lib)']
creation = <Date 2008-04-11.02:45:14.424>
creator = 'esrever_otua'
dependencies = []
files = []
hgrepos = []
issue_num = 2612
keywords = []
message_count = 4.0
messages = ['65331', '65333', '65334', '65349']
nosy_count = 3.0
nosy_names = ['georg.brandl', 'esrever_otua', 'benjamin.peterson']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2612'
versions = ['Python 2.4']

@esreverotua
Copy link
Mannequin Author

esreverotua mannequin commented Apr 11, 2008

Compare:

>>> class y(file):
...     def __init__(self, name):
...         file.__init__(self, name)
...     def __len__(self):
...         self.seek(0, 2)
...         return self.tell()
... 
>>> n = y('/tmp/longfile')
>>> len(n)
364773376

Versus:

>>> class z:
...     def __init__(self, name):
...         self.f = file(name, 'r')
...     def __len__(self):
...         self.f.seek(0, 2)
...         return self.f.tell()
... 
>>> x = z('/tmp/longfile')
>>> len(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: __len__() should return an int

Because:

>>> x = file('/tmp/longfile')
>>> type(x.tell())
<type 'long'>

@esreverotua esreverotua mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 11, 2008
@benjaminp
Copy link
Contributor

This is because y is a new-style class (because file is) and z is not.
If the return type of new-style classe's len is not an int, Python tries
to convert it, which is why that works for the first example. However,
that conversion doesn't happen in old-style classes. Try:

>>> class z(object):
...     def __init__(self, name):
...         self.f = file(name, 'r')
...     def __len__(self):
...         self.f.seek(0, 2)
...         return self.f.tell()
>>> x = z('/tmp/longfile')
>>> len(x)
[Whatever it is]

@esreverotua
Copy link
Mannequin Author

esreverotua mannequin commented Apr 11, 2008

Thanks Benjamin, that is a very interesting feature that I can't find
documented anywhere. Is there perhaps a documentation bug that can arise
from this? There are various places where the differences between old
and new-style classes are documented to a greater or lesser degree, eg:
http://www.python.org/download/releases/2.2.3/bugs/
Is this documented somewhere currently that I missed, or could it be
incorporated somewhere?

thanks,
D

@benjaminp
Copy link
Contributor

The documentation is still not very good about documenting the
differences between new and old style classes. Perhaps this is something
which could go under the __len__ entry in "Special Method Names."

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant