Skip to content

Full-javascript parser for Python's pickle format

License

Notifications You must be signed in to change notification settings

geiseri/node-jpickle

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-jpickle

Build Status

Full-javascript parser for Python's pickle format.

It currently supports most opcodes for:

  • protocol 0
  • protocol 1
  • protocol 2

Installation

npm install jpickle

Usage

Here is a basic example for parsing a pickled string:

var jpickle = require('jpickle');
jpickle.loads('U\x0bhello worldq\x01.');

To handle more complex objects from jpickle the Javascript objects first need to be registered with the the module. For most basic cases these can just be empty objects that are mapped to a a python class name. If the type is not registered with the emulated member then the unpickle will fail with an exception.

Python

class MyClass:
def __init__(self):
    self.data = "test"

Node

function MyClass() {}

var jpickle = require('jpickle');
jpickle.emulated['__main__.MyClass'] = MyClass;
var unpickled = jpickle.loads(pickled);
// unpickled.data is now "test"

If the class being unpicked uses inheritance the base classes need to be registered also.

Python

class MyClass:
def __init__(self):
    self.data = "test"
       
class MyOtherClass:
    def __init__(self):
        self.myclass = MyClass()
        self.myclasses = [ MyClass(), MyClass() ]
        self.myclasses[0].data = "new test value"

class MySubClass(MyOtherClass):
    def __init__(self):
        MyOtherClass.__init__(self)
        self.subvalue = 12

Node

function MyClass() {}
function MyOtherClass() {}
function MySubClass() {}

var jpickle = require('jpickle');
jpickle.emulated['__main__.MyClass'] = MyClass;
jpickle.emulated['__main__.MyOtherClass'] = MyOtherClass;
jpickle.emulated['__main__.MySubClass'] = MySubClass;
          
var unpickled = jpickle.loads(pickled);
// unpickled.myclasses[0].data is now "new test value"
// unpickled.subvalue is now "12"

In cases that more emulation is desired such as member functions those can be added to the object prototype.

Running Tests

To run the tests, install Mocha then run:

mocha

About

Full-javascript parser for Python's pickle format

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 86.5%
  • Python 13.5%