8000 bpo-35381 Remove all static state from posixmodule by eduardo-elizondo · Pull Request #15892 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-35381 Remove all static state from posixmodule #15892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 45 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e09cf81
Make Posixmodule use PyType_FromSpec
eduardo-elizondo Dec 3, 2018
40f0ff4
Added NEWS
eduardo-elizondo Dec 3, 2018
5be18a1
Updated hash
eduardo-elizondo Dec 3, 2018
9479848
Addressed PR Issues
eduardo-elizondo Jan 19, 2019
d31df5f
Rebased
eduardo-elizondo Jan 19, 2019
c84689c
Ran argument clinic
eduardo-elizondo Jan 19, 2019
796a8d7
Added NEWS
eduardo-elizondo Jan 19, 2019
b7cae5b
Remove INCREF from GenericAlloc
eduardo-elizondo Jan 19, 2019
5fa6e46
Make Posixmodule use PyType_FromSpec
eduardo-elizondo Dec 3, 2018
df178b4
Added NEWS
eduardo-elizondo Dec 3, 2018
e59208f
Updated hash
eduardo-elizondo Dec 3, 2018
def6467
Addressed PR Issues
eduardo-elizondo Jan 19, 2019
bb2ead8
Ran argument clinic
eduardo-elizondo Jan 19, 2019
427c772
Added NEWS
eduardo-elizondo Jan 19, 2019
7b9b85f
Merged to master
eduardo-elizondo Sep 11, 2019
9e8fb83
Merge branch 'master' into posixmodule-fromspec
eduardo-elizondo Sep 11, 2019
99d5c26
Cleaned up slot acces
eduardo-elizondo Sep 11, 2019
ca19f51
Cleaned up more slot accesses
eduardo-elizondo Sep 11, 2019
8af0c4e
Nits
eduardo-elizondo Sep 11, 2019
cee2cd9
Fixes
eduardo-elizondo Sep 11, 2019
a8d2f56
Run clinic
eduardo-elizondo Sep 11, 2019
5e4d2ab
Run clinic
eduardo-elizondo Sep 11, 2019
c42ec45
Improve error handling
encukou Sep 11, 2019
d27a547
Remove static from posixmodule.c
eduardo-elizondo Sep 12, 2019
6af98a9
Merge branch 'posixmodule-fromspec' of https://github.com/eduardo-eli…
eduardo-elizondo Sep 12, 2019
a723d1c
Address Comments
eduardo-elizondo Sep 12, 2019
e91096a
Address Comments
eduardo-elizondo Sep 12, 2019
3cf259e
Fix Windows build
eduardo-elizondo Sep 12, 2019
925a41e
Use interned strings for constants in module state
encukou Sep 13, 2019
ad7359b
Use descriptor directly, rather than look it up by name
encukou Sep 13, 2019
5de545d
Bring _Py_IDENTIFIER(__fspath__) back
encukou Sep 13, 2019
3ada298
Use the tp_free slot directly
encukou Sep 13, 2019
4c80b06
Remove duplicate function
encukou Sep 13, 2019
6e505ac
Use tp_new directly
encukou Sep 13, 2019
b9a49aa
Don't call PyState_AddModule
encukou Sep 13, 2019
c1a6d03
Add __new__ changes
eduardo-elizondo Sep 17, 2019
bbe887f
Merge branch 'master' into posixmodule-fromspec
eduardo-elizondo Sep 17, 2019
4a6d4ec
Regenerate clinic
eduardo-elizondo Sep 18, 2019
138ffb6
Nits
eduardo-elizondo Sep 23, 2019
78fbed3
Merge to master
eduardo-elizondo Nov 3, 2019
1b57f3a
Revert tp_new changes
eduardo-elizondo Nov 3, 2019
e7e9634
Rerun argument clinit
eduardo-elizondo Nov 3, 2019
3bf99bf
More fixes
eduardo-elizondo Nov 3, 2019
afb249c
Re-add newlines and fix docstring for os.sched_param.__new__
encukou Nov 5, 2019
187b5d4
Code style nitpicks
encukou Nov 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Addressed PR Issues
  • Loading branch information
eduardo-elizondo committed Sep 11, 2019
commit def6467746cbbb17cf87657adb94e3de884fed79
30 changes: 30 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3566,6 +3566,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)
10000
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 @@ -3600,6 +3618,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.

69 changes: 69 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -12347,13 +12347,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 @@ -12614,6 +12623,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 @@ -12645,6 +12685,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 @@ -12655,6 +12697,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 @@ -13061,23 +13104,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
0