[issue36897] shlex doesn't differentiate escaped characters in output

2019-05-12 Thread Matthew Gamble


New submission from Matthew Gamble :

The output of the following invocations are exactly the same:

list(shlex.shlex('a ; b', posix=True, punctuation_chars=True))

list(shlex.shlex('a \; b', posix=True, punctuation_chars=True))

They both output the following:

['a', ';', 'b']

This makes it impossible to determine when the user wanted to escape the 
semi-colon for some reason, such as if they were using find's `-exec` argument.

--
components: Library (Lib)
messages: 342276
nosy: Matthew Gamble
priority: normal
severity: normal
status: open
title: shlex doesn't differentiate escaped characters in output
type: behavior
versions: Python 3.6, Python 3.7

___
Python tracker 
<https://bugs.python.org/issue36897>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36897] shlex doesn't differentiate escaped characters in output

2019-05-13 Thread Matthew Gamble


Matthew Gamble  added the comment:

The point is that it's not possible to use the output of shlex.shlex to try to 
match the behaviour of a POSIX-compliant shell by reliably splitting up a 
user's input into multiple commands. In the first case I presented (no escape 
character), the user entered two commands. In the second case, the user entered 
a single command with two arguments. However, there's no way to differentiate 
the two situations based on the output of shlex.

It's also worth noting that the output is the same with this too:

list(shlex.shlex('a \\; b', posix=True, punctuation_chars=True))

I tested this code on python 3.6.7 and 3.7.2, and didn't see any deprecation 
warnings at all. I also checked the history of shlex.py:

https://github.com/python/cpython/commits/master/Lib/shlex.py

The last commit was from 2017, and I don't see any usages of DeprecationWarning 
inside that file. I'm also not sure how r-strings are relevant, as I don't see 
any regular expressions used inside of the shlex class.

--

___
Python tracker 
<https://bugs.python.org/issue36897>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36897] shlex doesn't differentiate escaped characters in output

2019-05-13 Thread Matthew Gamble


Matthew Gamble  added the comment:

My apologies, I didn't realise you were talking about the invalid escape 
sequence. Thanks for letting me know about the fact that it's deprecated, I'll 
definitely be keeping that in mind going forward.

In a bash shell with the find command available, run the following command:

find . -type f -exec ls {} \;

You should see a list of files.

If you run this:

find . -type f -exec ls {} ;

You should see an error message from find:

"find: missing argument to `-exec'"

If I pass the first example in this message to shlex, I get no indication that 
the user attempted escaped the semi-colon in their input.

--

___
Python tracker 
<https://bugs.python.org/issue36897>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6818] remove/delete method for zipfile/tarfile objects

2015-04-09 Thread Matthew Gamble

Matthew Gamble added the comment:

Hi,

I've recently been working on a Python module for the Adobe universal container 
format (UCF) which extends the zip specification - as part of this I wanted to 
be able to remove and rename files in an archive.

I discovered this issue when writing the module so realised there wasn't 
currently a solution - so I went down the rabbit hole.

I've attached a patch which supports the removal and renaming of files in a zip 
archive. You can also look at this python module in a git-repo which is a the 
same code but separated out into a class that extends ZipFile: 
https://github.com/gambl/zipextended.

The patch provides 4 main new "public" functions for the zipfile library:

- remove(self, zinfo_or_arcname):
- rename(self, zinfo_or_arcname, filename):
- commit(self):
- clone(self, file, filenames_or_infolist=None, ignore_hidden_files=False)

The patch is in part modelled on the rubyzip solution. Remove and rename will 
initially only update the ZipFile's infolist. Changes are then persisted via a 
commit function which can be called manually - or will be called automatically 
upon close. Commit will then clone the zipfile with the necessary changes to a 
temporary file and replace the original file when that operation has completed 
successfully.

An alternative to remove files without modifying the original is via the clone 
method directly. This is in the spirit of Serhiy's suggestion of filtering the 
content and not modifying the original. You can pass a list of filenames or 
fileinfos of the files to be included in the clone.
So that clone can be performed without decompressing and then recompressing the 
files in the archive I have added two functions write_compressed and 
read_compressed.

I have also attempted to address Serhiy's concern with respect to the 
tricky.zip - "hidden files" in between members of the archive. The clone method 
will by default retain any hidden files and maintain the same relative order in 
the archive. You can also elect to ignore the hidden files, and clone with just 
the files listed in the central directory.

I did have to modify the tricky.zip attached to this issue manually as the CRC 
of file two (with file three embedded) was incorrect - and would therefore fail 
testzip(). I'm not actually sure how one would create such an archive - but I 
think that it's valid according to the zip spec. I've actually included the 
modified version in the patch for a few of the tests.

I appreciate that this is a large-ish patch and may take some time to review - 
but as suggested in the comments - this wasn't as straight forward as is seems!

Look forward to your comments. 

The signatures of the main functions are described below:

remove(self, zinfo_or_arcname):

Remove a member from the archive.

Args:
  zinfo_or_arcname (ZipInfo, str) ZipInfo object or filename of the
member.

Raises:
  RuntimeError: If attempting to modify an Zip archive that is closed.
---

rename(self, zinfo_or_arcname, filename):

Rename a member in the archive.

Args:
  zinfo_or_arcname (ZipInfo, str): ZipInfo object or filename of the
member.
  filename (str): the new name for the member.

Raises:
  RuntimeError: If attempting to modify an Zip archive that is closed.


clone(self, file, filenames_or_infolist=None, ignore_hidden_files=False):

Clone the a zip file using the given file (filename or filepointer).

Args:
  file (File, str): file-like object or filename of file to write the
new zip file to.
  filenames_or_infolist (list(str), list(ZipInfo), optional): list of
members from this zip file to include in the new zip file.
  ignore_hidden_files (boolean): flag to indicate wether hidden files
(data inbetween managed memebers of the archive) should be included.

Returns:
A new ZipFile object of the cloned zipfile open in append mode.

If copying hidden files then clone will attempt to maintain the
relative order between the files and members in the archive

commit(self):
 Commit any inline modifications (removal and rename) to the zip archive.

 This makes use of a temporary file to create a new zip archive with the
 required modifications and then replaces the original.

 This therefore requires write access to either the directory where the
 original zipfile lives, or to python's default tempfile location.

--
nosy: +gambl
Added file: http://bugs.python.org/file38878/zipfile.remove_rename.patch

___
Python tracker 
<http://bugs.python.org/issue6818>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6818] remove/delete method for zipfile/tarfile objects

2015-04-09 Thread Matthew Gamble

Changes by Matthew Gamble :


Added file: http://bugs.python.org/file38879/zip_hiddenfiles.zip

___
Python tracker 
<http://bugs.python.org/issue6818>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com