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

Compile serialize.py with Cython #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ develop-eggs
src/pjpersist.egg-info/
.installed.cfg
tmp
*.py?
*.pyc
*.so
*.c
.coverage
htmlcov
coverage
Expand Down
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
import os
from setuptools import setup, find_packages
from Cython.Build import cythonize


def read(*rnames):
Expand Down Expand Up @@ -36,7 +37,7 @@ def alltests():

setup(
name='pjpersist',
version='2.0.2.dev0',
version='2.0.2.dev1',
author="Shoobx Team",
author_email="[email protected]",
url='https://github.com/Shoobx/pjpersist',
Expand All @@ -63,13 +64,14 @@ def alltests():
packages=find_packages('src'),
package_dir={'': 'src'},
data_files=[('pjpersist', ['README.rst'])], # in order to test it with tox
extras_require = dict(
extras_require=dict(
test=TESTS_REQUIRE,
zope=(
'zope.container',
),
),
install_requires=[
'Cython',
'future',
'persistent',
'transaction',
Expand All @@ -85,6 +87,7 @@ def alltests():
],
include_package_data=True,
zip_safe=False,
ext_modules=cythonize(["src/pjpersist/serialize.pyx"]),
entry_points='''
[console_scripts]
profile = pjpersist.tests.performance:main
Expand Down
28 changes: 17 additions & 11 deletions src/pjpersist/serialize.py → src/pjpersist/serialize.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# cython: language_level=3
# cython: profile=False
##############################################################################
#
# Copyright (c) 2011 Zope Foundation and Contributors.
Expand Down Expand Up @@ -143,6 +145,10 @@ class PersistentList(persistent.list.PersistentList):


class DBRef(object):
_table: str
_id: str
_database: str
hash: int

def __init__(self, table, id, database=None):
self._table = table
Expand All @@ -151,7 +157,7 @@ def __init__(self, table, id, database=None):
self.__calculate_hash()

def __calculate_hash(self):
self.hash = hash(str(self.database)+str(self.table)+str(self.id))
self.hash = hash(str(self._database)+str(self._table)+str(self._id))

@property
def database(self):
Expand Down Expand Up @@ -184,9 +190,9 @@ def __setstate__(self, state):
self.__init__(state['table'], state['id'], state['database'])

def __getstate__(self):
return {'database': self.database,
'table': self.table,
'id': self.id}
return {'database': self._database,
'table': self._table,
'id': self._id}

def __hash__(self):
return self.hash
Expand All @@ -198,16 +204,16 @@ def __neq__(self, other):
return self.hash != other.hash

def __repr__(self):
return 'DBRef(%r, %r, %r)' %(self.table, self.id, self.database)
return 'DBRef(%r, %r, %r)' % (self._table, self._id, self._database)

def as_tuple(self):
return self.database, self.table, self.id
return self._database, self._table, self._id

def as_json(self):
return {'_py_type': 'DBREF',
'database': self.database,
'table': self.table,
'id': self.id}
'database': self._database,
'table': self._table,
'id': self._id}


class Binary(str):
Expand Down Expand Up @@ -599,9 +605,9 @@ def get_object(self, state, obj):
# the code was:
# if isinstance(state, dict) and state.get('_py_type') == 'DBREF':
# this methods gets called a gazillion times, so being fast is crucial
stateIsDict = isinstance(state, dict)
stateIsDict: int = isinstance(state, dict)
if stateIsDict:
state_py_type = state.get('_py_type')
state_py_type :str = state.get('_py_type')
if state_py_type == 'BINARY':
# Binary data in Python 2 is presented as a string. We will
# convert back to binary when serializing again.
Expand Down