Skip to content

Commit

Permalink
Addressed PR Issues
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardo-elizondo committed Jan 19, 2019
1 parent 5be18a1 commit 9479848
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Include/objimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ _PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
{
assert(op != NULL);
Py_TYPE(op) = typeobj;
if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(typeobj);
}
_Py_NewReference(op);
return op;
}
Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3324,6 +3324,24 @@ def test_os_all(self):
self.assertIn('walk', os.__all__)


class TestDirEntry(unittest.TestCase):
def setUp(self):
self.path = os.path.realpath(support.TESTFN)
self.addCleanup(support.rmtree, self.path)
os.mkdir(self.path)

def test_uninstantiable(self):
self.assertRaises(TypeError, os.DirEntry)

def test_unpickable(self):
filename = create_file(os.path.join(self.path, "file.txt"), b'python')
entry = [entry for entry in os.scandir(self.path)].pop()
self.assertIsInstance(entry, os.DirEntry)
self.assertEqual(entry.name, "file.txt")
import pickle
self.assertRaises(TypeError, pickle.dumps, entry, filename)


class TestScandir(unittest.TestCase):
check_no_resource_warning = support.check_no_resource_warning

Expand Down Expand Up @@ -3358,6 +3376,18 @@ def assert_stat_equal(self, stat1, stat2, skip_fields):
else:
self.assertEqual(stat1, stat2)

def test_uninstantiable(self):
scandir_iter = os.scandir(self.path)
self.assertRaises(TypeError, type(scandir_iter))
scandir_iter.close()

def test_unpickable(self):
filename = self.create_file("file.txt")
scandir_iter = os.scandir(self.path)
import pickle
self.assertRaises(TypeError, pickle.dumps, scandir_iter, filename)
scandir_iter.close()

def check_entry(self, entry, name, is_dir, is_file, is_symlink):
self.assertIsInstance(entry, os.DirEntry)
self.assertEqual(entry.name, name)
Expand Down

This file was deleted.

47 changes: 46 additions & 1 deletion Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -12020,13 +12020,22 @@ typedef struct {
#endif
} DirEntry;

static PyObject *
DirEntry_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyErr_Format(PyExc_TypeError,
"cannot create '%.100s' instances", type->tp_name);
return NULL;
}

static void
DirEntry_dealloc(DirEntry *entry)
{
Py_XDECREF(entry->name);
Py_XDECREF(entry->path);
Py_XDECREF(entry->stat);
Py_XDECREF(entry->lstat);
Py_DECREF(Py_TYPE(entry));
Py_TYPE(entry)->tp_free((PyObject *)entry);
}

Expand Down Expand Up @@ -12287,6 +12296,37 @@ os_DirEntry_inode_impl(DirEntry *self)
#endif
}

/*[clinic input]
os.DirEntry.__reduce__
returns null and raises an exception to avoid pickling
[clinic start generated code]*/

static PyObject *
os_DirEntry___reduce___impl(DirEntry *self)
/*[clinic end generated code: output=45167543e30c210c input=c1689a589f9c38f2]*/
{
PyErr_Format(PyExc_TypeError,
"cannot pickle '%.100s' instances", Py_TYPE(self)->tp_name);
return NULL;
}

/*[clinic input]
os.DirEntry.__reduce_ex__
protocol: int
/
Returns NULL and raises an exception to avoid pickling
[clinic start generated code]*/

static PyObject *
os_DirEntry___reduce_ex___impl(DirEntry *self, int protocol)
/*[clinic end generated code: output=a81881dfe241a631 input=1afbee3b136a7ece]*/
{
return os_DirEntry___reduce___impl(self);
}

static PyObject *
DirEntry_repr(DirEntry *self)
{
Expand Down Expand Up @@ -12318,6 +12358,8 @@ static PyMemberDef DirEntry_members[] = {
#include "clinic/posixmodule.c.h"

static PyMethodDef DirEntry_methods[] = {
OS_DIRENTRY___REDUCE___METHODDEF
OS_DIRENTRY___REDUCE_EX___METHODDEF
OS_DIRENTRY_IS_DIR_METHODDEF
OS_DIRENTRY_IS_FILE_METHODDEF
OS_DIRENTRY_IS_SYMLINK_METHODDEF
Expand All @@ -12328,6 +12370,7 @@ static PyMethodDef DirEntry_methods[] = {
};

static PyType_Slot DirEntryType_slots[] = {
{Py_tp_new, DirEntry_new},
{Py_tp_dealloc, DirEntry_dealloc},
{Py_tp_repr, DirEntry_repr},
{Py_tp_methods, DirEntry_methods},
Expand Down Expand Up @@ -12734,23 +12777,49 @@ ScandirIterator_finalize(ScandirIterator *iterator)
PyErr_Restore(error_type, error_value, error_traceback);
}

static PyObject *
ScandirIterator_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyErr_Format(PyExc_TypeError,
"cannot create '%.100s' instances", type->tp_name);
return NULL;
}

static PyObject *
ScandirIterator_reduce(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyErr_Format(PyExc_TypeError,
"cannot pickle '%.100s' instances", Py_TYPE(self)->tp_name);
return NULL;
}

static PyObject *
ScandirIterator_reduce_ex(PyObject *self, PyObject *arg)
{
return ScandirIterator_reduce(self, NULL, NULL);
}

static void
ScandirIterator_dealloc(ScandirIterator *iterator)
{
if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
return;

Py_DECREF(Py_TYPE(iterator));
Py_TYPE(iterator)->tp_free((PyObject *)iterator);
}

static PyMethodDef ScandirIterator_methods[] = {
{"__reduce__", (PyCFunction)ScandirIterator_reduce, METH_NOARGS},
{"__reduce_ex__", (PyCFunction)ScandirIterator_reduce_ex, METH_O},
{"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
{"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
{"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
{NULL}
};

static PyType_Slot ScandirIteratorType_slots[] = {
{Py_tp_new, ScandirIterator_new},
{Py_tp_dealloc, ScandirIterator_dealloc},
{Py_tp_finalize, ScandirIterator_finalize},
{Py_tp_iter, PyObject_SelfIter},
Expand Down
8 changes: 5 additions & 3 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ PyObject_Init(PyObject *op, PyTypeObject *tp)
return PyErr_NoMemory();
/* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Py_TYPE(op) = tp;
if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(tp);
}
_Py_NewReference(op);
return op;
}
Expand All @@ -240,9 +243,8 @@ PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
if (op == NULL)
return (PyVarObject *) PyErr_NoMemory();
/* Any changes should be reflected in PyObject_INIT_VAR */
op->ob_size = size;
Py_TYPE(op) = tp;
_Py_NewReference((PyObject *)op);
Py_SIZE(op) = size;
PyObject_Init((PyObject *)op, tp);
return op;
}

Expand Down

0 comments on commit 9479848

Please sign in to comment.