473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help! File objects and scoping

I'm trying to return an 'mmap' object from a function. The return
works, but some of the object's methods crash. Here are two examples

doesntwork.py
---------------------------
import mmap
import os

name="/any/real/file/on/my/hard/drive"

def getfile(name):
somefile=file(name, 'r+b')
mmapped = mmap.mmap(fileno=somefile.fileno(), \
length=os.path.getsize(name))
return mmapped

mmapped=getfile(name)
mmapped.seek(1) #this works, so mmapped is a working mmap object
print mmapped.size() #this dies with an error
mmapped.close()
--------------------------

doesntwork.py dies with the error:
EnvironmentError: [Errno 9] Bad file descriptor

On the other hand,
doeswork.py
---------------------------
import mmap
import os

name="/the/exact/same/file"

somefile=file(name, 'r+b')
mmapped = mmap.mmap(fileno=somefile.fileno(), \
length=os.path.getsize(name))

mmapped.seek(1) #works fine
print mmapped.size() #works fine, prints out the right number
mmapped.close()
--------------------------

The only difference between doeswork and doesntwork is whether 'mmapped'
is created in the global scope or created in a local scope and returned.
Why does that make a difference?

The strangest thing about this, I think, is that the error printed out
above, EnvironmentError, is listed in the docs under "only used as base
classes for other exceptions". In other words, it's never supposed to
be thrown.

I'll take any advice you can give me.
Thanks,
Ben Schwartz
www.mit.edu/~bens/
Jul 19 '05 #1
3 1416
bens wrote:
I'm trying to return an 'mmap' object from a function. The return
works, but some of the object's methods crash. Here are two examples

doesntwork.py
---------------------------
import mmap
import os

name="/any/real/file/on/my/hard/drive"

def getfile(name):
somefile=file(name, 'r+b')
mmapped = mmap.mmap(fileno=somefile.fileno(), \
length=os.path.getsize(name))
return mmapped

mmapped=getfile(name)
mmapped.seek(1) #this works, so mmapped is a working mmap object
print mmapped.size() #this dies with an error
mmapped.close()
--------------------------

doesntwork.py dies with the error:
EnvironmentError: [Errno 9] Bad file descriptor
My guess is that when getfile() returns, somefile goes out of scope and the actual file is closed. Try returning both somefile and mmapped or some other way of keeping a reference to someefile.

Kent
On the other hand,
doeswork.py
---------------------------
import mmap
import os

name="/the/exact/same/file"

somefile=file(name, 'r+b')
mmapped = mmap.mmap(fileno=somefile.fileno(), \
length=os.path.getsize(name))

mmapped.seek(1) #works fine
print mmapped.size() #works fine, prints out the right number
mmapped.close()
--------------------------

The only difference between doeswork and doesntwork is whether 'mmapped'
is created in the global scope or created in a local scope and returned.
Why does that make a difference?

The strangest thing about this, I think, is that the error printed out
above, EnvironmentError, is listed in the docs under "only used as base
classes for other exceptions". In other words, it's never supposed to
be thrown.

I'll take any advice you can give me.
Thanks,
Ben Schwartz
www.mit.edu/~bens/

Jul 19 '05 #2
bens wrote:
I'm trying to return an 'mmap' object from a function. The return
works, but some of the object's methods crash. Here are two examples

doesntwork.py
---------------------------
import mmap
import os

name="/any/real/file/on/my/hard/drive"

def getfile(name):
somefile=file(name, 'r+b')
mmapped = mmap.mmap(fileno=somefile.fileno(), \
length=os.path.getsize(name))
return mmapped


Here's the problem: the "mmap" object doesn't have a reference to
"somefile", only to its file descriptor number. So, when getfile() goes
out of scope, there are no references left, and the file gets closed
automatically.

One way to avoid this problem is to return the file object as well as
the "mmap" object, ie.:

def getfile(name):
somefile = file(name, 'r+b')
mmapped = mmap.mmap(fileno=somefile.fileno(),
length=os.path.getsize(name))
return (somefile, mmapped)

(somefile, mmapped) = getfile(name)

# do something with mmapped...

Now, as long as you keep "somefile" in scope, the file won't get closed.
You could also build an object and make both "somefile" and "mmapped"
attributes of this object.

Caveat: There may be a better way of doing this. I've never used "mmap".

Dave
Jul 19 '05 #3
Dave Benjamin wrote:
Here's the problem: the "mmap" object doesn't have a reference to
"somefile", only to its file descriptor number. So, when getfile() goes
out of scope, there are no references left, and the file gets closed
automatically.


That makes a lot of sense. It's now how I would have built mmap, but I
can live with it.

Many thanks,
Ben
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: python-help-bounces | last post by:
Your message for python-help@python.org, the Python programming language assistance line, has been received and is being delivered. This automated response is sent to those of you new to...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
0
by: tbatwork828 | last post by:
If you were like me trying to figure out how to launch context sensitive help topic by the context id, here is the link: http://weblogs.asp.net/kencox/archive/2004/09/12/228349.aspx and if...
2
by: John Baker | last post by:
I find it highly annoying that MS Access tries to go online when I want to look at the help files. Is there a way to configure it so it just looks at my local helpfiles when I hit F1?
9
by: JJ | last post by:
Do you all use HTML help workshop to create your help system. I am finding it quite clumsy to use. Mayeb because I am not used to using it. Do any of you use any other techniques to create help...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.