Commit Graph

59 Commits

Author SHA1 Message Date
Petr Viktorin 2e74eec244 Python stable ABI: Adjust type usage & declarations
The previous commit removed all *_Type variables.
This adds them back, but as *pointers* to Python type objects
rather than type objects themselves.

All usages now need an extra pointer dereference (or removing an
address-of).
(The compiler will complain of type safety if we miss a spot,
so I'm OK using the original names.)

Also, add header declarations for the Spec structs.
2023-10-05 13:39:37 +03:00
Petr Viktorin 0d33ad8530 Python stable ABI: Change type definitions to heap type specs
In the stable ABI Python's type objects aren't defined in structs
that reference other structs, but by an array of "slots" from which
type objects are created.

(This commit was generated semi-automatically. The automation
is too messy and bespoke to share widely, but if you're interested
in undocumented work in progress, look in
https://github.com/encukou/api-limiter/tree/rpm-mess )
2023-10-05 13:39:37 +03:00
Petr Viktorin a532a0d8c2 Python stable ABI: Make calls to `tp_free` go through PyType_GetSlot
The Python type struct changes between versions, and thus direct access to
its fields is not allowed in Stable ABI.
The members can, however, be retrieved using `PyType_GetSlot`.

Commit generated with this semantic patch (see https://coccinelle.lip6.fr ):
```
@@
identifier s;
type t;
@@

- Py_TYPE(s)->tp_free((t*) s);
+ Py_TYPE(s)->tp_free(s);

@@
@@

- Py_TYPE(s)->tp_free(s);
+ freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
+ free(s);
```

applied using:

    spatch --sp-file patch.cocci --dir python/ --in-place
2023-10-05 13:39:37 +03:00
Panu Matilainen 67f8f2b01d Axe support for Python 2
Python 2 will be EOL by the time of the next major rpm release,
time to retire the Python 2 bindings. Specifically we require
Python >= 3.1 for surrogateescape-support.
2019-08-12 09:54:44 +03:00
Panu Matilainen 84920f8983 In Python 3, return all our string data as surrogate-escaped utf-8 strings
In the almost ten years of rpm sort of supporting Python 3 bindings, quite
obviously nobody has actually tried to use them. There's a major mismatch
between what the header API outputs (bytes) and what all the other APIs
accept (strings), resulting in hysterical TypeErrors all over the place,
including but not limited to labelCompare() (RhBug:1631292). Also a huge
number of other places have been returning strings and silently assuming
utf-8 through use of Py_BuildValue("s", ...), which will just irrevocably
fail when non-utf8 data is encountered.

The politically Python 3-correct solution would be declaring all our data
as bytes with unspecified encoding - that's exactly what it historically is.
However doing so would by definition break every single rpm script people
have developed on Python 2. And when 99% of the rpm content in the world
actually is utf-8 encoded even if it doesn't say so (and in recent times
packages even advertise themselves as utf-8 encoded), the bytes-only route
seems a wee bit too draconian, even to this grumpy old fella.

Instead, route all our string returns through a single helper macro
which on Python 2 just does what we always did, but in Python 3 converts
the data to surrogate-escaped utf-8 strings. This makes stuff "just work"
out of the box pretty much everywhere even with Python 3 (including
our own test-suite!), while still allowing to handle the non-utf8 case.
Handling the non-utf8 case is a bit more uglier but still possible,
which is exactly how you want corner-cases to be. There might be some
uses for retrieving raw byte data from the header, but worrying about
such an API is a case for some other rainy day, for now we mostly only
care that stuff works again.

Also add test-cases for mixed data source labelCompare() and
non-utf8 insert to + retrieve from header.
2019-02-22 20:37:20 +02:00
Panu Matilainen d5e4ef4d05 Dup the file descriptor when reopening an rpm.fd in python
- Avoids a segfault on closing in some circumstances
2013-12-20 12:52:51 +02:00
Panu Matilainen 6bd4a7475f Add .open() class method to rpm.fd
- This is simply equivalent of calling rpm.fd constructor, but
  might be somewhat more natural way of opening files
2013-12-19 15:44:47 +02:00
Panu Matilainen d02d053ebe Remember open-mode and flags in python rpm.fd's as properties 2013-12-19 12:58:52 +02:00
Panu Matilainen feafc9262e Support reopening an rpm.fd descriptor as well 2013-12-18 13:50:02 +02:00
Panu Matilainen ea89615cbc Honor mode and flags also when reopening an fd in python rpm.fd 2013-12-18 13:48:35 +02:00
Panu Matilainen f220f627f6 Construct mode as understood by librpmio in rpmfd constructor already
- No functional change here, but we'll need this for the next step
2013-12-18 13:42:43 +02:00
David Malcolm 5fc863ee1e Fix python binding build on python 3
- Fixes PyString caused breakage from commit
  cb84c0db42

Signed-off-by: Panu Matilainen <pmatilai@redhat.com>
2013-04-10 09:47:11 +03:00
David Malcolm fdba253885 fix use-after-free within rpmfdFromPyObject's error-handling
These lines within python/rpmfd-py.c: rpmfdFromPyObject
are the wrong way around:

	Py_DECREF(fdo);
 	PyErr_SetString(PyExc_IOError, Fstrerror(fdo->fd));

If fdo was allocated by the call above to PyObject_CallFunctionObjArgs,
it may have an ob_refcnt == 1, and thus the Py_DECREF() frees it, so
fdo->fd is reading from deallocated memory.

Signed-off-by: Ales Kozumplik <akozumpl@redhat.com>
2012-01-02 08:39:17 +01:00
David Malcolm a9e6f2ab9d mark strings extracted from PyArg_Parse* as "const"
- Various places within the bindings use PyArg_ParseTuple[AndKeywords] to
  extract (char*) string arguments. These are pointers to the internal
  representation of a PyStringObject, and shouldn't be modified, hence
  it's safest to explicitly mark these values as (const char*), rather
  than just (char*).

Signed-off-by: Ales Kozumplik <akozumpl@redhat.com>
2011-12-22 10:49:56 +01:00
David Malcolm 59807943af fix memory leaks in invocations of PyObject_Call
- Various functions in the Python bindings have expressions of the form:

  PyObject_Call(callable,
                  Py_BuildValue(fmtstring, ...), NULL);

  This leaks memory for the case when Py_BuildValue succeeds (it returns a
  new reference, which is never freed; PyObject_Call doesn't steal the
  reference): the argument tuple and all of its components will not be
  freed (until the process exits).

Signed-off-by: Ales Kozumplik <akozumpl@redhat.com>
2011-12-21 08:45:52 +01:00
Panu Matilainen eb94de6394 Teach python rpmio bindings about Fdescr()
- Similarly to python file object having o.name, export Fdescr()
  as fd.name. Python uses <foo> for non-paths but [foo] seems like
  a safer choice wrt accidental redirections.
- Also add a basic testcase for fd.name
2011-07-07 16:31:46 +03:00
Panu Matilainen f4a0788484 Fix/sanitize rpm.fd python object creation
- FD's are not really immutable, move the initialization work into
  tp_init and use PyType_GenericNew for tp_new since we're not
  doing anything special there.
- Remove half a dozen different unnecessary exit points
2011-07-01 12:12:41 +03:00
Panu Matilainen cb84c0db42 Fix the totally broken rpm.fd() read method 2011-07-01 11:20:15 +03:00
Panu Matilainen d69ebf18f1 Throw an exception from Fseek() errors in python rpmfd.seek() 2011-03-04 14:43:54 +02:00
Panu Matilainen 7796da46f5 Oops, missing include in previous commit 2010-11-23 17:42:57 +02:00
Panu Matilainen 8093218e8c Permit unicode paths in rpm.fd() (RhBug:654145)
- Python 3 has fs-specific converter function, for Python 2 just
  assume utf-8 and hope the best.
2010-11-23 12:33:09 +02:00
Panu Matilainen 16f9d7f4dc Remove unnecessary (cmpfunc) casts from our type objects
- cmpfunc is no more in Python 3 and casting NULL to anything makes
  little sense anyhow
2009-10-28 16:35:32 +02:00
Panu Matilainen a5e123bc3e Use PyObject_AsFileDescriptor() instead of object type comparison
- this permits any file-like object implementing .fileno() method
  (including rpm.fd) to be dup'ed, not just PyFile subtypes
- this also avoids yet another incompatibility with Python 3 which doesn't
  have PyFile_Check() and PyFile_AsFile() at all
2009-10-28 15:55:35 +02:00
Panu Matilainen 1866fc41c8 Replace PyString usage with PyBytes everywhere
- In Python 2.6 PyBytes is just an alias for PyString, Python 3.0
  removed PyString entirely
- Add compatibility defines for Python < 2.6
- Based on David Malcolm's Python 3.x efforts
2009-10-21 13:15:44 +03:00
David Malcolm 4b8e0ebde6 Generalize type object initialization to work with both Python 2.* and Python 3.*
The layout of PyVarObject changed between python 2 and python 3, and this leads
to the existing code for all of the various PyTypeObject initializers failing to
compile with python 3

Change the way we initialize these structs to use PyVarObject_HEAD_INIT directly,
rather than merely PyObject_HEAD_INIT, so that it compiles cleanly with both major
versions of Python
2009-10-19 11:02:13 +03:00
David Malcolm 7b51c4a1eb Generalize access to ob_type so that they work with both Python 2.* and Python 3.*
Python 2's various object structs use macros to implement common fields at the top of each
struct.

Python 3's objects instead embed a PyObject struct as the first member within the more refined
object structs.

Use the Py_TYPE() macro when accessing ob_type in order to encapsulate this difference.
2009-10-19 10:50:24 +03:00
Panu Matilainen ed40ff253a Add tell() method to rpm.fd class 2009-10-15 11:34:26 +03:00
Panu Matilainen 410bf68fd4 Accept rpm.fd() types file objects everywhere in python bindings
- turn rpmfdFromPyObject() into a python-level object converter, add
  a separate C-level getter for the fd pointer itself
- take advantage of python refcounting to handle differences between
  native vs converted rpm.fd in callers so we can simply decref the
  rpmfdFromPyObject() result without having to worry whether it was
  converted or not (ie should we close it or not)
2009-10-15 10:11:37 +03:00
Panu Matilainen aac3cb417c Add first rough-cut python bindings for rpmio fd
- attempt to mimic python file object where possible, but nowhere near
  all methods are supported, either just not yet done or due to
  underlying limitations
2009-10-14 14:14:51 +03:00
Panu Matilainen ac219c129d Turn rpmFdFromPyObject() into an object converter interface
- permits direct validation and conversion from arg parsing
2009-09-23 12:05:02 +03:00
Panu Matilainen 79e87e5ea1 Add helper to convert python file objects to rpmio FD_t type 2009-09-23 11:09:19 +03:00
Panu Matilainen 2b06ca079c Lose the utterly broken rpmio FD_t wrappers 2009-09-22 16:39:39 +03:00
Panu Matilainen ae1e7d8389 We dont support ancient python versions... 2009-09-22 16:28:58 +03:00
Panu Matilainen 7be0a763d2 Lone module-private const .. remove 2007-12-16 22:50:30 +02:00
Panu Matilainen 9ce13e09ef Switch to <rpm/foo.h> style for public headers
- adjust include paths accordingly
2007-12-08 14:02:32 +02:00
Panu Matilainen dbdbe8010c Eliminate debug junk from the API
- add some helper macros to accomplish the same, easy to enable / disable
  as needed
2007-12-04 11:25:46 +02:00
Ralf Corsépius abeea80a38 Use #include <x.h> syntax to include public headers. 2007-11-23 06:46:19 +01:00
Panu Matilainen 1fe38d71cc Use fdGetFILE() instead of rpmio internal fdGetFp() everywhere 2007-10-29 09:42:21 +02:00
Panu Matilainen fda6c48f00 Remove bunch of unnecessary rpmio_internal & related includes from python 2007-10-23 09:50:23 +03:00
Ralf Corsépius 3aad15624e Remove split tags. 2007-09-11 15:28:26 +02:00
Panu Matilainen f91bb73993 Fix bunch of compiler warnings from python bindings (method pointer types) 2007-07-10 15:19:04 +03:00
pauln 24e6f8633e Add kwargs everywhere - courtesy of pjones@redhat.com
CVS patchset: 7582
CVS date: 2004/11/17 17:05:39
2004-11-17 17:05:39 +00:00
jbj b8988af3c5 Build with gcc-2.95.3 and python-2.2, remove incompat CFLAGS and casts.
CVS patchset: 7082
CVS date: 2004/01/22 14:28:08
2004-01-22 14:28:08 +00:00
jbj 0deeb05eed - python: include Python.h before glibc features.h.
CVS patchset: 7055
CVS date: 2004/01/01 16:34:10
2004-01-01 16:34:10 +00:00
jbj a3bf9b96f9 - use -fPIC -DPIC on all platforms, not just mandatory (#112713).
CVS patchset: 7051
CVS date: 2003/12/30 15:12:50
2003-12-30 15:12:50 +00:00
jbj 5e20d90422 splint fiddles, no warnings.
CVS patchset: 6961
CVS date: 2003/12/01 19:15:38
2003-12-01 19:15:38 +00:00
jbj 3dcfeb3f9a Merge changes from rpm-4.2.1 development.
CVS patchset: 6959
CVS date: 2003/11/23 19:50:52
2003-11-23 19:50:52 +00:00
jbj 3db8afedc0 Use getattro/setattro throughout.
Convert to mpfprintlin from diddled mpprintln.

CVS patchset: 6811
CVS date: 2003/05/04 17:34:53
2003-05-04 17:34:53 +00:00
jbj 1b00e0031d Generate random primes method.
CVS patchset: 6807
CVS date: 2003/05/02 21:05:46
2003-05-02 21:05:46 +00:00
jbj c6156e0abd Doxygen doco markup.
CVS patchset: 6760
CVS date: 2003/04/17 17:17:27
2003-04-17 17:17:27 +00:00