Skip to content

Instantly share code, notes, and snippets.

@julian-klode
Created March 31, 2025 16:16
Show Gist options
  • Save julian-klode/13781169a03c1badc630762c7ef22348 to your computer and use it in GitHub Desktop.
Save julian-klode/13781169a03c1badc630762c7ef22348 to your computer and use it in GitHub Desktop.
python/tarfile.cc | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/python/tarfile.cc b/python/tarfile.cc
index b87fa71a..75bdd21c 100644
--- a/python/tarfile.cc
+++ b/python/tarfile.cc
@@ -46,6 +46,9 @@ public:
// Set to true if an error occurred in the Python callback, or a file
// was too large to read in extractdata.
bool error;
+
+ // Set to true by default
+ bool extract_data;
// Place where the copy of the data is stored.
char *copy;
// The size of the copy
@@ -60,8 +63,8 @@ public:
virtual bool Process(Item &Itm,const unsigned char *Data,
unsigned long Size,unsigned long Pos);
#endif
- PyDirStream(PyObject *callback, const char *member=0) : callback(callback),
- py_data(0), member(member), error(false), copy(0), copy_size(0)
+ PyDirStream(PyObject *callback, const char *member=0, bool extract_data=true) : callback(callback),
+ py_data(0), member(member), error(false), extract_data(extract_data), copy(0), copy_size(0)
{
Py_XINCREF(callback);
}
@@ -75,7 +78,7 @@ public:
bool PyDirStream::DoItem(Item &Itm, int &Fd)
{
- if (!member || strcmp(Itm.Name, member) == 0) {
+ if (extract_data && (!member || strcmp(Itm.Name, member) == 0)) {
// Allocate a new buffer if the old one is too small.
if (Itm.Size > SIZE_MAX)
goto to_large;
@@ -405,23 +408,25 @@ static PyObject *tarfile_extractall(PyObject *self, PyObject *args)
}
static const char *tarfile_go_doc =
- "go(callback: callable[, member: str]) -> True\n\n"
+ "go(callback: callable[, member: str, extract_data: bool = True]) -> True\n\n"
"Go through the archive and call the callable 'callback' for each\n"
"member with 2 arguments. The first argument is the TarMember and\n"
"the second one is the data, as bytes.\n\n"
"The optional parameter 'member' can be used to specify the member for\n"
"which to call the callback. If not specified, it will be called for all\n"
"members. If specified and not found, LookupError will be raised.";
-static PyObject *tarfile_go(PyObject *self, PyObject *args)
+static PyObject *tarfile_go(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyObject *callback;
PyApt_Filename member;
- if (PyArg_ParseTuple(args,"O|O&",&callback, PyApt_Filename::Converter, &member) == 0)
+ int extract_data = 1;
+ const char * const keywords[] = {"callback", "member", "extract_data", nullptr};
+ if (PyArg_ParseTupleAndKeywords(args,kwargs,"O|O&$p", keywords, &callback, PyApt_Filename::Converter, &member, &extract_data) == 0)
return 0;
if (member && strcmp(member, "") == 0)
member = 0;
pkgDirStream Extract;
- PyDirStream stream(callback, member);
+ PyDirStream stream(callback, member, extract_data);
((PyTarFileObject*)self)->Fd.Seek(((PyTarFileObject*)self)->min);
bool res = GetCpp<ExtractTar*>(self)->Go(stream);
if (stream.error)
@@ -458,7 +463,7 @@ static PyObject *tarfile_extractdata(PyObject *self, PyObject *args)
static PyMethodDef tarfile_methods[] = {
{"extractdata",tarfile_extractdata,METH_VARARGS,tarfile_extractdata_doc},
{"extractall",tarfile_extractall,METH_VARARGS,tarfile_extractall_doc},
- {"go",tarfile_go,METH_VARARGS,tarfile_go_doc},
+ {"go",(PyCFunction)tarfile_go,METH_VARARGS|METH_KEYWORDS,tarfile_go_doc},
{NULL}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment