Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
zlib_into
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dataAnalysis
zlib_into
Commits
281ed3f6
Commit
281ed3f6
authored
2 months ago
by
Thomas Kluyver
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit
parents
Branches
feat/sfx-hitfinder
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
.gitignore
+4
-0
4 additions, 0 deletions
.gitignore
pyproject.toml
+12
-0
12 additions, 0 deletions
pyproject.toml
setup.cfg
+2
-0
2 additions, 0 deletions
setup.cfg
try.py
+11
-0
11 additions, 0 deletions
try.py
zlib_into.c
+120
-0
120 additions, 0 deletions
zlib_into.c
with
149 additions
and
0 deletions
.gitignore
0 → 100644
+
4
−
0
View file @
281ed3f6
build/
dist/
wheelhouse/
*.egg-info
This diff is collapsed.
Click to expand it.
pyproject.toml
0 → 100644
+
12
−
0
View file @
281ed3f6
[build-system]
requires
=
[
"setuptools"
]
build-backend
=
"setuptools.build_meta"
[project]
name
=
"zlib-into"
version
=
"0.1"
[tool.setuptools]
ext-modules
=
[
{name
=
"zlib_into"
,
sources
=
[
"zlib_into.c"
],
libraries
=
[
"z"
],
py-limited-api
=
true
}
]
This diff is collapsed.
Click to expand it.
setup.cfg
0 → 100644
+
2
−
0
View file @
281ed3f6
[bdist_wheel]
py_limited_api=cp311
This diff is collapsed.
Click to expand it.
try.py
0 → 100644
+
11
−
0
View file @
281ed3f6
import
zlib
import
zlib_into
a
=
b
'
abcde
'
*
5000
b
=
bytearray
(
5000
)
res
=
zlib_into
.
compress_into
(
a
,
b
,
1
,
15
)
print
(
"
res
"
,
res
)
print
(
b
[:
res
])
print
(
"
Roundtrip
"
,
zlib
.
decompress
(
b
[:
res
])
==
a
)
This diff is collapsed.
Click to expand it.
zlib_into.c
0 → 100644
+
120
−
0
View file @
281ed3f6
#define PY_SSIZE_T_CLEAN
#define Py_LIMITED_API 0x030B0000
#include
<stdlib.h>
#include
<Python.h>
#include
"zlib.h"
#define DEF_MEM_LEVEL 8
// malloc & free copied from CPython
static
void
*
PyZlib_Malloc
(
voidpf
ctx
,
uInt
items
,
uInt
size
)
{
if
(
size
!=
0
&&
items
>
(
size_t
)
PY_SSIZE_T_MAX
/
size
)
return
NULL
;
/* PyMem_Malloc() cannot be used: the GIL is not held when
inflate() and deflate() are called */
/* The builtin zlib module uses PyMem_RawMalloc here, but this was only
added to the stable ABI from Python 3.13, so use plain malloc for now */
return
malloc
((
size_t
)
items
*
(
size_t
)
size
);
}
static
void
PyZlib_Free
(
voidpf
ctx
,
void
*
ptr
)
{
/* The builtin zlib module uses PyMem_RawFree here, but this was only
added to the stable ABI from Python 3.13, so use plain free for now */
free
(
ptr
);
}
static
PyObject
*
compress_into
(
PyObject
*
module
,
PyObject
*
args
)
{
PyObject
*
return_value
=
NULL
;
Py_buffer
input
,
output
;
Py_ssize_t
bytes_written
;
int
level
,
wbits
;
z_stream
zst
;
if
(
!
PyArg_ParseTuple
(
args
,
"y*w*ii"
,
&
input
,
&
output
,
&
level
,
&
wbits
))
{
return
NULL
;
}
if
(
output
.
len
<=
0
)
{
PyErr_SetString
(
PyExc_ValueError
,
"Output buffer may not be 0 size"
);
goto
done
;
}
zst
.
opaque
=
NULL
;
zst
.
zalloc
=
PyZlib_Malloc
;
zst
.
zfree
=
PyZlib_Free
;
zst
.
next_in
=
input
.
buf
;
zst
.
avail_in
=
input
.
len
;
zst
.
next_out
=
output
.
buf
;
zst
.
avail_out
=
output
.
len
;
int
err
=
deflateInit2
(
&
zst
,
level
,
Z_DEFLATED
,
wbits
,
DEF_MEM_LEVEL
,
Z_DEFAULT_STRATEGY
);
switch
(
err
)
{
case
Z_OK
:
break
;
case
Z_MEM_ERROR
:
PyErr_SetString
(
PyExc_MemoryError
,
"Out of memory while compressing data"
);
goto
done
;
case
Z_STREAM_ERROR
:
PyErr_SetString
(
PyExc_ValueError
,
"Bad compression level"
);
goto
done
;
default:
deflateEnd
(
&
zst
);
PyErr_SetString
(
PyExc_RuntimeError
,
"Other error"
);
//zlib_error(state, zst, err, "while compressing data");
goto
done
;
}
Py_BEGIN_ALLOW_THREADS
err
=
deflate
(
&
zst
,
Z_FINISH
);
Py_END_ALLOW_THREADS
switch
(
err
)
{
case
Z_STREAM_END
:
break
;
case
Z_OK
:
case
Z_BUF_ERROR
:
deflateEnd
(
&
zst
);
PyErr_SetString
(
PyExc_BufferError
,
"Not enough space in output buffer"
);
goto
done
;
default:
deflateEnd
(
&
zst
);
PyErr_SetString
(
PyExc_RuntimeError
,
"Other error"
);
goto
done
;
}
bytes_written
=
output
.
len
-
zst
.
avail_out
;
deflateEnd
(
&
zst
);
return_value
=
PyLong_FromSsize_t
(
bytes_written
);
done:
PyBuffer_Release
(
&
input
);
PyBuffer_Release
(
&
output
);
return
return_value
;
}
static
PyMethodDef
ZlibIntoMethods
[]
=
{
{
"compress_into"
,
compress_into
,
METH_VARARGS
,
"zlib compress data into a buffer"
},
{
NULL
,
NULL
,
0
,
NULL
}
};
static
struct
PyModuleDef
zlibintomodule
=
{
PyModuleDef_HEAD_INIT
,
"zlib_into"
,
NULL
,
// docstring
-
1
,
ZlibIntoMethods
};
PyMODINIT_FUNC
PyInit_zlib_into
(
void
)
{
return
PyModule_Create
(
&
zlibintomodule
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment