Skip to content
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

MAINT: Python 3.12 support improve #4300

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Changes from all commits
Commits
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
80 changes: 52 additions & 28 deletions package/MDAnalysis/lib/picklable_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,23 @@
self._mode = mode
super().__init__(name, mode)

def __getstate__(self):

def __setstate__(self, state):
name = state["name_val"]
super().__init__(name, mode='r')
try:
self.seek(state["tell_val"])
except KeyError:
pass

Check warning on line 119 in package/MDAnalysis/lib/picklable_file_io.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/lib/picklable_file_io.py#L118-L119

Added lines #L118 - L119 were not covered by tests

def __reduce_ex__(self, prot):
if self._mode != 'r':
raise RuntimeError("Can only pickle files that were opened "
"in read mode, not {}".format(self._mode))
return self.name, self.tell()

def __setstate__(self, args):
name = args[0]
super().__init__(name, mode='r')
self.seek(args[1])
return (self.__class__,
(self.name, self._mode),
{"name_val": self.name,
"tell_val": self.tell()})


class BufferIOPicklable(io.BufferedReader):
Expand Down Expand Up @@ -151,16 +158,22 @@
super().__init__(raw)
self.raw_class = raw.__class__

def __getstate__(self):
return self.raw_class, self.name, self.tell()

def __setstate__(self, args):
raw_class = args[0]
name = args[1]
def __setstate__(self, state):
raw_class = state["raw_class"]
name = state["name_val"]

Check warning on line 164 in package/MDAnalysis/lib/picklable_file_io.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/lib/picklable_file_io.py#L163-L164

Added lines #L163 - L164 were not covered by tests
raw = raw_class(name)
super().__init__(raw)
self.seek(args[2])
self.seek(state["tell_val"])

Check warning on line 167 in package/MDAnalysis/lib/picklable_file_io.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/lib/picklable_file_io.py#L167

Added line #L167 was not covered by tests

def __reduce_ex__(self, prot):
# don't ask, for Python 3.12+ see:
# https://github.com/python/cpython/pull/104370
return (self.raw_class,
(self.name,),
{"raw_class": self.raw_class,
"name_val": self.name,
"tell_val": self.tell()})

class TextIOPicklable(io.TextIOWrapper):
"""Character and line based picklable file-like object.
Expand Down Expand Up @@ -197,22 +210,26 @@
super().__init__(raw)
self.raw_class = raw.__class__

def __getstate__(self):
try:
name = self.name
except AttributeError:
# This is kind of ugly--BZ2File does not save its name.
name = self.buffer._fp.name
return self.raw_class, name

def __setstate__(self, args):
raw_class = args[0]
name = args[1]
raw_class = args["raw_class"]
name = args["name_val"]
# raw_class is used for further expansion this functionality to
# Gzip files, which also requires a text wrapper.
raw = raw_class(name)
super().__init__(raw)

def __reduce_ex__(self, prot):
try:
name = self.name
except AttributeError:
# This is kind of ugly--BZ2File does not save its name.
name = self.buffer._fp.name
return (self.__class__.__new__,
(self.__class__,),
{"raw_class": self.raw_class,
"name_val": name})


class BZ2Picklable(bz2.BZ2File):
"""File object (read-only) for bzip2 (de)compression that can be pickled.
Expand Down Expand Up @@ -269,11 +286,14 @@
if not self._bz_mode.startswith('r'):
raise RuntimeError("Can only pickle files that were opened "
"in read mode, not {}".format(self._bz_mode))
return self._fp.name, self.tell()
return {"name_val": self._fp.name, "tell_val": self.tell()}

def __setstate__(self, args):
super().__init__(args[0])
self.seek(args[1])
super().__init__(args["name_val"])
try:
self.seek(args["tell_val"])
except KeyError:
pass

Check warning on line 296 in package/MDAnalysis/lib/picklable_file_io.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/lib/picklable_file_io.py#L295-L296

Added lines #L295 - L296 were not covered by tests


class GzipPicklable(gzip.GzipFile):
Expand Down Expand Up @@ -331,11 +351,15 @@
if not self._gz_mode.startswith('r'):
raise RuntimeError("Can only pickle files that were opened "
"in read mode, not {}".format(self._gz_mode))
return self.name, self.tell()
return {"name_val": self.name,
"tell_val": self.tell()}

def __setstate__(self, args):
super().__init__(args[0])
self.seek(args[1])
super().__init__(args["name_val"])
try:
self.seek(args["tell_val"])
except KeyError:
pass

Check warning on line 362 in package/MDAnalysis/lib/picklable_file_io.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/lib/picklable_file_io.py#L361-L362

Added lines #L361 - L362 were not covered by tests


def pickle_open(name, mode='rt'):
Expand Down
Loading