Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Prev Previous commit
Next Next commit
Added an zipimporter doc and some changes.
  • Loading branch information
AraHaan committed Nov 20, 2017
commit 3df3f0907bca5bcee3e739e847e954948022d5e8
32 changes: 32 additions & 0 deletions Doc/c-api/zipimporter.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.. highlightlang:: c

.. zipimporter:

ZipImporter Type Object
-----------------------

Python for the longest of time had an zipimporter that could not be
subclassed using the C Python API but could in the Python Layer.
Since Python 3.7 users can now subclass it in their C code as now
it is an exported Type in the Python Core.

.. c:var:: PyTypeObject PyZipImporter_Type

This instance of :c:type:`PyTypeObject` represents the Python zipimporter type;
it is the same object as :class:`zipimport.zipimporter` in the Python layer.

.. note::

This type used to be named ZipImporter_Type and not exported. Since then it
caused problems with subtyping it in another type forcing them to copy most
of the implementation from zipimport.c to their extension module which can
not be a reasonable thing. So this change was needed.


Type check macros
^^^^^^^^^^^^^^^^^

.. c:function:: int PyZipImporter_Check(PyObject *o)

Return true if the object *o* is a zipimporter type or an instance of a
subtype of the zipimporter type.
16 changes: 2 additions & 14 deletions Include/zipimport.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,9 @@
extern "C" {
#endif

/* zipimporter object definition and support */
PyAPI_DATA(PyTypeObject) PyZipImporter_Type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the rest of the file shouldn't be part of the stable ABI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then how would someone be able to subclass or subtype the zip importer in their C code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relying only on the stable ABI is an opt-in thing when you build an extension, so leaving this out of it won't prevent it from being available overall, just in a certain instance.


struct _zipimporter {
PyObject_HEAD
PyObject *archive; /* pathname of the Zip archive,
decoded from the filesystem encoding */
PyObject *prefix; /* file prefix: "a/sub/directory/",
encoded to the filesystem encoding */
PyObject *files; /* dict with file info {path: toc_entry} */
};

typedef struct _zipimporter ZipImporter;
PyAPI_DATA(PyTypeObject) ZipImporter_Type;

#define ZipImporter_Check(op) PyObject_TypeCheck(op, &ZipImporter_Type)
#define ZipImporter_Check(op) PyObject_TypeCheck(op, &PyZipImporter_Type)

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/clinic/zipimport.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ zipimport_zipimporter___init__(PyObject *self, PyObject *args, PyObject *kwargs)
int return_value = -1;
PyObject *path;

if ((Py_TYPE(self) == &ZipImporter_Type) &&
if ((Py_TYPE(self) == &PyZipImporter_Type) &&
!_PyArg_NoKeywords("zipimporter", kwargs)) {
goto exit;
}
Expand Down
21 changes: 17 additions & 4 deletions Modules/zipimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ static struct st_zip_searchorder zip_searchorder[] = {
{"", 0}
};

/* zipimporter object definition and support */

struct _zipimporter {
PyObject_HEAD
PyObject *archive; /* pathname of the Zip archive,
decoded from the filesystem encoding */
PyObject *prefix; /* file prefix: "a/sub/directory/",
encoded to the filesystem encoding */
PyObject *files; /* dict with file info {path: toc_entry} */
};

typedef struct _zipimporter ZipImporter;

static PyObject *ZipImportError;
/* read_directory() cache */
static PyObject *zip_directory_cache = NULL;
Expand Down Expand Up @@ -788,7 +801,7 @@ static PyMemberDef zipimporter_members[] = {

#define DEFERRED_ADDRESS(ADDR) 0

PyTypeObject ZipImporter_Type = {
PyTypeObject PyZipImporter_Type = {
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
"zipimport.zipimporter",
sizeof(ZipImporter),
Expand Down Expand Up @@ -1570,7 +1583,7 @@ PyInit_zipimport(void)
{
PyObject *mod;

if (PyType_Ready(&ZipImporter_Type) < 0)
if (PyType_Ready(&PyZipImporter_Type) < 0)
return NULL;

/* Correct directory separator */
Expand All @@ -1591,9 +1604,9 @@ PyInit_zipimport(void)
ZipImportError) < 0)
return NULL;

Py_INCREF(&ZipImporter_Type);
Py_INCREF(&PyZipImporter_Type);
if (PyModule_AddObject(mod, "zipimporter",
(PyObject *)&ZipImporter_Type) < 0)
(PyObject *)&PyZipImporter_Type) < 0)
return NULL;

zip_directory_cache = PyDict_New();
Expand Down