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

fileinput device or resource busy error #42514

Closed
meren mannequin opened this issue Oct 24, 2005 · 2 comments
Closed

fileinput device or resource busy error #42514

meren mannequin opened this issue Oct 24, 2005 · 2 comments
Assignees

Comments

@meren
Copy link
Mannequin

meren mannequin commented Oct 24, 2005

BPO 1336582
Nosy @malemburg, @birkenfeld

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 = 'https://github.com/malemburg'
closed_at = <Date 2006-02-19.09:52:12.000>
created_at = <Date 2005-10-24.17:03:30.000>
labels = ['expert-unicode']
title = 'fileinput device or resource busy error'
updated_at = <Date 2006-02-19.09:52:12.000>
user = 'https://bugs.python.org/meren'

bugs.python.org fields:

activity = <Date 2006-02-19.09:52:12.000>
actor = 'georg.brandl'
assignee = 'lemburg'
closed = True
closed_date = None
closer = None
components = ['Unicode']
creation = <Date 2005-10-24.17:03:30.000>
creator = 'meren'
dependencies = []
files = []
hgrepos = []
issue_num = 1336582
keywords = []
message_count = 2.0
messages = ['26687', '26688']
nosy_count = 3.0
nosy_names = ['lemburg', 'georg.brandl', 'meren']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1336582'
versions = []

@meren
Copy link
Mannequin Author

meren mannequin commented Oct 24, 2005

Fileinput module can't work with the unicode path names
properly.

Here is the reproduction of the error:

---------------8<---------------8<---------------8<---------------8<-------

meren@pardus /home/meren $ touch myfile
meren@pardus /home/meren $ python
>>> fn = u'/home/meren/myfile'
>>> type(fn)
<type 'unicode'>
>>> import fileinput
>>> for line in fileinput.input(fn, inplace = 1):
...     print line
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/fileinput.py", line 231, in next
    line = self.readline()
  File "/usr/lib/python2.3/fileinput.py", line 300, in
readline
    os.rename(self._filename, self._backupfilename)
OSError: [Errno 16] Device or resource busy
>>> 
--------------->8--------------->8--------------->8--------------->8

This is happening, because the value of the
self._filename variable is just the first character of
the fn: '/'.

The __init__ method of the FileInput class
(/usr/lib/python2.3/fileinput.py) is the responsible
one about this malfunction. In the __init__ function
it's checking the type of the 'files' variable (in this
case it is fn) for an iteration hack:

---------------8<---------------8<---------------8<---------------8<-------
(...)
def __init__(self, files=None, inplace=0,
backup="", bufsize=0):
if type(files) == type(''):
files = (files,)
(...)
--------------->8--------------->8--------------->8--------------->8-------

When the type of the 'files' variable is unicode, the
value of the 'files' variable is incohorently becoming
into this:

files = [u'/', u'h', u'o', u'm', u'e', u'/', u'm',
u'e', u'r', u'e' ....

consequently, python is trying to execute
os.rename('/', '/.bak') instead of
os.rename('/home/meren/myfile',
'/home/meren/myfile.bak'). After this function call os
module decides to throw an error, normally..

[1],
(...)
if isinstance(files, basestring):
files = (files,)
(...)

[2]
(...)
if type(files) == type(''):
files = (files,)
(...)

Checking the type of the variable in the __init__
function of the class like [1] is solving the problem
(now we are doing it like [2]).. Also, no backward
compatibility problem appearing with this replacement.

Here is a small patch to show replacement properly:

---------------8<---------------8<---------------8<---------------8<-------
--- fileinput.orig.py 2005-10-24 19:55:53.019413368 +0300
+++ fileinput.py 2005-10-24 19:56:19.590373968 +0300
@@ -184,7 +184,7 @@
"""

     def __init__(self, files=None, inplace=0,
backup="", bufsize=0):
-        if type(files) == type(''):
+        if isinstance(files, basestring):
             files = (files,)
         else:
             if files is None:
--------------->8--------------->8--------------->8--------------->8

and here is the result of the same operation after this
patch:

---------------8<---------------8<---------------8<---------------8<-------

(...)
    os.rename(self._filename, self._backupfilename)
OSError: [Errno 16] Device or resource busy
>>> reload(fileinput)
<module 'fileinput' from '/usr/lib/python2.3/fileinput.py'>
>>> for line in fileinput.input(fn, inplace = 1):
...     print line
...
>>>
--------------->8--------------->8--------------->8--------------->8

Sorry for the long explanation,
Thanks in advice..
Ciao.

@meren meren mannequin closed this as completed Oct 24, 2005
@meren meren mannequin assigned malemburg Oct 24, 2005
@meren meren mannequin closed this as completed Oct 24, 2005
@meren meren mannequin added the topic-unicode label Oct 24, 2005
@meren meren mannequin assigned malemburg Oct 24, 2005
@meren meren mannequin added the topic-unicode label Oct 24, 2005
@birkenfeld
Copy link
Member

Logged In: YES
user_id=1188172

Thanks for the report, applied your patch in rev. 42489, 42490.

@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
Projects
None yet
Development

No branches or pull requests

2 participants