-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support protocol 5 * PObject enhancement * update docs * fix typo * add more ut * update docs
- Loading branch information
Showing
10 changed files
with
239 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pickle | ||
|
||
class mybytearray(bytearray): | ||
|
||
def __reduce_ex__(self, protocol): | ||
if protocol >= 5: | ||
return type(self), (pickle.PickleBuffer(self),), None | ||
|
||
key = mybytearray([0x01, 0x02, 0x03, 0x04]) | ||
filehandler = open(b"bytearray8.pkl", "wb") | ||
|
||
buffers = [] | ||
t= memoryview(bytearray()) | ||
oo = [pickle.PickleBuffer(memoryview(bytearray()).toreadonly()), pickle.PickleBuffer(bytearray())] | ||
d = pickle.dumps(oo, pickle.HIGHEST_PROTOCOL, buffer_callback= lambda _: False) | ||
pickle.dump(mybytearray([0x01, 0x02, 0x03, 0x04]), filehandler, pickle.HIGHEST_PROTOCOL, buffer_callback= lambda _: True) | ||
print(buffers) | ||
buffers = [bytearray([]), []] | ||
l = pickle.loads(d, buffers=buffers) | ||
print(l) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { createPObject } from '../src/PObject'; | ||
|
||
describe('PObject', () => { | ||
describe('createPObject', () => { | ||
it('can be created', () => { | ||
const name = 'name'; | ||
const module = 'module'; | ||
const pobject = createPObject(module, name); | ||
expect(pobject).toBeDefined(); | ||
}); | ||
|
||
it('can be used with as a class', () => { | ||
const name = 'name'; | ||
const module = 'module'; | ||
const data = [1, true, [null, 'str']]; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const pobject = createPObject<new (...args: any[]) => any>(module, name); | ||
const obj = new pobject(...data); | ||
expect(obj).toHaveProperty('__module__', module); | ||
expect(obj).toHaveProperty('__name__', name); | ||
expect(obj.args).toStrictEqual(data); | ||
}); | ||
|
||
it('can be used with as a function', () => { | ||
const name = 'name'; | ||
const module = 'module'; | ||
const data = [1, true, [null, 'str']]; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const pobject = createPObject<(...args: any[]) => any>(module, name); | ||
const obj = pobject(...data); | ||
expect(obj).toHaveProperty('__module__', module); | ||
expect(obj).toHaveProperty('__name__', name); | ||
expect(obj.args).toStrictEqual(data); | ||
}); | ||
|
||
it('can be worked with __setnewargs_ex__', () => { | ||
const name = 'name'; | ||
const module = 'module'; | ||
const data = [1, true, [null, 'str']]; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const pobject = createPObject<new (...args: any[]) => any>(module, name); | ||
const obj = new pobject(...data); | ||
obj.__setnewargs_ex__(...data); | ||
expect(obj).toHaveProperty('__module__', module); | ||
expect(obj).toHaveProperty('__name__', name); | ||
expect(obj.args).toStrictEqual(data); | ||
expect(obj.kwargs).toStrictEqual(data); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pickle | ||
|
||
def bytearray8(): | ||
key = bytearray([0x01, 0x02, 0x03]) | ||
return key | ||
|
||
def next_buffer(): | ||
key = pickle.PickleBuffer(bytearray()) | ||
return key | ||
|
||
def multi_next_buffer(): | ||
key = [pickle.PickleBuffer(bytearray()), pickle.PickleBuffer(bytearray())] | ||
return key | ||
|
||
def readonly_buffer(): | ||
key = pickle.PickleBuffer(memoryview(bytearray()).toreadonly()) | ||
return key | ||
|
||
def next_buffer_and_readonly_buffer(): | ||
key = [pickle.PickleBuffer(memoryview(bytearray()).toreadonly()), pickle.PickleBuffer(bytearray())] | ||
return key | ||
|
||
class mybytearray(bytearray): | ||
def __reduce_ex__(self, protocol): | ||
if protocol >= 5: | ||
return type(self), (pickle.PickleBuffer(self),), None | ||
|
||
def next_buffer_with_reduce_ex(): | ||
key = mybytearray([0x01, 0x02, 0x03, 0x04]) | ||
return key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters