|
| 1 | +# Copyright (c) "Neo4j" |
| 2 | +# Neo4j Sweden AB [https://neo4j.com] |
| 3 | +# |
| 4 | +# This file is part of Neo4j. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | + |
| 19 | +import asyncio |
| 20 | +import tracemalloc |
| 21 | +import typing as t |
| 22 | +from functools import wraps |
| 23 | +from warnings import warn |
| 24 | + |
| 25 | + |
| 26 | +_FuncT = t.TypeVar("_FuncT", bound=t.Callable) |
| 27 | + |
| 28 | + |
| 29 | +# Can be automatically overridden in builds |
| 30 | +package = "neo4j" |
| 31 | +version = "5.0.dev0" |
| 32 | + |
| 33 | + |
| 34 | +def get_user_agent(): |
| 35 | + """ Obtain the default user agent string sent to the server after |
| 36 | + a successful handshake. |
| 37 | + """ |
| 38 | + from sys import ( |
| 39 | + platform, |
| 40 | + version_info, |
| 41 | + ) |
| 42 | + template = "neo4j-python/{} Python/{}.{}.{}-{}-{} ({})" |
| 43 | + fields = (version,) + tuple(version_info) + (platform,) |
| 44 | + return template.format(*fields) |
| 45 | + |
| 46 | + |
| 47 | +def _id(x): |
| 48 | + return x |
| 49 | + |
| 50 | + |
| 51 | +def copy_signature(_: _FuncT) -> t.Callable[[t.Callable], _FuncT]: |
| 52 | + return _id |
| 53 | + |
| 54 | + |
| 55 | +def deprecation_warn(message, stack_level=1): |
| 56 | + warn(message, category=DeprecationWarning, stacklevel=stack_level + 1) |
| 57 | + |
| 58 | + |
| 59 | +def deprecated(message: str) -> t.Callable[[_FuncT], _FuncT]: |
| 60 | + """ Decorator for deprecating functions and methods. |
| 61 | +
|
| 62 | + :: |
| 63 | +
|
| 64 | + @deprecated("'foo' has been deprecated in favour of 'bar'") |
| 65 | + def foo(x): |
| 66 | + pass |
| 67 | +
|
| 68 | + """ |
| 69 | + def decorator(f): |
| 70 | + if asyncio.iscoroutinefunction(f): |
| 71 | + @wraps(f) |
| 72 | + async def inner(*args, **kwargs): |
| 73 | + deprecation_warn(message, stack_level=2) |
| 74 | + return await f(*args, **kwargs) |
| 75 | + |
| 76 | + return inner |
| 77 | + else: |
| 78 | + @wraps(f) |
| 79 | + def inner(*args, **kwargs): |
| 80 | + deprecation_warn(message, stack_level=2) |
| 81 | + return f(*args, **kwargs) |
| 82 | + |
| 83 | + return inner |
| 84 | + |
| 85 | + return decorator |
| 86 | + |
| 87 | + |
| 88 | +def deprecated_property(message: str): |
| 89 | + def decorator(f): |
| 90 | + return property(deprecated(message)(f)) |
| 91 | + return t.cast(property, decorator) |
| 92 | + |
| 93 | + |
| 94 | +class ExperimentalWarning(Warning): |
| 95 | + """ Base class for warnings about experimental features. |
| 96 | + """ |
| 97 | + |
| 98 | + |
| 99 | +def experimental_warn(message, stack_level=1): |
| 100 | + warn(message, category=ExperimentalWarning, stacklevel=stack_level + 1) |
| 101 | + |
| 102 | + |
| 103 | +def experimental(message) -> t.Callable[[_FuncT], _FuncT]: |
| 104 | + """ Decorator for tagging experimental functions and methods. |
| 105 | +
|
| 106 | + :: |
| 107 | +
|
| 108 | + @experimental("'foo' is an experimental function and may be " |
| 109 | + "removed in a future release") |
| 110 | + def foo(x): |
| 111 | + pass |
| 112 | +
|
| 113 | + """ |
| 114 | + def decorator(f): |
| 115 | + if asyncio.iscoroutinefunction(f): |
| 116 | + @wraps(f) |
| 117 | + async def inner(*args, **kwargs): |
| 118 | + experimental_warn(message, stack_level=2) |
| 119 | + return await f(*args, **kwargs) |
| 120 | + |
| 121 | + return inner |
| 122 | + else: |
| 123 | + @wraps(f) |
| 124 | + def inner(*args, **kwargs): |
| 125 | + experimental_warn(message, stack_level=2) |
| 126 | + return f(*args, **kwargs) |
| 127 | + |
| 128 | + return inner |
| 129 | + |
| 130 | + return decorator |
| 131 | + |
| 132 | + |
| 133 | +def unclosed_resource_warn(obj): |
| 134 | + msg = f"Unclosed {obj!r}." |
| 135 | + trace = tracemalloc.get_object_traceback(obj) |
| 136 | + if trace: |
| 137 | + msg += "\nObject allocated at (most recent call last):\n" |
| 138 | + msg += "\n".join(trace.format()) |
| 139 | + else: |
| 140 | + msg += "\nEnable tracemalloc to get the object allocation traceback." |
| 141 | + warn(msg, ResourceWarning, stacklevel=2, source=obj) |
0 commit comments